def test_it_parses_type_with_description_string(): assert_node_equal( parse( """ "Description" type Hello { world: String }""", allow_type_system=True, ), _ast.Document( loc=(0, 45), definitions=[ _ast.ObjectTypeDefinition( loc=(1, 45), name=_name((20, 25), "Hello"), interfaces=[], directives=[], description=_ast.StringValue(loc=(1, 14), value="Description"), fields=[ _field( (30, 43), _name((30, 35), "world"), _type((37, 43), "String"), ) ], ) ], ), )
def test_it_parses_type_with_description_multi_line_string(): body = ''' """ Description """ # Even with comments between them type Hello { world: String }''' assert_node_equal( parse(body, allow_type_system=True), _ast.Document( loc=(0, 85), definitions=[ _ast.ObjectTypeDefinition( loc=(1, 85), name=_name((60, 65), "Hello"), interfaces=[], directives=[], description=_ast.StringValue(loc=(1, 20), value="Description", block=True), fields=[ _field( (70, 83), _name((70, 75), "world"), _type((77, 83), "String"), ) ], ) ], ), )
def test_it_parses_simple_type(): body = """ type Hello { world: String }""" assert_node_equal( parse(body, allow_type_system=True), _ast.Document( loc=(0, 31), definitions=[ _ast.ObjectTypeDefinition( loc=(1, 31), name=_name((6, 11), "Hello"), interfaces=[], directives=[], fields=[ _field( (16, 29), _name((16, 21), "world"), _type((23, 29), "String"), ) ], ) ], ), )
def test_it_parses_simple_type_inheriting_multiple_interfaces_with_leading_ampersand( ): # noqa: E501 body = "type Hello implements & Wo & rld { field: String }" assert_node_equal( parse(body, allow_type_system=True), _ast.Document( loc=(0, 50), definitions=[ _ast.ObjectTypeDefinition( loc=(0, 50), name=_name((5, 10), "Hello"), interfaces=[_type((24, 26), "Wo"), _type((29, 32), "rld")], directives=[], fields=[ _field( (35, 48), _name((35, 40), "field"), _type((42, 48), "String"), ) ], ) ], ), )
def test_it_parses_extension_without_fields_followed_by_extension(): assert_node_equal( parse( """ extend type Hello implements Greeting extend type Hello implements SecondGreeting """, allow_type_system=True, ), _ast.Document( loc=(0, 100), definitions=[ _ast.ObjectTypeExtension( loc=(7, 44), name=_name((19, 24), "Hello"), interfaces=[_type((36, 44), "Greeting")], fields=[], directives=[], ), _ast.ObjectTypeExtension( loc=(52, 95), name=_name((64, 69), "Hello"), interfaces=[_type((81, 95), "SecondGreeting")], fields=[], directives=[], ), ], ), )
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 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_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_simple_type_inheriting_interface(): body = "type Hello implements World { field: String }" assert_node_equal( parse(body, allow_type_system=True), _ast.Document( loc=(0, 45), definitions=[ _ast.ObjectTypeDefinition( loc=(0, 45), name=_name((5, 10), "Hello"), interfaces=[_type((22, 27), "World")], directives=[], fields=[ _field( (30, 43), _name((30, 35), "field"), _type((37, 43), "String"), ) ], ) ], ), )
def _doc(loc, defs): return _ast.Document(loc=loc, definitions=defs)
def test_noop_without_extension_nodes(): # NOTE: The parser cannot technically build such a document new_schema = extend_schema(BASE_SCHEMA, _ast.Document(definitions=[])) assert new_schema is BASE_SCHEMA
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, ), ], ), ) ], ), ) ], ), )