Example #1
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)
            ]))
    ])
Example #2
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,
                )
            ]),
        )
    ])
Example #3
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))))
    ])
Example #4
0
def query(*fields):
    return ast.Document(
        definitions=[ast.OperationDefinition(
            operation='query',
            selection_set=ast.SelectionSet(
                selections=list(selections(*fields))
            )
        )]
    )
Example #5
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))),
        )
    ])
Example #6
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)]))]))])
Example #7
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')
Example #8
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'),
                                            ),
                                        ]
                                    ),
                                ),
                            ],
                        ),
                    ],
                ),
            ),
        ],
    )
Example #9
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,
                                    ),
                                ],
                            ),
                        )
                    ],
                ),
            )
        ],
    )