def test_prints_interface(): FooType = GraphQLInterfaceType( name='Foo', resolve_type=lambda *_: None, fields={ 'str': GraphQLField(GraphQLString) } ) BarType = GraphQLObjectType( name='Bar', fields={ 'str': GraphQLField(GraphQLString), }, interfaces=[FooType] ) Root = GraphQLObjectType( name='Root', fields={ 'bar': GraphQLField(BarType) } ) Schema = GraphQLSchema(Root) output = print_for_test(Schema) assert output == '''
def test_builds_a_schema_with_an_interface(): FriendlyType = GraphQLInterfaceType( name='Friendly', resolve_type=lambda: None, fields=lambda: { 'bestFriend': GraphQLField(FriendlyType, description='The best friend of this friendly thing.') } ) GraphQLObjectType( name='Human', interfaces=[FriendlyType], fields=lambda: { 'bestFriend': GraphQLField(FriendlyType) } ) schema = GraphQLSchema( query=GraphQLObjectType( name='WithInterface', fields={ 'friendly': GraphQLField(FriendlyType) } ) ) _test_schema(schema)
def test_gets_execution_info_in_resolver(): encountered_schema = [None] encountered_root_value = [None] def resolve_type(obj, info): encountered_schema[0] = info.schema encountered_root_value[0] = info.root_value return PersonType2 NamedType2 = GraphQLInterfaceType( name='Named', fields={'name': GraphQLField(GraphQLString)}, resolve_type=resolve_type) PersonType2 = GraphQLObjectType(name='Person', interfaces=[NamedType2], fields={ 'name': GraphQLField(GraphQLString), 'friends': GraphQLField(GraphQLList(NamedType2)) }) schema2 = GraphQLSchema(query=PersonType2) john2 = Person('John', [], [liz]) ast = parse('''{ name, friends { name } }''') result = execute(schema2, john2, ast) assert result.data == {'name': 'John', 'friends': [{'name': 'Liz'}]} assert encountered_schema[0] == schema2 assert encountered_root_value[0] == john2
def test_includes_interfaces_thunk_subtypes_in_the_type_map(): SomeInterface = GraphQLInterfaceType( name='SomeInterface', fields={ 'f': GraphQLField(GraphQLInt) } ) SomeSubtype = GraphQLObjectType( name='SomeSubtype', fields={ 'f': GraphQLField(GraphQLInt) }, interfaces=lambda: [SomeInterface], is_type_of=lambda: True ) schema = GraphQLSchema(query=GraphQLObjectType( name='Query', fields={ 'iface': GraphQLField(SomeInterface) } )) assert schema.get_type_map()['SomeSubtype'] is SomeSubtype
def test_includes_interfaces_subtypes_in_the_type_map(): SomeInterface = GraphQLInterfaceType('SomeInterface') SomeSubtype = GraphQLObjectType(name='SomeSubtype', fields={}, interfaces=[SomeInterface]) schema = GraphQLSchema(SomeInterface) assert schema.get_type_map()['SomeSubtype'] == SomeSubtype
def test_prints_multiple_interfaces(): FooType = GraphQLInterfaceType(name='Foo', resolve_type=lambda *_: None, fields={'str': GraphQLField(GraphQLString)}) BaazType = GraphQLInterfaceType(name='Baaz', resolve_type=lambda *_: None, fields={'int': GraphQLField(GraphQLInt)}) BarType = GraphQLObjectType(name='Bar', fields=OrderedDict([ ('str', GraphQLField(GraphQLString)), ('int', GraphQLField(GraphQLInt)) ]), interfaces=[FooType, BaazType]) Root = GraphQLObjectType(name='Root', fields={'bar': GraphQLField(BarType)}) Schema = GraphQLSchema(Root) output = print_for_test(Schema) assert output == '''
def resolve_node_type(get_node_type): ''' Constructs the node interface. ''' return GraphQLInterfaceType('Node', description='An object with an ID', fields=lambda: { 'id': GraphQLField( GraphQLNonNull(GraphQLID), description='The id of the object.', ), }, resolve_type=get_node_type)
def internal_type(cls, schema): if cls._meta.is_interface: return GraphQLInterfaceType( cls._meta.type_name, description=cls._meta.description, resolve_type=partial(cls._resolve_type, schema), fields=partial(cls.get_fields, schema) ) elif cls._meta.is_union: return GraphQLUnionType( cls._meta.type_name, types=cls._meta.types, description=cls._meta.description, ) return GraphQLObjectType( cls._meta.type_name, description=cls._meta.description, interfaces=[schema.T(i) for i in cls._meta.interfaces], fields=partial(cls.get_fields, schema), is_type_of=getattr(cls, 'is_type_of', None) )
class Cat(object): def __init__(self, name, meows): self.name = name self.meows = meows class Person(object): def __init__(self, name, pets, friends): self.name = name self.pets = pets self.friends = friends NamedType = GraphQLInterfaceType('Named', {'name': GraphQLField(GraphQLString)}) DogType = GraphQLObjectType(name='Dog', interfaces=[NamedType], fields={ 'name': GraphQLField(GraphQLString), 'barks': GraphQLField(GraphQLBoolean), }, is_type_of=lambda value: isinstance(value, Dog)) CatType = GraphQLObjectType(name='Cat', interfaces=[NamedType], fields={ 'name': GraphQLField(GraphQLString), 'meows': GraphQLField(GraphQLBoolean), },
BlogQuery = GraphQLObjectType( 'Query', { 'article': GraphQLField(BlogArticle, args={ 'id': GraphQLArgument(GraphQLString), }), 'feed': GraphQLField(GraphQLList(BlogArticle)) }) BlogMutation = GraphQLObjectType('Mutation', {'writeArticle': GraphQLField(BlogArticle)}) ObjectType = GraphQLObjectType('Object', {}) InterfaceType = GraphQLInterfaceType('Interface') UnionType = GraphQLUnionType('Union', [ObjectType]) EnumType = GraphQLEnumType('Enum', {}) InputObjectType = GraphQLInputObjectType('InputObject', {}) def test_defines_a_query_only_schema(): BlogSchema = GraphQLSchema(BlogQuery) assert BlogSchema.get_query_type() == BlogQuery article_field = BlogQuery.get_fields()['article'] assert article_field.type == BlogArticle assert article_field.type.name == 'Article' assert article_field.name == 'article'
from graphql.core.language.parser import parse from graphql.core.type import (GraphQLArgument, GraphQLBoolean, GraphQLEnumType, GraphQLEnumValue, GraphQLField, GraphQLFloat, GraphQLID, GraphQLInputObjectField, GraphQLInputObjectType, GraphQLInt, GraphQLInterfaceType, GraphQLList, GraphQLNonNull, GraphQLObjectType, GraphQLSchema, GraphQLString, GraphQLUnionType) from graphql.core.type.directives import (GraphQLDirective, GraphQLIncludeDirective, GraphQLSkipDirective) from graphql.core.validation import validate Being = GraphQLInterfaceType('Being', { 'name': GraphQLField(GraphQLString, { 'surname': GraphQLArgument(GraphQLBoolean), }) }) Pet = GraphQLInterfaceType('Pet', { 'name': GraphQLField(GraphQLString, { 'surname': GraphQLArgument(GraphQLBoolean), }), }) DogCommand = GraphQLEnumType('DogCommand', { 'SIT': GraphQLEnumValue(0), 'HEEL': GraphQLEnumValue(1), 'DOWN': GraphQLEnumValue(2), })
6, description='Released in 1983.', ) }) characterInterface = GraphQLInterfaceType( 'Character', description='A character in the Star Wars Trilogy', fields=lambda: { 'id': GraphQLField(GraphQLNonNull(GraphQLString), description='The id of the character.'), 'name': GraphQLField(GraphQLString, description='The name of the character.'), 'friends': GraphQLField( GraphQLList(characterInterface), description= 'The friends of the character, or an empty list if they have none.' ), 'appearsIn': GraphQLField(GraphQLList(episodeEnum), description='Which movies they appear in.'), }, resolve_type=lambda character, *_: humanType if starwars_fixtures.getHuman(character.id) else droidType, ) humanType = GraphQLObjectType( 'Human', description='A humanoid creature in the Star Wars universe.', fields=lambda: {
from pytest import raises from graphql.core import parse from graphql.core.execution import execute from graphql.core.type import (GraphQLArgument, GraphQLField, GraphQLID, GraphQLInterfaceType, GraphQLList, GraphQLNonNull, GraphQLObjectType, GraphQLSchema, GraphQLString, GraphQLUnionType) from graphql.core.utils.extend_schema import extend_schema from graphql.core.utils.schema_printer import print_schema # Test schema. SomeInterfaceType = GraphQLInterfaceType( name='SomeInterface', resolve_type=lambda: FooType, fields=lambda: OrderedDict([ ('name', GraphQLField(GraphQLString)), ('some', GraphQLField(SomeInterfaceType)), ])) FooType = GraphQLObjectType( name='Foo', interfaces=[SomeInterfaceType], fields=lambda: OrderedDict([ ('name', GraphQLField(GraphQLString)), ('some', GraphQLField(SomeInterfaceType)), ('tree', GraphQLField(GraphQLNonNull(GraphQLList(FooType)))), ])) BarType = GraphQLObjectType(name='Bar', interfaces=[SomeInterfaceType],
GraphQLNonNull, GraphQLString, GraphQLInt, GraphQLFloat, GraphQLBoolean, GraphQLInterfaceType, GraphQLEnumType, GraphQLEnumValue, GraphQLInputObjectType, GraphQLUnionType, GraphQLList) from graphql.core.error import format_error Pet = GraphQLInterfaceType('Pet', { 'name': GraphQLField(GraphQLString, { 'surname': GraphQLArgument(GraphQLBoolean), }), }) DogCommand = GraphQLEnumType('DogCommand', { 'SIT': GraphQLEnumValue(0), 'HEEL': GraphQLEnumValue(1), 'DOWN': GraphQLEnumValue(2), }) Dog = GraphQLObjectType('Dog', { 'name': GraphQLField(GraphQLString, { 'surname': GraphQLArgument(GraphQLBoolean), }), 'nickname': GraphQLField(GraphQLString), 'barks': GraphQLField(GraphQLBoolean),