Пример #1
0
def test_parse_creates_ast():
    source = Source("""{
  node(id: 4) {
    id,
    name
  }
}
""")
    result = parse(source)

    assert result == \
        ast.Document(
            loc=Loc(start=0, end=41, source=source),
            definitions=[ast.OperationDefinition(
                loc=Loc(start=0, end=40, source=source),
                operation='query',
                name=None,
                variable_definitions=None,
                directives=[],
                selection_set=ast.SelectionSet(
                    loc=Loc(start=0, end=40, source=source),
                    selections=[ast.Field(
                        loc=Loc(start=4, end=38, source=source),
                        alias=None,
                        name=ast.Name(
                            loc=Loc(start=4, end=8, source=source),
                            value='node'),
                        arguments=[ast.Argument(
                            name=ast.Name(loc=Loc(start=9, end=11, source=source),
                                          value='id'),
                            value=ast.IntValue(
                                   loc=Loc(start=13, end=14, source=source),
                                   value='4'),
                            loc=Loc(start=9, end=14, source=source))],
                        directives=[],
                        selection_set=ast.SelectionSet(
                            loc=Loc(start=16, end=38, source=source),
                            selections=[ast.Field(
                                loc=Loc(start=22, end=24, source=source),
                                alias=None,
                                name=ast.Name(
                                    loc=Loc(start=22, end=24, source=source),
                                    value='id'),
                                arguments=[],
                                directives=[],
                                selection_set=None),
                                ast.Field(
                                loc=Loc(start=30, end=34, source=source),
                                alias=None,
                                name=ast.Name(
                                    loc=Loc(start=30, end=34, source=source),
                                    value='name'),
                                arguments=[],
                                directives=[],
                                selection_set=None)]))]))])
Пример #2
0
def test_get_selections_to_dict():
    selections = [
        ast.Field(name='foo'),
        ast.Field(name='nested', selection_set=ast.SelectionSet([
            ast.Field(name='bar'),
            ast.Field(name='more_nested', selection_set=ast.SelectionSet(selections=[
                ast.Field(name='baz'),
            ]))
        ]))
    ]

    assert {'foo': None, 'nested': {'bar': None, 'more_nested': {'baz': None}}} == selections_to_dict(selections)
Пример #3
0
 def _insert_selection_set_parent_nodes_from_deep_field_name(
         self, selection, deep_field_name):
     split_field_name = deep_field_name.split(LOOKUP_SEP, 1)
     name = to_camel_case(split_field_name[0])
     if len(split_field_name) == 1:
         return ast.Field(
             name=ast.Name(value=name),
             selection_set=ast.SelectionSet(
                 selections=selection.selection_set.selections))
     return ast.Field(
         name=ast.Name(value=name),
         selection_set=ast.SelectionSet(selections=[
             self._insert_selection_set_parent_nodes_from_deep_field_name(
                 selection, split_field_name[1])
         ]))
Пример #4
0
    def _get_field_selection_set(
            self,
            field: GraphQLField,
            include_node: bool = True) -> Optional[graphql_ast.SelectionSet]:
        return_type = self.get_return_type(field.type)

        if isinstance(return_type, (GraphQLScalarType, GraphQLEnumType)):
            return None
        elif isinstance(return_type,
                        (GraphQLObjectType, GrapheneInterfaceType)):
            all_selections = []

            sub_fields = return_type.fields.items()

            if self.is_node_type(return_type):
                if include_node is False:
                    sub_fields = [('id', return_type.fields['id'])]
                else:
                    # disable full rendering of nested nodes to avoid recursion
                    include_node = False

            for name, sub_field in sub_fields:
                selection = graphql_ast.Field(
                    name=graphql_ast.Name(value=name),
                    selection_set=self._get_field_selection_set(
                        sub_field, include_node=include_node))
                all_selections.append(selection)

            return graphql_ast.SelectionSet(selections=all_selections)

        raise NotImplementedError
Пример #5
0
def test_parses_multi_byte_characters():
    result = parse(u'''
        # This comment has a \u0A0A multi-byte character.
        { field(arg: "Has a \u0A0A multi-byte character.") }
    ''',
                   no_location=True,
                   no_source=True)
    assert result == ast.Document(definitions=[
        ast.OperationDefinition(
            operation='query',
            name=None,
            variable_definitions=None,
            directives=[],
            selection_set=ast.SelectionSet(selections=[
                ast.Field(
                    alias=None,
                    name=ast.Name(value=u'field'),
                    arguments=[
                        ast.Argument(
                            name=ast.
                            Name(value=u'arg'),
                            value=ast
                            .StringValue(
                                value=u'Has a \u0a0a multi-byte character.'))
                    ],
                    directives=[],
                    selection_set=None)
            ]))
    ])
Пример #6
0
def test_parses_multi_byte_characters():
    # type: () -> None
    result = parse(
        u"""
        # This comment has a \u0A0A multi-byte character.
        { field(arg: "Has a \u0A0A multi-byte character.") }
    """,
        no_location=True,
        no_source=True,
    )
    assert result == ast.Document(definitions=[
        ast.OperationDefinition(
            operation="query",
            name=None,
            variable_definitions=None,
            directives=[],
            selection_set=ast.SelectionSet(selections=[
                ast.Field(
                    alias=None,
                    name=ast.Name(value=u"field"),
                    arguments=[
                        ast.Argument(
                            name=ast.Name(value=u"arg"),
                            value=ast.StringValue(
                                value=u"Has a \u0a0a multi-byte character."),
                        )
                    ],
                    directives=[],
                    selection_set=None,
                )
            ]),
        )
    ])
