Пример #1
0
    def args(self, **kwargs) -> "DSLField":
        r"""Set the arguments of a field

        The arguments are parsed to be stored in the AST of this field.

        .. note::
            You can also call the field directly with your arguments.
            :code:`ds.Query.human(id=1000)` is equivalent to:
            :code:`ds.Query.human.args(id=1000)`

        :param \**kwargs: the arguments (keyword=value)
        :return: itself

        :raises KeyError: if any of the provided arguments does not exist
                          for this field.
        """

        assert self.ast_field.arguments is not None

        self.ast_field.arguments = FrozenList(
            self.ast_field.arguments
            + [
                ArgumentNode(
                    name=NameNode(value=name),
                    value=ast_from_value(value, self._get_argument(name).type),
                )
                for name, value in kwargs.items()
            ]
        )

        log.debug(f"Added arguments {kwargs} in field {self!r})")

        return self
Пример #2
0
Файл: dsl.py Проект: tony/gql
def get_arg_serializer(arg_type, known_serializers):
    if isinstance(arg_type, GraphQLNonNull):
        return get_arg_serializer(arg_type.of_type, known_serializers)
    if isinstance(arg_type, GraphQLInputField):
        return get_arg_serializer(arg_type.type, known_serializers)
    if isinstance(arg_type, GraphQLInputObjectType):
        if arg_type in known_serializers:
            return known_serializers[arg_type]
        known_serializers[arg_type] = None
        serializers = {
            k: get_arg_serializer(v, known_serializers)
            for k, v in arg_type.fields.items()
        }
        known_serializers[arg_type] = lambda value: ObjectValueNode(
            fields=FrozenList(
                ObjectFieldNode(name=NameNode(value=k),
                                value=serializers[k](v))
                for k, v in value.items()))
        return known_serializers[arg_type]
    if isinstance(arg_type, GraphQLList):
        inner_serializer = get_arg_serializer(arg_type.of_type,
                                              known_serializers)
        return partial(serialize_list, inner_serializer)
    if isinstance(arg_type, GraphQLEnumType):
        return lambda value: EnumValueNode(value=arg_type.serialize(value))
    return lambda value: ast_from_value(arg_type.serialize(value), arg_type)
Пример #3
0
def serialize_input_object(arg_type, value):
    serializers = {k: get_arg_serializer(v) for k, v in arg_type.fields.items()}
    result = ast_from_value(value, arg_type)
    for f in result.fields:
        serialized = serializers[f.name.value](value[f.name.value])
        if isinstance(f.value, ast.ListValueNode):
            f.value = ast.ListValueNode(values=serialized)
        else:
            f.value = serialized
    return result
Пример #4
0
def input_value_info(graph, fields, ids):
    nodes_map = _nodes_map(graph)
    for ident in ids:
        node = nodes_map[ident.node]
        field = node.fields_map[ident.field]
        option = field.options_map[ident.name]
        if option.default is Nothing:
            default = None
        elif option.default is None:
            # graphql-core currently can't parse/print "null" values
            default = 'null'
        else:
            default = print_ast(ast_from_value(option.default))
        info = {'id': ident,
                'name': option.name,
                'description': option.description,
                'defaultValue': default}
        yield [info[f.name] for f in fields]
Пример #5
0
def serialize_value(arg_type, value):
    return ast_from_value(
        str(value) if arg_type.serialize(value) is None else arg_type.serialize(value),
        arg_type,
    )