serializer_converter.py 文件源码

python
阅读 34 收藏 0 点赞 0 评论 0

项目:graphene-django 作者: graphql-python 项目源码 文件源码
def convert_serializer_field(field, is_input=True):
    """
    Converts a django rest frameworks field to a graphql field
    and marks the field as required if we are creating an input type
    and the field itself is required
    """

    graphql_type = get_graphene_type_from_serializer_field(field)

    args = []
    kwargs = {
        'description': field.help_text,
        'required': is_input and field.required,
    }

    # if it is a tuple or a list it means that we are returning
    # the graphql type and the child type
    if isinstance(graphql_type, (list, tuple)):
        kwargs['of_type'] = graphql_type[1]
        graphql_type = graphql_type[0]

    if isinstance(field, serializers.ModelSerializer):
        if is_input:
            graphql_type = convert_serializer_to_input_type(field.__class__)
        else:
            global_registry = get_global_registry()
            field_model = field.Meta.model
            args = [global_registry.get_type_for_model(field_model)]

    return graphql_type(*args, **kwargs)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号