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, ) ], ), ) ], ), ) ], ), )
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")), ], ) ], ) ], ), )
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)
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" ), ) ], ), ) ], ), ) ], ), )
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_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, ), ], ), ) ], ), ) ], ), )
def test_minimal_ast(): assert print_ast(_ast.Field(name=_ast.Name(value="foo"))) == "foo"
( _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=[ _ast.Argument( name=_ast.Name(value="arg"), value=_ast.StringValue( value="Has a \u0A0A multi-byte character."), ) ], ), _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."), ) ], ), True,