Пример #7
0
def query(*fields, **kwargs):
    if 'operation' not in kwargs:
        kwargs['operation'] = 'query'
    return ast.Document(definitions=[
        ast.OperationDefinition(operation=kwargs['operation'],
                                selection_set=ast.SelectionSet(
                                    selections=list(selections(*fields))))
    ])
Пример #8
0
def query(*fields):
    return ast.Document(
        definitions=[ast.OperationDefinition(
            operation='query',
            selection_set=ast.SelectionSet(
                selections=list(selections(*fields))
            )
        )]
    )
Пример #9
0
def query(*fields, **kwargs):
    if "operation" not in kwargs:
        kwargs["operation"] = "query"
    return ast.Document(definitions=[
        ast.OperationDefinition(
            operation=kwargs["operation"],
            selection_set=ast.SelectionSet(selections=list(selections(
                *fields))),
        )
    ])
Пример #10
0
        def view_func():
            variable_values = self.get_variable_values()

            field_selection_set = self._get_field_selection_set(
                field, include_node=True)

            if hasattr(field.type, 'graphene_type') and issubclass(
                    field.type.graphene_type, Node):
                _type_name, _id = Node.from_global_id(variable_values['id'])
                node_type = schema.get_type(_type_name)
                inline_selection = graphql_ast.InlineFragment(
                    type_condition=graphql_ast.NamedType(name=graphql_ast.Name(
                        value=_type_name)),
                    selection_set=self._get_field_selection_set(
                        GraphQLField(node_type), include_node=True))
                field_selection_set.selections.append(inline_selection)

            document_ast = graphql_ast.Document([
                graphql_ast.OperationDefinition(
                    operation=operation,
                    variable_definitions=variable_definitions,
                    selection_set=graphql_ast.SelectionSet(selections=[
                        graphql_ast.Field(name=graphql_ast.Name(
                            value=field_name),
                                          arguments=arguments,
                                          selection_set=field_selection_set)
                    ]))
            ])

            execution_results = schema.execute(document_ast,
                                               variable_values=variable_values)

            # TODO custom encoder that positions data[field_name] at data
            result, status_code = encode_execution_results(
                [execution_results],
                is_batch=False,
                format_error=default_format_error,
                encode=json_encode)

            return Response(result,
                            status=status_code,
                            content_type='application/json')
Пример #11
0
def test_parses_null_value_in_list():
    result = parse('{ fieldWithObjectInput(input: {b: ["A", null, "C"], c: "C"}) }')
    assert result == ast.Document(
        definitions=[
            ast.OperationDefinition(
                operation='query', name=None, variable_definitions=None, directives=[],
                selection_set=ast.SelectionSet(
                    selections=[
                        ast.Field(
                            alias=None,
                            name=ast.Name(value=u'fieldWithObjectInput'),
                            directives=[],
                            selection_set=None,
                            arguments=[
                                ast.Argument(
                                    name=ast.Name(value=u'input'),
                                    value=ast.ObjectValue(
                                        fields=[
                                            ast.ObjectField(
                                                name=ast.Name(value=u'b'),
                                                value=ast.ListValue(
                                                    values=[
                                                        ast.StringValue(value=u'A'),
                                                        ast.NullValue(),
                                                        ast.StringValue(value=u'C'),
                                                    ],
                                                ),
                                            ),
                                            ast.ObjectField(
                                                name=ast.Name(value=u'c'),
                                                value=ast.StringValue(value=u'C'),
                                            ),
                                        ]
                                    ),
                                ),
                            ],
                        ),
                    ],
                ),
            ),
        ],
    )
Пример #12
0
 def select(self, *fields):
     if not self.ast_field.selection_set:
         self.ast_field.selection_set = ast.SelectionSet(selections=[])
     self.ast_field.selection_set.selections.extend(selections(*fields))
     return self
Пример #13
0
def test_parse_creates_ast():
    # type: () -> None
    source = Source(
        """{
  node(id: 4) {
    id,
    name
  }
}
"""
    )
    result = parse(source)

    assert result == ast.Document(
        loc=Loc(start=0, end=41, source=source),
        definitions=[
            ast.OperationDefinition(
                loc=Loc(start=0, end=40, source=source),
                operation="query",
                name=None,
                variable_definitions=None,
                directives=[],
                selection_set=ast.SelectionSet(
                    loc=Loc(start=0, end=40, source=source),
                    selections=[
                        ast.Field(
                            loc=Loc(start=4, end=38, source=source),
                            alias=None,
                            name=ast.Name(
                                loc=Loc(start=4, end=8, source=source), value="node"
                            ),
                            arguments=[
                                ast.Argument(
                                    name=ast.Name(
                                        loc=Loc(start=9, end=11, source=source),
                                        value="id",
                                    ),
                                    value=ast.IntValue(
                                        loc=Loc(start=13, end=14, source=source),
                                        value="4",
                                    ),
                                    loc=Loc(start=9, end=14, source=source),
                                )
                            ],
                            directives=[],
                            selection_set=ast.SelectionSet(
                                loc=Loc(start=16, end=38, source=source),
                                selections=[
                                    ast.Field(
                                        loc=Loc(start=22, end=24, source=source),
                                        alias=None,
                                        name=ast.Name(
                                            loc=Loc(start=22, end=24, source=source),
                                            value="id",
                                        ),
                                        arguments=[],
                                        directives=[],
                                        selection_set=None,
                                    ),
                                    ast.Field(
                                        loc=Loc(start=30, end=34, source=source),
                                        alias=None,
                                        name=ast.Name(
                                            loc=Loc(start=30, end=34, source=source),
                                            value="name",
                                        ),
                                        arguments=[],
                                        directives=[],
                                        selection_set=None,
                                    ),
                                ],
                            ),
                        )
                    ],
                ),
            )
        ],
    )