示例#1
0
def test_it_parses_variable_definition_with_default_and_directives():
    assert_node_equal(
        parse("query ($foo: Int = 42 @bar @baz) { foo }", no_location=True),
        _ast.Document(
            definitions=[
                _ast.OperationDefinition(
                    "query",
                    _ast.SelectionSet(
                        selections=[_ast.Field(name=_ast.Name(value="foo"),)],
                    ),
                    variable_definitions=[
                        _ast.VariableDefinition(
                            variable=_ast.Variable(
                                name=_ast.Name(value="foo"),
                            ),
                            type=_ast.NamedType(name=_ast.Name(value="Int"),),
                            default_value=_ast.IntValue(value="42"),
                            directives=[
                                _ast.Directive(name=_ast.Name(value="bar")),
                                _ast.Directive(name=_ast.Name(value="baz")),
                            ],
                        )
                    ],
                )
            ],
        ),
    )
示例#2
0
 def enter_field(self, field):
     if field.name.value == "foo":
         return _ast.Field(
             name=_ast.Name("Foo"),
             arguments=[
                 _ast.Argument(_ast.Name("arg"), _ast.IntValue("42"))
             ],
         )
     return field
def _test_node(argument_value=None, argument_name="foo"):
    if argument_value is None:
        arguments = []  # type: List[_ast.Argument]
    else:
        arguments = [
            _ast.Argument(
                name=_ast.Name(value=argument_name), value=argument_value
            )
        ]
    return _ast.Field(name=_ast.Name(value="test"), arguments=arguments)
示例#4
0
def test_it_creates_ast_from_nameless_query_without_variables():
    body = """query {
  node {
    id
  }
}
"""
    assert_node_equal(
        parse(body),
        _ast.Document(
            loc=(0, 30),
            definitions=[
                _ast.OperationDefinition(
                    loc=(0, 29),
                    operation="query",
                    name=None,
                    variable_definitions=[],
                    directives=[],
                    selection_set=_ast.SelectionSet(
                        loc=(6, 29),
                        selections=[
                            _ast.Field(
                                loc=(10, 27),
                                alias=None,
                                name=_ast.Name(loc=(10, 14), value="node"),
                                arguments=[],
                                directives=[],
                                selection_set=_ast.SelectionSet(
                                    loc=(15, 27),
                                    selections=[
                                        _ast.Field(
                                            loc=(21, 23),
                                            alias=None,
                                            name=_ast.Name(
                                                loc=(21, 23), value="id"
                                            ),
                                            arguments=[],
                                            directives=[],
                                            selection_set=None,
                                        )
                                    ],
                                ),
                            )
                        ],
                    ),
                )
            ],
        ),
    )
示例#5
0
def test_it_parses_inline_fragment_without_type():
    assert_node_equal(
        parse(
            """
    fragment validFragment on Pet {
        ... {
            name
        }
    }
    """
        ),
        _ast.Document(
            loc=(0, 88),
            definitions=[
                _ast.FragmentDefinition(
                    loc=(5, 83),
                    name=_ast.Name(loc=(14, 27), value="validFragment"),
                    type_condition=_ast.NamedType(
                        loc=(31, 34), name=_ast.Name(loc=(31, 34), value="Pet")
                    ),
                    variable_definitions=None,
                    selection_set=_ast.SelectionSet(
                        loc=(35, 83),
                        selections=[
                            _ast.InlineFragment(
                                loc=(45, 77),
                                selection_set=_ast.SelectionSet(
                                    loc=(49, 77),
                                    selections=[
                                        _ast.Field(
                                            loc=(63, 67),
                                            name=_ast.Name(
                                                loc=(63, 67), value="name"
                                            ),
                                        )
                                    ],
                                ),
                            )
                        ],
                    ),
                )
            ],
        ),
    )
示例#6
0
def test_parse_type_it_parses_non_null_types():
    assert_node_equal(
        parse_type("MyType!"),
        _ast.NonNullType(
            loc=(0, 7),
            type=_ast.NamedType(
                loc=(0, 6), name=_ast.Name(loc=(0, 6), value="MyType")
            ),
        ),
    )
示例#7
0
def test_parse_type_it_parses_list_types():
    assert_node_equal(
        parse_type("[MyType]"),
        _ast.ListType(
            loc=(0, 8),
            type=_ast.NamedType(
                loc=(1, 7), name=_ast.Name(loc=(1, 7), value="MyType")
            ),
        ),
    )
示例#8
0
def test_parse_type_it_parses_nested_types():
    assert_node_equal(
        parse_type("[MyType!]"),
        _ast.ListType(
            loc=(0, 9),
            type=_ast.NonNullType(
                loc=(1, 8),
                type=_ast.NamedType(
                    loc=(1, 7), name=_ast.Name(loc=(1, 7), value="MyType")
                ),
            ),
        ),
    )
示例#9
0
def test_it_parses_multi_bytes_characters():
    source = """
        # This comment has a \u0A0A multi-byte character.
        { field(arg: "Has a \u0A0A multi-byte character.") }
      """
    tree = parse(source, no_location=True)
    assert_node_equal(
        tree.definitions[0].selection_set.selections,  # type: ignore
        [
            _ast.Field(
                name=_ast.Name(value="field"),
                arguments=[
                    _ast.Argument(
                        name=_ast.Name(value="arg"),
                        value=_ast.StringValue(
                            value="Has a \u0A0A multi-byte character."
                        ),
                    )
                ],
            )
        ],
    )
