def test_resolver_factory__resolver_executor_update_prop_cant_be_null( _resolver_executor_mock): from tartiflette.types.field import GraphQLField from tartiflette.types.non_null import GraphQLNonNull _resolver_executor_mock._schema_field = GraphQLField("A", gql_type="F") assert not _resolver_executor_mock.cant_be_null _resolver_executor_mock._schema_field = GraphQLField( "A", gql_type=GraphQLNonNull(gql_type="F")) assert _resolver_executor_mock.cant_be_null
def field_definition(self, tree: Tree) -> GraphQLField: # TODO: Add directives description = None name = None arguments = None gql_type = None for child in tree.children: if child.type == 'description': description = child.value elif child.type == 'IDENT': name = String(str(child.value), ast_node=child) elif child.type == 'type': gql_type = child.value elif child.type == 'arguments': arguments = child.value elif child.type == 'discard': pass else: raise UnexpectedASTNode( "Unexpected AST node `{}`, type `{}`".format( child, child.__class__.__name__)) return GraphQLField( name=name, gql_type=gql_type, arguments=arguments, description=description )
def field_definition(self, tree: Tree) -> GraphQLField: description = None name = None arguments = None gql_type = None directives = None for child in tree.children: if child.type == "description": description = child.value elif child.type == "IDENT": name = child.value elif child.type == "type": gql_type = child.value elif child.type == "arguments": arguments = child.value elif child.type == "directives": directives = child.value elif child.type == "discard": pass else: raise UnexpectedASTNode( "Unexpected AST node `{}`, type `{}`".format( child, child.__class__.__name__)) return GraphQLField( name=name, gql_type=gql_type, arguments=arguments, description=description, directives=directives, schema=self._schema, )
def test_graphql_interface_init(mocked_resolver_factory): interface = GraphQLInterfaceType( name="Name", fields=OrderedDict([ ("test", GraphQLField(name="arg", gql_type="Int")), ("another", GraphQLField(name="arg", gql_type="String")), ]), description="description", ) assert interface.name == "Name" assert interface.find_field("test") == GraphQLField(name="arg", gql_type="Int") assert interface.find_field("another") == GraphQLField(name="arg", gql_type="String") assert interface.description == "description"
def test_graphql_interface_eq(mocked_resolver_factory): interface = GraphQLInterfaceType( name="Name", fields=OrderedDict([ ("test", GraphQLField(name="arg", gql_type="Int")), ("another", GraphQLField(name="arg", gql_type="String")), ]), description="description", ) ## Same assert interface == interface assert interface == GraphQLInterfaceType( name="Name", fields=OrderedDict([ ("test", GraphQLField(name="arg", gql_type="Int")), ("another", GraphQLField(name="arg", gql_type="String")), ]), description="description", ) # Currently we ignore the description in comparing assert interface == GraphQLInterfaceType( name="Name", fields=OrderedDict([ ("test", GraphQLField(name="arg", gql_type="Int")), ("another", GraphQLField(name="arg", gql_type="String")), ]), ) ## Different assert interface != GraphQLInterfaceType( name="Name", fields=OrderedDict([ ("another", GraphQLField(name="arg", gql_type="String")), ("test", GraphQLField(name="arg", gql_type="Int")), # We reversed the order of arguments ]), ) assert interface != GraphQLInterfaceType(name="Name", fields=OrderedDict()) assert interface != GraphQLInterfaceType( name="OtherName", fields=OrderedDict([ ("another", GraphQLField(name="arg", gql_type="String")), ("test", GraphQLField(name="arg", gql_type="Int")), # We reversed the order of arguments ]), )
def test_graphql_object_init(mocked_resolver_factory): obj = GraphQLObjectType( name="Name", fields=OrderedDict([ ("test", GraphQLField(name="arg", gql_type="Int")), ("another", GraphQLField(name="arg", gql_type="String")), ]), interfaces=["First", "Second"], description="description", ) assert obj.name == "Name" assert obj.find_field("test") == GraphQLField(name="arg", gql_type="Int") assert obj.find_field("another") == GraphQLField(name="arg", gql_type="String") assert obj.interfaces_names == ["First", "Second"] assert obj.description == "description"
def test_graphql_interface_repr(): interface = GraphQLInterfaceType(name="Name", fields=OrderedDict([ ("test", GraphQLField(name="arg", gql_type="Int")), ("another", GraphQLField(name="arg", gql_type="String")), ]), description="description") assert interface.__repr__() == "GraphQLInterfaceType(name='Name', " \ "fields=OrderedDict([" \ "('test', GraphQLField(name='arg', gql_type='Int', arguments=OrderedDict(), resolver=None, description=None)), " \ "('another', GraphQLField(name='arg', gql_type='String', arguments=OrderedDict(), resolver=None, description=None))" \ "]), description='description')" assert interface == eval(repr(interface))
def test_graphql_interface_init(): interface = GraphQLInterfaceType(name="Name", fields=OrderedDict([ ("test", GraphQLField(name="arg", gql_type="Int")), ("another", GraphQLField(name="arg", gql_type="String")), ]), description="description") assert interface.name == "Name" assert interface.fields == OrderedDict([ ("test", GraphQLField(name="arg", gql_type="Int")), ("another", GraphQLField(name="arg", gql_type="String")), ]) assert interface.description == "description"
def test_graphql_field_init(): field = GraphQLField( name="Name", gql_type="Test", arguments=OrderedDict([("test", 42), ("another", 24)]), description="description", ) assert field.name == "Name" assert field.gql_type == "Test" assert field.arguments == OrderedDict([("test", 42), ("another", 24)]) assert field.resolver._func is default_resolver assert field.description == "description"
def test_graphql_object_repr(): obj = GraphQLObjectType(name="Name", fields=OrderedDict([ ("test", GraphQLField(name="arg", gql_type="Int")), ("another", GraphQLField(name="arg", gql_type="String")), ]), interfaces=["First", "Second"], description="description") assert obj.__repr__() == "GraphQLObjectType(" \ "name='Name', " \ "fields=OrderedDict([" \ "('test', GraphQLField(name='arg', " \ "gql_type='Int', arguments=OrderedDict(), " \ "resolver=None, description=None)), " \ "('another', GraphQLField(name='arg', " \ "gql_type='String', arguments=OrderedDict(), " \ "resolver=None, description=None))]), " \ "interfaces=['First', 'Second'], " \ "description='description')" assert obj == eval(repr(obj))
def prepare_type_root_field(schema: "GraphQLSchema") -> "GraphQLField": return GraphQLField( name="__type", description="Request the type information of a single type.", arguments={ "name": GraphQLArgument( name="name", gql_type=GraphQLNonNull("String", schema=schema), schema=schema, ) }, gql_type="__Type", resolver=__type_resolver, schema=schema, )
def prepare_type_root_field(schema: "GraphQLSchema") -> "GraphQLField": """ Factory to generate a `__type` field. :param schema: the GraphQLSchema instance linked to the engine :type schema: GraphQLSchema :return: the `__type` field :rtype: GraphQLField """ return GraphQLField( name="__type", description="Request the type information of a single type.", arguments={ "name": GraphQLArgument(name="name", gql_type=GraphQLNonNull("String", schema=schema)) }, gql_type="__Type", resolver=__type_resolver, )
def test_graphql_argument_eq(mocked_resolver_factory): field = GraphQLField( name="Name", gql_type="Test", arguments=OrderedDict([("test", 42), ("another", 24)]), description="description", ) ## Same assert field == field assert field == GraphQLField( name="Name", gql_type="Test", arguments=OrderedDict([("test", 42), ("another", 24)]), description="description", ) # Currently we ignore the description in comparing assert field == GraphQLField( name="Name", gql_type="Test", arguments=OrderedDict([("test", 42), ("another", 24)]), ) ## Different assert field != GraphQLField( name="Name", gql_type="Test", arguments=OrderedDict([ ("another", 24), ("test", 42), # We reversed the order of arguments ]), ) assert field != GraphQLField( name="Name", gql_type="Test", arguments=OrderedDict()) assert field != GraphQLField( name="Name", gql_type="NotTest", arguments=OrderedDict([("test", 42), ("another", 24)]), ) assert field != GraphQLField( name="OtherName", gql_type="Test", arguments=OrderedDict([("test", 42), ("another", 24)]), )
def parse_field_definition( field_definition_node: "FieldDefinitionNode", schema: "GraphQLSchema") -> Optional["GraphQLField"]: """ Computes an AST field definition node into a GraphQLField instance. :param field_definition_node: AST field definition node to treat :param schema: the GraphQLSchema instance linked to the engine :type field_definition_node: FieldDefinitionNode :type schema: GraphQLSchema :return: the GraphQLField instance :rtype: Optional[GraphQLField] """ if not field_definition_node: return None return GraphQLField( name=parse_name(field_definition_node.name, schema), description=parse_name(field_definition_node.description, schema), gql_type=parse_type(field_definition_node.type, schema), arguments=parse_arguments_definition(field_definition_node.arguments, schema), directives=field_definition_node.directives, )
async def test_build_schema(monkeypatch, clean_registry): from tartiflette.resolver.factory import ResolverExecutorFactory from tartiflette.engine import _import_builtins resolver_excutor = Mock() monkeypatch.setattr( ResolverExecutorFactory, "get_resolver_executor", Mock(return_value=resolver_excutor), ) from tartiflette.schema.bakery import SchemaBakery from tartiflette.types.argument import GraphQLArgument from tartiflette.types.field import GraphQLField from tartiflette.types.input_object import GraphQLInputObjectType from tartiflette.types.interface import GraphQLInterfaceType from tartiflette.types.list import GraphQLList from tartiflette.types.non_null import GraphQLNonNull from tartiflette.types.object import GraphQLObjectType from tartiflette.types.union import GraphQLUnionType schema_sdl = """ schema @enable_cache { query: RootQuery mutation: RootMutation subscription: RootSubscription } union Group = Foo | Bar | Baz interface Something { oneField: [Int] anotherField: [String] aLastOne: [[Date!]]! } input UserInfo { name: String dateOfBirth: [Date] graphQLFan: Boolean! } # directive @partner(goo: Anything) on ENUM_VALUE \"\"\" This is a docstring for the Test Object Type. \"\"\" type Test implements Unknown & Empty{ \"\"\" This is a field description :D \"\"\" field(input: InputObject): String! anotherField: [Int] fieldWithDefaultValueArg(test: String = "default"): ID simpleField: Date } """ _, schema_sdl = await _import_builtins([], schema_sdl, "G") _, schema_E = await _import_builtins([], "", "E") clean_registry.register_sdl("G", schema_sdl) clean_registry.register_sdl("E", schema_E) generated_schema = SchemaBakery._preheat("G") expected_schema = SchemaBakery._preheat("E") expected_schema.query_type = "RootQuery" expected_schema.mutation_type = "RootMutation" expected_schema.subscription_type = "RootSubscription" expected_schema.add_definition( GraphQLUnionType(name="Group", gql_types=["Foo", "Bar", "Baz"])) expected_schema.add_definition( GraphQLInterfaceType( name="Something", fields=OrderedDict( oneField=GraphQLField(name="oneField", gql_type=GraphQLList(gql_type="Int")), anotherField=GraphQLField( name="anotherField", gql_type=GraphQLList(gql_type="String"), ), aLastOne=GraphQLField( name="aLastOne", gql_type=GraphQLNonNull(gql_type=GraphQLList( gql_type=GraphQLList(gql_type=GraphQLNonNull( gql_type="Date")))), ), ), )) expected_schema.add_definition( GraphQLInputObjectType( name="UserInfo", fields=OrderedDict([ ("name", GraphQLArgument(name="name", gql_type="String")), ( "dateOfBirth", GraphQLArgument( name="dateOfBirth", gql_type=GraphQLList(gql_type="Date"), ), ), ( "graphQLFan", GraphQLArgument( name="graphQLFan", gql_type=GraphQLNonNull(gql_type="Boolean"), ), ), ]), )) expected_schema.add_definition( GraphQLObjectType( name="Test", fields=OrderedDict([ ( "field", GraphQLField( name="field", gql_type=GraphQLNonNull(gql_type="String"), arguments=OrderedDict(input=GraphQLArgument( name="input", gql_type="InputObject")), ), ), ( "anotherField", GraphQLField( name="anotherField", gql_type=GraphQLList(gql_type="Int"), ), ), ( "fieldWithDefaultValueArg", GraphQLField( name="fieldWithDefaultValueArg", gql_type="ID", arguments=OrderedDict(test=GraphQLArgument( name="test", gql_type="String", default_value="default", )), ), ), ( "simpleField", GraphQLField(name="simpleField", gql_type="Date"), ), ]), interfaces=["Unknown", "Empty"], )) assert 5 < len(generated_schema._gql_types) assert len(expected_schema._gql_types) == len(generated_schema._gql_types) monkeypatch.undo()
def test_build_schema(): schema_sdl = """ schema @enable_cache { query: RootQuery mutation: RootMutation subscription: RootSubscription } scalar Date union Group = Foo | Bar | Baz interface Something { oneField: [Int] anotherField: [String] aLastOne: [[Date!]]! } input UserInfo { name: String dateOfBirth: [Date] graphQLFan: Boolean! } # directive @partner(goo: Anything) on ENUM_VALUE \"\"\" This is a docstring for the Test Object Type. \"\"\" type Test implements Unknown & Empty @enable_cache { \"\"\" This is a field description :D \"\"\" field(input: InputObject): String! @deprecated(reason: "Useless field") anotherField: [Int] @something( lst: ["about" "stuff"] obj: {some: [4, 8, 16], complex: {about: 19.4}, another: EnumVal} ) fieldWithDefaultValueArg(test: String = "default"): ID simpleField: Date } """ generated_schema = build_graphql_schema_from_sdl(schema_sdl, schema=GraphQLSchema()) expected_schema = GraphQLSchema() expected_schema.query_type = "RootQuery" expected_schema.mutation_type = "RootMutation" expected_schema.subscription_type = "RootSubscription" expected_schema.add_definition(GraphQLScalarType(name="Date")) expected_schema.add_definition(GraphQLUnionType(name="Group", gql_types=[ "Foo", "Bar", "Baz", ])) expected_schema.add_definition(GraphQLInterfaceType(name="Something", fields=OrderedDict( oneField=GraphQLField(name="oneField", gql_type=GraphQLList(gql_type="Int")), anotherField=GraphQLField(name="anotherField", gql_type=GraphQLList(gql_type="String")), aLastOne=GraphQLField(name="aLastOne", gql_type=GraphQLNonNull(gql_type=GraphQLList(gql_type=GraphQLList(gql_type=GraphQLNonNull(gql_type="Date"))))), ))) expected_schema.add_definition(GraphQLInputObjectType(name="UserInfo", fields=OrderedDict([ ('name', GraphQLArgument(name="name", gql_type="String")), ('dateOfBirth', GraphQLArgument(name="dateOfBirth", gql_type=GraphQLList(gql_type="Date"))), ('graphQLFan', GraphQLArgument(name="graphQLFan", gql_type=GraphQLNonNull(gql_type="Boolean"))), ]))) expected_schema.add_definition(GraphQLObjectType(name="Test", fields=OrderedDict([ ('field', GraphQLField(name="field", gql_type=GraphQLNonNull(gql_type="String"), arguments=OrderedDict( input=GraphQLArgument(name="input", gql_type="InputObject"), ))), ('anotherField', GraphQLField(name="anotherField", gql_type=GraphQLList(gql_type="Int"))), ('fieldWithDefaultValueArg', GraphQLField(name="fieldWithDefaultValueArg", gql_type="ID", arguments=OrderedDict( test=GraphQLArgument(name="test", gql_type="String", default_value="default"), ))), ('simpleField', GraphQLField(name="simpleField", gql_type="Date")), ]), interfaces=[ "Unknown", "Empty", ])) assert expected_schema == generated_schema assert len(generated_schema._gql_types) > 5 assert len(generated_schema._gql_types) == len(expected_schema._gql_types) # Only the default types should be in the schema assert len(DefaultGraphQLSchema._gql_types) == 5
), ( """ \"\"\" Vehicle interface for any type of entity-moving apparatus \"\"\" interface Vehicle { speedInKmh: Float } """, [ Tree('type_definition', [ GraphQLInterfaceType(name="Vehicle", fields=OrderedDict([ ("speedInKmh", GraphQLField(name="speedInKmh", gql_type="Float")), ])), ]) ], ), ( """ input UserProfileInput { fullName: String } """, [ Tree('type_definition', [ GraphQLInputObjectType(name="UserProfileInput", fields=OrderedDict([ ("fullName",
def test_graphql_object_eq(): obj = GraphQLObjectType(name="Name", fields=OrderedDict([ ("test", GraphQLField(name="arg", gql_type="Int")), ("another", GraphQLField(name="arg", gql_type="String")), ]), interfaces=["First", "Second"], description="description") ## Same assert obj == obj assert obj == GraphQLObjectType(name="Name", fields=OrderedDict([ ("test", GraphQLField(name="arg", gql_type="Int")), ("another", GraphQLField(name="arg", gql_type="String")), ]), interfaces=["First", "Second"], description="description") # Currently we ignore the description in comparing assert obj == GraphQLObjectType(name="Name", fields=OrderedDict([ ("test", GraphQLField(name="arg", gql_type="Int")), ("another", GraphQLField(name="arg", gql_type="String")), ]), interfaces=["First", "Second"]) ## Different assert obj != GraphQLObjectType( name="Name", fields=OrderedDict([ ("another", GraphQLField(name="arg", gql_type="String")), ("test", GraphQLField(name="arg", gql_type="Int")), # We reversed the fields ]), interfaces=["First", "Second"]) assert obj != GraphQLObjectType( name="Name", fields=OrderedDict([ ("test", GraphQLField(name="arg", gql_type="Int")), ("another", GraphQLField(name="arg", gql_type="String")), ]), interfaces=[ "Second" "First", # We reversed the interface fields ], description="description") assert obj != GraphQLObjectType( name="Name", fields=OrderedDict(), interfaces=["First", "Second"]) assert obj != GraphQLObjectType( name="Name", fields=OrderedDict([ ("test", GraphQLField(name="arg", gql_type="Int")), ("another", GraphQLField(name="arg", gql_type="String")), ]), interfaces=[]) assert obj != GraphQLObjectType( name="OtherName", fields=OrderedDict([ ("test", GraphQLField(name="arg", gql_type="Int")), ("another", GraphQLField(name="arg", gql_type="String")), ]), interfaces=["First", "Second"], description="description")