def test_injected_scalar_type_extension():
    class ProtectedDirective(SchemaDirective):
        definition = "protected"

        def visit_scalar(self, scalar_type):
            return scalar_type

    schema = build_schema(
        """
        directive @protected on SCALAR

        type Query {
            foo: UUID
        }

        extend scalar UUID @protected
        """,
        additional_types=[UUID],
        schema_directives=(ProtectedDirective, ),
    )

    assert schema.to_string(include_descriptions=False) == dedent("""
        directive @protected on SCALAR

        type Query {
            foo: UUID
        }

        scalar UUID
        """)

    assert list(
        flatten(n.directives
                for n in schema.types["UUID"].nodes  # type: ignore
                )) == [
                    _ast.Directive(
                        loc=(122, 132),
                        name=_ast.Name(loc=(123, 132), value="protected"),
                        arguments=[],
                    )
                ]

    assert schema.types["UUID"] is not UUID
def test_scalar_type_extension():
    class ProtectedDirective(SchemaDirective):
        definition = "protected"

        def visit_scalar(self, scalar_type):
            return scalar_type

    schema = build_schema(
        """
        directive @protected on SCALAR

        type Query {
            foo: Foo
        }

        scalar Foo

        extend scalar Foo @protected
        """,
        schema_directives=(ProtectedDirective, ),
    )

    assert schema.to_string() == dedent("""
        directive @protected on SCALAR

        scalar Foo

        type Query {
            foo: Foo
        }
        """)

    assert list(
        flatten(n.directives
                for n in schema.types["Foo"].nodes)  # type: ignore
    ) == [
        _ast.Directive(
            loc=(140, 150),
            name=_ast.Name(loc=(141, 150), value="protected"),
            arguments=[],
        )
    ]
示例#12
0
def _type(loc, value):
    return _ast.NamedType(loc=loc, name=_ast.Name(loc=loc, value=value))
示例#13
0
def _name(loc, value):
    return _ast.Name(loc=loc, value=value)
def _var(name):
    return _ast.Variable(name=_ast.Name(value=name))
示例#15
0
def test_minimal_ast():
    assert print_ast(_ast.Field(name=_ast.Name(value="foo"))) == "foo"
示例#16
0
def test_parse_type_it_parses_custom_types():
    assert_node_equal(
        parse_type("MyType"),
        _ast.NamedType(loc=(0, 6), name=_ast.Name(loc=(0, 6), value="MyType")),
    )
示例#17
0
def test_parse_type_it_parses_well_known_types():
    assert_node_equal(
        parse_type("String"),
        _ast.NamedType(loc=(0, 6), name=_ast.Name(loc=(0, 6), value="String")),
    )
示例#18
0
    [InputField("foo", Float),
     InputField("bar", NonNullType(Enum))],
)


@pytest.mark.parametrize(
    "value, expected",
    [
        (
            {
                "foo": 3,
                "bar": "HELLO"
            },
            _ast.ObjectValue(fields=[
                _ast.ObjectField(
                    name=_ast.Name(value="foo"),
                    value=_ast.IntValue(value="3"),
                ),
                _ast.ObjectField(
                    name=_ast.Name(value="bar"),
                    value=_ast.EnumValue(value="HELLO"),
                ),
            ]),
        ),
        (
            {
                "foo": None,
                "bar": "HELLO"
            },
            _ast.ObjectValue(fields=[
                _ast.ObjectField(name=_ast.Name(value="foo"),
示例#19
0
# -*- coding: utf-8 -*-

import copy

import pytest

from py_gql.lang import ast as _ast, parse


@pytest.mark.parametrize(
    "rhs,lhs,eq",
    [
        (
            _ast.Name("foo", " ... ", (0, 1)),
            _ast.Name("foo", "  ", (0, 1)),
            True,
        ),
        (
            _ast.Name("foo", " ... ", (0, 1)),
            _ast.Name("foo", "  ", (0, 2)),
            False,
        ),
        (
            _ast.Name("bar", " ... ", (0, 1)),
            _ast.Name("foo", "  ", (0, 1)),
            False,
        ),
        (
            _ast.Field(
                name=_ast.Name(value="field"),
                arguments=[
示例#20
0
def test_it_creates_ast():
    assert_node_equal(
        parse(
            """{
  node(id: 4) {
    id,
    name
  }
}
"""
        ),
        _ast.Document(
            loc=(0, 41),
            definitions=[
                _ast.OperationDefinition(
                    loc=(0, 40),
                    operation="query",
                    name=None,
                    variable_definitions=[],
                    directives=[],
                    selection_set=_ast.SelectionSet(
                        loc=(0, 40),
                        selections=[
                            _ast.Field(
                                loc=(4, 38),
                                alias=None,
                                name=_ast.Name(loc=(4, 8), value="node"),
                                arguments=[
                                    _ast.Argument(
                                        loc=(9, 14),
                                        name=_ast.Name(loc=(9, 11), value="id"),
                                        value=_ast.IntValue(
                                            loc=(13, 14), value="4"
                                        ),
                                    )
                                ],
                                directives=[],
                                selection_set=_ast.SelectionSet(
                                    loc=(16, 38),
                                    selections=[
                                        _ast.Field(
                                            loc=(22, 24),
                                            alias=None,
                                            name=_ast.Name(
                                                loc=(22, 24), value="id"
                                            ),
                                            directives=[],
                                            arguments=[],
                                            selection_set=None,
                                        ),
                                        _ast.Field(
                                            loc=(30, 34),
                                            alias=None,
                                            name=_ast.Name(
                                                loc=(30, 34), value="name"
                                            ),
                                            directives=[],
                                            arguments=[],
                                            selection_set=None,
                                        ),
                                    ],
                                ),
                            )
                        ],
                    ),
                )
            ],
        ),
    )