def test_includes_nested_input_objects_in_the_map(): NestedInputObject = GraphQLInputObjectType( name='NestedInputObject', fields={'value': GraphQLInputObjectField(GraphQLString)} ) SomeInputObject = GraphQLInputObjectType( name='SomeInputObject', fields={'nested': GraphQLInputObjectField(NestedInputObject)} ) SomeMutation = GraphQLObjectType( name='SomeMutation', fields={ 'mutateSomething': GraphQLField( type=BlogArticle, args={ 'input': GraphQLArgument(SomeInputObject) } ) } ) schema = GraphQLSchema( query=BlogQuery, mutation=SomeMutation ) assert schema.get_type_map()['NestedInputObject'] is NestedInputObject
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_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' article_field_type = article_field.type assert isinstance(article_field_type, GraphQLObjectType) title_field = article_field_type.get_fields()['title'] assert title_field.name == 'title' assert title_field.type == GraphQLString assert title_field.type.name == 'String' author_field = article_field_type.get_fields()['author'] author_field_type = author_field.type assert isinstance(author_field_type, GraphQLObjectType) recent_article_field = author_field_type.get_fields()['recentArticle'] assert recent_article_field.type == BlogArticle feed_field = BlogQuery.get_fields()['feed'] assert feed_field.type.of_type == BlogArticle assert feed_field.name == 'feed'
def test_defines_a_mutation_schema(): BlogSchema = GraphQLSchema(BlogQuery, BlogMutation) assert BlogSchema.get_mutation_type() == BlogMutation write_mutation = BlogMutation.get_fields()['writeArticle'] assert write_mutation.type == BlogArticle assert write_mutation.type.name == 'Article' assert write_mutation.name == 'writeArticle'
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_defines_a_subscription_schema(): BlogSchema = GraphQLSchema( query=BlogQuery, subscription=BlogSubscription ) assert BlogSchema.get_subscription_type() == BlogSubscription subscription = BlogSubscription.get_fields()['articleSubscribe'] assert subscription.type == BlogArticle assert subscription.type.name == 'Article' assert subscription.name == 'articleSubscribe'
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 test_builds_a_schema_with_a_circular_type_reference(): DogType = GraphQLObjectType( name='Dog', fields=lambda: { 'bestFriend': GraphQLField(HumanType) } ) HumanType = GraphQLObjectType( name='Human', fields=lambda: { 'bestFriend': GraphQLField(DogType) } ) schema = GraphQLSchema(query=GraphQLObjectType( name='Circular', fields=OrderedDict([ ('dog', GraphQLField(DogType)), ('human', GraphQLField(HumanType)), ]) )) _test_schema(schema)
def test_nulls_out_error_subtrees(): doc = '''{ ok, error }''' class Data(object): def ok(self): return 'ok' def error(self): raise Exception('Error getting error') doc_ast = parse(doc) Type = GraphQLObjectType('Type', { 'ok': GraphQLField(GraphQLString), 'error': GraphQLField(GraphQLString), }) result = execute(GraphQLSchema(Type), Data(), doc_ast) assert result.data == {'ok': 'ok', 'error': None} assert len(result.errors) == 1 assert result.errors[0].message == 'Error getting error'
def test_builds_a_schema_with_field_arguments_with_default_values(): GeoType = GraphQLInputObjectType( name='Geo', fields=OrderedDict([ ('lat', GraphQLInputObjectField(GraphQLFloat)), ('lon', GraphQLInputObjectField(GraphQLFloat)), ])) schema = GraphQLSchema(query=GraphQLObjectType( name='ArgFields', fields=OrderedDict( [('defaultInt', GraphQLField(GraphQLString, args={ 'intArg': GraphQLArgument(GraphQLInt, default_value=10) })), ('defaultList', GraphQLField(GraphQLString, args={ 'listArg': GraphQLArgument(GraphQLList(GraphQLInt), default_value=[1, 2, 3]) })), ('defaultObject', GraphQLField(GraphQLString, args={ 'objArg': GraphQLArgument(GeoType, default_value={ 'lat': 37.485, 'lon': -122.148 }) }))]))) _test_schema(schema)
async def test_asyncio_py35_executor(): doc = 'query Example { a, b, c }' async def resolver(context, *_): await asyncio.sleep(0.001) return 'hey' async def resolver_2(context, *_): await asyncio.sleep(0.003) return 'hey2' def resolver_3(context, *_): return 'hey3' Type = GraphQLObjectType('Type', { 'a': GraphQLField(GraphQLString, resolver=resolver), 'b': GraphQLField(GraphQLString, resolver=resolver_2), 'c': GraphQLField(GraphQLString, resolver=resolver_3) }) executor = Executor([AsyncioExecutionMiddleware()]) result = await executor.execute(GraphQLSchema(Type), doc) assert not result.errors assert result.data == {'a': 'hey', 'b': 'hey2', 'c': 'hey3'}
def test_identifies_deprecated_fields(): TestType = GraphQLObjectType( 'TestType', OrderedDict([('nonDeprecated', GraphQLField(GraphQLString)), ('deprecated', GraphQLField(GraphQLString, deprecation_reason='Removed in 1.0'))])) schema = GraphQLSchema(TestType) request = '''{__type(name: "TestType") { name fields(includeDeprecated: true) { name isDeprecated deprecationReason } } }''' result = graphql(schema, request) assert not result.errors assert result.data == { '__type': { 'name': 'TestType', 'fields': [ { 'name': 'nonDeprecated', 'isDeprecated': False, 'deprecationReason': None }, { 'name': 'deprecated', 'isDeprecated': True, 'deprecationReason': 'Removed in 1.0' }, ] } }
def test_uses_built_in_scalars_when_possible(): customScalar = GraphQLScalarType(name='CustomScalar', serialize=lambda: None) schema = GraphQLSchema( query=GraphQLObjectType(name='Scalars', fields=OrderedDict([ ('int', GraphQLField(GraphQLInt)), ('float', GraphQLField(GraphQLFloat)), ('string', GraphQLField(GraphQLString)), ('boolean', GraphQLField(GraphQLBoolean)), ('id', GraphQLField(GraphQLID)), ('custom', GraphQLField(customScalar)), ]))) client_schema = _test_schema(schema) assert client_schema.get_type('Int') == GraphQLInt assert client_schema.get_type('Float') == GraphQLFloat assert client_schema.get_type('String') == GraphQLString assert client_schema.get_type('Boolean') == GraphQLBoolean assert client_schema.get_type('ID') == GraphQLID assert client_schema.get_type('CustomScalar') != customScalar
def test_builds_a_schema_with_a_recursive_type_reference(): recurType = GraphQLObjectType( name='Recur', fields=lambda: {'recur': GraphQLField(recurType)}) schema = GraphQLSchema(query=recurType) _test_schema(schema)
), 'human': GraphQLField( humanType, args={ 'id': GraphQLArgument( description='id of the human', type=GraphQLNonNull(GraphQLString), ) }, resolver=lambda root, args, *_: starwars_fixtures.getHuman(args[ 'id']), ), 'droid': GraphQLField( droidType, args={ 'id': GraphQLArgument( description='id of the droid', type=GraphQLNonNull(GraphQLString), ) }, resolver=lambda root, args, *_: starwars_fixtures.getDroid(args[ 'id']), ), }) StarWarsSchema = GraphQLSchema(query=queryType)
def print_single_field_schema(field_config): Root = GraphQLObjectType(name='Root', fields={'singleField': field_config}) return print_for_test(GraphQLSchema(Root))
PetType = GraphQLUnionType('Pet', [DogType, CatType], resolve_type=resolve_pet_type) PersonType = GraphQLObjectType( name='Person', interfaces=[NamedType], fields={ 'name': GraphQLField(GraphQLString), 'pets': GraphQLField(GraphQLList(PetType)), 'friends': GraphQLField(GraphQLList(NamedType)), }, is_type_of=lambda value: isinstance(value, Person)) schema = GraphQLSchema(PersonType) garfield = Cat('Garfield', False) odie = Dog('Odie', True) liz = Person('Liz', [], []) john = Person('John', [garfield, odie], [liz, odie]) # Execute: Union and intersection types def test_can_introspect_on_union_and_intersetion_types(): # TODO pass def test_executes_using_union_types():
def test_exposes_descriptions_on_enums(): QueryRoot = GraphQLObjectType('QueryRoot', {}) schema = GraphQLSchema(QueryRoot) request = '''{ typeKindType: __type(name: "__TypeKind") { name, description, enumValues { name, description } } } ''' result = graphql(schema, request) assert not result.errors assert sort_lists(result.data) == sort_lists({ 'typeKindType': { 'name': '__TypeKind', 'description': 'An enum describing what kind of type a given __Type is', 'enumValues': [{ 'description': 'Indicates this type is a scalar.', 'name': 'SCALAR' }, { 'description': 'Indicates this type is an object. ' + '`fields` and `interfaces` are valid fields.', 'name': 'OBJECT' }, { 'description': 'Indicates this type is an interface. ' + '`fields` and `possibleTypes` are valid fields.', 'name': 'INTERFACE' }, { 'description': 'Indicates this type is a union. ' + '`possibleTypes` is a valid field.', 'name': 'UNION' }, { 'description': 'Indicates this type is an enum. ' + '`enumValues` is a valid field.', 'name': 'ENUM' }, { 'description': 'Indicates this type is an input object. ' + '`inputFields` is a valid field.', 'name': 'INPUT_OBJECT' }, { 'description': 'Indicates this type is a list. ' + '`ofType` is a valid field.', 'name': 'LIST' }, { 'description': 'Indicates this type is a non-null. ' + '`ofType` is a valid field.', 'name': 'NON_NULL' }] } })
def test_executes_arbitary_code(): class Data(object): a = 'Apple' b = 'Banana' c = 'Cookie' d = 'Donut' e = 'Egg' @property def f(self): return succeed('Fish') def pic(self, size=50): return succeed('Pic of size: {}'.format(size)) def deep(self): return DeepData() def promise(self): return succeed(Data()) class DeepData(object): a = 'Already Been Done' b = 'Boring' c = ['Contrived', None, succeed('Confusing')] def deeper(self): return [Data(), None, succeed(Data())] doc = ''' query Example($size: Int) { a, b, x: c ...c f ...on DataType { pic(size: $size) promise { a } } deep { a b c deeper { a b } } } fragment c on DataType { d e } ''' expected = { 'a': 'Apple', 'b': 'Banana', 'x': 'Cookie', 'd': 'Donut', 'e': 'Egg', 'f': 'Fish', 'pic': 'Pic of size: 100', 'promise': { 'a': 'Apple' }, 'deep': { 'a': 'Already Been Done', 'b': 'Boring', 'c': ['Contrived', None, 'Confusing'], 'deeper': [{ 'a': 'Apple', 'b': 'Banana' }, None, { 'a': 'Apple', 'b': 'Banana' }] } } DataType = GraphQLObjectType( 'DataType', lambda: { 'a': GraphQLField(GraphQLString), 'b': GraphQLField(GraphQLString), 'c': GraphQLField(GraphQLString), 'd': GraphQLField(GraphQLString), 'e': GraphQLField(GraphQLString), 'f': GraphQLField(GraphQLString), 'pic': GraphQLField( args={'size': GraphQLArgument(GraphQLInt)}, type=GraphQLString, resolver=lambda obj, args, *_: obj.pic(args['size']), ), 'deep': GraphQLField(DeepDataType), 'promise': GraphQLField(DataType), }) DeepDataType = GraphQLObjectType( 'DeepDataType', { 'a': GraphQLField(GraphQLString), 'b': GraphQLField(GraphQLString), 'c': GraphQLField(GraphQLList(GraphQLString)), 'deeper': GraphQLField(GraphQLList(DataType)), }) schema = GraphQLSchema(query=DataType) executor = Executor() def handle_result(result): assert not result.errors assert result.data == expected raise_callback_results( executor.execute(schema, doc, Data(), {'size': 100}, 'Example'), handle_result) raise_callback_results( executor.execute(schema, doc, Data(), {'size': 100}, 'Example', execute_serially=True), handle_result)
args={'newNumber': GraphQLArgument(GraphQLInt)}, resolver=lambda obj, args, *_: obj. promise_to_change_the_number(args['newNumber'])), 'failToChangeTheNumber': GraphQLField(NumberHolderType, args={'newNumber': GraphQLArgument(GraphQLInt)}, resolver=lambda obj, args, *_: obj. fail_to_change_the_number(args['newNumber'])), 'promiseAndFailToChangeTheNumber': GraphQLField(NumberHolderType, args={'newNumber': GraphQLArgument(GraphQLInt)}, resolver=lambda obj, args, *_: obj. promise_and_fail_to_change_the_number(args['newNumber'])), }) schema = GraphQLSchema(QueryType, MutationType) def test_evaluates_mutations_serially(): doc = '''mutation M { first: immediatelyChangeTheNumber(newNumber: 1) { theNumber }, second: promiseToChangeTheNumber(newNumber: 2) { theNumber }, third: immediatelyChangeTheNumber(newNumber: 3) { theNumber } fourth: promiseToChangeTheNumber(newNumber: 4) { theNumber
def test_executes_using_a_schema(): BlogImage = GraphQLObjectType( 'BlogImage', { 'url': GraphQLField(GraphQLString), 'width': GraphQLField(GraphQLInt), 'height': GraphQLField(GraphQLInt), }) BlogAuthor = GraphQLObjectType( 'Author', lambda: { 'id': GraphQLField(GraphQLString), 'name': GraphQLField(GraphQLString), 'pic': GraphQLField(BlogImage, args={ 'width': GraphQLArgument(GraphQLInt), 'height': GraphQLArgument(GraphQLInt), }, resolver=lambda obj, args, *_: obj.pic( args['width'], args['height'])), 'recentArticle': GraphQLField(BlogArticle), }) BlogArticle = GraphQLObjectType( 'Article', { 'id': GraphQLField(GraphQLNonNull(GraphQLString)), 'isPublished': GraphQLField(GraphQLBoolean), 'author': GraphQLField(BlogAuthor), 'title': GraphQLField(GraphQLString), 'body': GraphQLField(GraphQLString), 'keywords': GraphQLField(GraphQLList(GraphQLString)), }) BlogQuery = GraphQLObjectType( 'Query', { 'article': GraphQLField(BlogArticle, args={'id': GraphQLArgument(GraphQLID)}, resolver=lambda obj, args, *_: Article(args['id'])), 'feed': GraphQLField(GraphQLList(BlogArticle), resolver=lambda *_: map(Article, range(1, 10 + 1))), }) BlogSchema = GraphQLSchema(BlogQuery) class Article(object): def __init__(self, id): self.id = id self.isPublished = True self.author = Author() self.title = 'My Article {}'.format(id) self.body = 'This is a post' self.hidden = 'This data is not exposed in the schema' self.keywords = ['foo', 'bar', 1, True, None] class Author(object): id = 123 name = 'John Smith' def pic(self, width, height): return Pic(123, width, height) @property def recentArticle(self): return Article(1) class Pic(object): def __init__(self, uid, width, height): self.url = 'cdn://{}'.format(uid) self.width = str(width) self.height = str(height) request = ''' { feed { id, title }, article(id: "1") { ...articleFields, author { id, name, pic(width: 640, height: 480) { url, width, height }, recentArticle { ...articleFields, keywords } } } } fragment articleFields on Article { id, isPublished, title, body, hidden, notdefined } ''' # Note: this is intentionally not validating to ensure appropriate # behavior occurs when executing an invalid query. result = execute(BlogSchema, None, parse(request)) assert not result.errors assert result.data == \ { "feed": [ { "id": "1", "title": "My Article 1" }, { "id": "2", "title": "My Article 2" }, { "id": "3", "title": "My Article 3" }, { "id": "4", "title": "My Article 4" }, { "id": "5", "title": "My Article 5" }, { "id": "6", "title": "My Article 6" }, { "id": "7", "title": "My Article 7" }, { "id": "8", "title": "My Article 8" }, { "id": "9", "title": "My Article 9" }, { "id": "10", "title": "My Article 10" } ], "article": { "id": "1", "isPublished": True, "title": "My Article 1", "body": "This is a post", "author": { "id": "123", "name": "John Smith", "pic": { "url": "cdn://123", "width": 640, "height": 480 }, "recentArticle": { "id": "1", "isPublished": True, "title": "My Article 1", "body": "This is a post", "keywords": [ "foo", "bar", "1", "true", None ] } } } }
fields=lambda: OrderedDict([ ('fizz', GraphQLField(GraphQLString)), ])) SomeUnionType = GraphQLUnionType( name='SomeUnion', resolve_type=lambda: FooType, types=[FooType, BizType], ) test_schema = GraphQLSchema(query=GraphQLObjectType( name='Query', fields=lambda: OrderedDict([ ('foo', GraphQLField(FooType)), ('someUnion', GraphQLField(SomeUnionType)), ('someInterface', GraphQLField( SomeInterfaceType, args={'id': GraphQLArgument(GraphQLNonNull(GraphQLID))}, )), ]))) def test_returns_original_schema_if_no_type_definitions(): ast = parse('{ field }') extended_schema = extend_schema(test_schema, ast) assert extended_schema == test_schema def test_extends_without_altering_original_schema(): ast = parse('''
def test_executes_an_introspection_query(): EmptySchema = GraphQLSchema(GraphQLObjectType('QueryRoot', {})) result = graphql(EmptySchema, introspection_query) assert not result.errors expected = { '__schema': {'directives': [{'args': [{'defaultValue': None, 'description': 'Directs the executor ' 'to include this field ' 'or fragment only when ' 'the `if` argument is ' 'true.', 'name': 'if', 'type': {'kind': 'NON_NULL', 'name': None, 'ofType': {'kind': 'SCALAR', 'name': 'Boolean', 'ofType': None}}}], 'description': None, 'name': 'include', 'onField': True, 'onFragment': True, 'onOperation': False}, {'args': [{'defaultValue': None, 'description': 'Directs the executor ' 'to skip this field or ' 'fragment only when the ' '`if` argument is true.', 'name': 'if', 'type': {'kind': 'NON_NULL', 'name': None, 'ofType': {'kind': 'SCALAR', 'name': 'Boolean', 'ofType': None}}}], 'description': None, 'name': 'skip', 'onField': True, 'onFragment': True, 'onOperation': False}], 'mutationType': None, 'queryType': {'name': 'QueryRoot'}, 'types': [{'description': 'An enum describing what kind of type ' 'a given __Type is', 'enumValues': [{'deprecationReason': None, 'description': 'Indicates this type ' 'is a scalar.', 'isDeprecated': False, 'name': 'SCALAR'}, {'deprecationReason': None, 'description': 'Indicates this type ' 'is an object. ' '`fields` and ' '`interfaces` are ' 'valid fields.', 'isDeprecated': False, 'name': 'OBJECT'}, {'deprecationReason': None, 'description': 'Indicates this type ' 'is an interface. ' '`fields` and ' '`possibleTypes` are ' 'valid fields.', 'isDeprecated': False, 'name': 'INTERFACE'}, {'deprecationReason': None, 'description': 'Indicates this type ' 'is a union. ' '`possibleTypes` is a ' 'valid field.', 'isDeprecated': False, 'name': 'UNION'}, {'deprecationReason': None, 'description': 'Indicates this type ' 'is an enum. ' '`enumValues` is a ' 'valid field.', 'isDeprecated': False, 'name': 'ENUM'}, {'deprecationReason': None, 'description': 'Indicates this type ' 'is an input object. ' '`inputFields` is a ' 'valid field.', 'isDeprecated': False, 'name': 'INPUT_OBJECT'}, {'deprecationReason': None, 'description': 'Indicates this type ' 'is a list. `ofType` ' 'is a valid field.', 'isDeprecated': False, 'name': 'LIST'}, {'deprecationReason': None, 'description': 'Indicates this type ' 'is a non-null. ' '`ofType` is a valid ' 'field.', 'isDeprecated': False, 'name': 'NON_NULL'}], 'fields': None, 'inputFields': None, 'interfaces': None, 'kind': 'ENUM', 'name': '__TypeKind', 'possibleTypes': None}, {'description': None, 'enumValues': None, 'fields': None, 'inputFields': None, 'interfaces': None, 'kind': 'SCALAR', 'name': 'String', 'possibleTypes': None}, {'description': None, 'enumValues': None, 'fields': [{'args': [], 'deprecationReason': None, 'description': None, 'isDeprecated': False, 'name': 'name', 'type': {'kind': 'NON_NULL', 'name': None, 'ofType': {'kind': 'SCALAR', 'name': 'String', 'ofType': None}}}, {'args': [], 'deprecationReason': None, 'description': None, 'isDeprecated': False, 'name': 'description', 'type': {'kind': 'SCALAR', 'name': 'String', 'ofType': None}}, {'args': [], 'deprecationReason': None, 'description': None, 'isDeprecated': False, 'name': 'args', 'type': {'kind': 'NON_NULL', 'name': None, 'ofType': {'kind': 'LIST', 'name': None, 'ofType': {'kind': 'NON_NULL', 'name': None, 'ofType': {'kind': 'OBJECT', 'name': '__InputValue'}}}}}, {'args': [], 'deprecationReason': None, 'description': None, 'isDeprecated': False, 'name': 'type', 'type': {'kind': 'NON_NULL', 'name': None, 'ofType': {'kind': 'OBJECT', 'name': '__Type', 'ofType': None}}}, {'args': [], 'deprecationReason': None, 'description': None, 'isDeprecated': False, 'name': 'isDeprecated', 'type': {'kind': 'NON_NULL', 'name': None, 'ofType': {'kind': 'SCALAR', 'name': 'Boolean', 'ofType': None}}}, {'args': [], 'deprecationReason': None, 'description': None, 'isDeprecated': False, 'name': 'deprecationReason', 'type': {'kind': 'SCALAR', 'name': 'String', 'ofType': None}}], 'inputFields': None, 'interfaces': [], 'kind': 'OBJECT', 'name': '__Field', 'possibleTypes': None}, {'description': 'A GraphQL Schema defines the ' 'capabilities of a GraphQL server. It ' 'exposes all available types and ' 'directives on the server, as well as ' 'the entry points for query and ' 'mutation operations.', 'enumValues': None, 'fields': [{'args': [], 'deprecationReason': None, 'description': 'A list of all types ' 'supported by this server.', 'isDeprecated': False, 'name': 'types', 'type': {'kind': 'NON_NULL', 'name': None, 'ofType': {'kind': 'LIST', 'name': None, 'ofType': {'kind': 'NON_NULL', 'name': None, 'ofType': {'kind': 'OBJECT', 'name': '__Type'}}}}}, {'args': [], 'deprecationReason': None, 'description': 'The type that query ' 'operations will be rooted ' 'at.', 'isDeprecated': False, 'name': 'queryType', 'type': {'kind': 'NON_NULL', 'name': None, 'ofType': {'kind': 'OBJECT', 'name': '__Type', 'ofType': None}}}, {'args': [], 'deprecationReason': None, 'description': 'If this server supports ' 'mutation, the type that ' 'mutation operations will ' 'be rooted at.', 'isDeprecated': False, 'name': 'mutationType', 'type': {'kind': 'OBJECT', 'name': '__Type', 'ofType': None}}, {'args': [], 'deprecationReason': None, 'description': 'A list of all directives ' 'supported by this server.', 'isDeprecated': False, 'name': 'directives', 'type': {'kind': 'NON_NULL', 'name': None, 'ofType': {'kind': 'LIST', 'name': None, 'ofType': {'kind': 'NON_NULL', 'name': None, 'ofType': {'kind': 'OBJECT', 'name': '__Directive'}}}}}], 'inputFields': None, 'interfaces': [], 'kind': 'OBJECT', 'name': '__Schema', 'possibleTypes': None}, {'description': None, 'enumValues': None, 'fields': [{'args': [], 'deprecationReason': None, 'description': None, 'isDeprecated': False, 'name': 'kind', 'type': {'kind': 'NON_NULL', 'name': None, 'ofType': {'kind': 'ENUM', 'name': '__TypeKind', 'ofType': None}}}, {'args': [], 'deprecationReason': None, 'description': None, 'isDeprecated': False, 'name': 'name', 'type': {'kind': 'SCALAR', 'name': 'String', 'ofType': None}}, {'args': [], 'deprecationReason': None, 'description': None, 'isDeprecated': False, 'name': 'description', 'type': {'kind': 'SCALAR', 'name': 'String', 'ofType': None}}, {'args': [{'defaultValue': 'false', 'description': None, 'name': 'includeDeprecated', 'type': {'kind': 'SCALAR', 'name': 'Boolean', 'ofType': None}}], 'deprecationReason': None, 'description': None, 'isDeprecated': False, 'name': 'fields', 'type': {'kind': 'LIST', 'name': None, 'ofType': {'kind': 'NON_NULL', 'name': None, 'ofType': {'kind': 'OBJECT', 'name': '__Field', 'ofType': None}}}}, {'args': [], 'deprecationReason': None, 'description': None, 'isDeprecated': False, 'name': 'interfaces', 'type': {'kind': 'LIST', 'name': None, 'ofType': {'kind': 'NON_NULL', 'name': None, 'ofType': {'kind': 'OBJECT', 'name': '__Type', 'ofType': None}}}}, {'args': [], 'deprecationReason': None, 'description': None, 'isDeprecated': False, 'name': 'possibleTypes', 'type': {'kind': 'LIST', 'name': None, 'ofType': {'kind': 'NON_NULL', 'name': None, 'ofType': {'kind': 'OBJECT', 'name': '__Type', 'ofType': None}}}}, {'args': [{'defaultValue': 'false', 'description': None, 'name': 'includeDeprecated', 'type': {'kind': 'SCALAR', 'name': 'Boolean', 'ofType': None}}], 'deprecationReason': None, 'description': None, 'isDeprecated': False, 'name': 'enumValues', 'type': {'kind': 'LIST', 'name': None, 'ofType': {'kind': 'NON_NULL', 'name': None, 'ofType': {'kind': 'OBJECT', 'name': '__EnumValue', 'ofType': None}}}}, {'args': [], 'deprecationReason': None, 'description': None, 'isDeprecated': False, 'name': 'inputFields', 'type': {'kind': 'LIST', 'name': None, 'ofType': {'kind': 'NON_NULL', 'name': None, 'ofType': {'kind': 'OBJECT', 'name': '__InputValue', 'ofType': None}}}}, {'args': [], 'deprecationReason': None, 'description': None, 'isDeprecated': False, 'name': 'ofType', 'type': {'kind': 'OBJECT', 'name': '__Type', 'ofType': None}}], 'inputFields': None, 'interfaces': [], 'kind': 'OBJECT', 'name': '__Type', 'possibleTypes': None}, {'description': None, 'enumValues': None, 'fields': [{'args': [], 'deprecationReason': None, 'description': None, 'isDeprecated': False, 'name': 'name', 'type': {'kind': 'NON_NULL', 'name': None, 'ofType': {'kind': 'SCALAR', 'name': 'String', 'ofType': None}}}, {'args': [], 'deprecationReason': None, 'description': None, 'isDeprecated': False, 'name': 'description', 'type': {'kind': 'SCALAR', 'name': 'String', 'ofType': None}}, {'args': [], 'deprecationReason': None, 'description': None, 'isDeprecated': False, 'name': 'isDeprecated', 'type': {'kind': 'NON_NULL', 'name': None, 'ofType': {'kind': 'SCALAR', 'name': 'Boolean', 'ofType': None}}}, {'args': [], 'deprecationReason': None, 'description': None, 'isDeprecated': False, 'name': 'deprecationReason', 'type': {'kind': 'SCALAR', 'name': 'String', 'ofType': None}}], 'inputFields': None, 'interfaces': [], 'kind': 'OBJECT', 'name': '__EnumValue', 'possibleTypes': None}, {'description': None, 'enumValues': None, 'fields': [{'args': [], 'deprecationReason': None, 'description': None, 'isDeprecated': False, 'name': 'name', 'type': {'kind': 'NON_NULL', 'name': None, 'ofType': {'kind': 'SCALAR', 'name': 'String', 'ofType': None}}}, {'args': [], 'deprecationReason': None, 'description': None, 'isDeprecated': False, 'name': 'description', 'type': {'kind': 'SCALAR', 'name': 'String', 'ofType': None}}, {'args': [], 'deprecationReason': None, 'description': None, 'isDeprecated': False, 'name': 'args', 'type': {'kind': 'NON_NULL', 'name': None, 'ofType': {'kind': 'LIST', 'name': None, 'ofType': {'kind': 'NON_NULL', 'name': None, 'ofType': {'kind': 'OBJECT', 'name': '__InputValue'}}}}}, {'args': [], 'deprecationReason': None, 'description': None, 'isDeprecated': False, 'name': 'onOperation', 'type': {'kind': 'NON_NULL', 'name': None, 'ofType': {'kind': 'SCALAR', 'name': 'Boolean', 'ofType': None}}}, {'args': [], 'deprecationReason': None, 'description': None, 'isDeprecated': False, 'name': 'onFragment', 'type': {'kind': 'NON_NULL', 'name': None, 'ofType': {'kind': 'SCALAR', 'name': 'Boolean', 'ofType': None}}}, {'args': [], 'deprecationReason': None, 'description': None, 'isDeprecated': False, 'name': 'onField', 'type': {'kind': 'NON_NULL', 'name': None, 'ofType': {'kind': 'SCALAR', 'name': 'Boolean', 'ofType': None}}}], 'inputFields': None, 'interfaces': [], 'kind': 'OBJECT', 'name': '__Directive', 'possibleTypes': None}, {'description': None, 'enumValues': None, 'fields': None, 'inputFields': None, 'interfaces': None, 'kind': 'SCALAR', 'name': 'Boolean', 'possibleTypes': None}, {'description': None, 'enumValues': None, 'fields': [{'args': [], 'deprecationReason': None, 'description': None, 'isDeprecated': False, 'name': 'name', 'type': {'kind': 'NON_NULL', 'name': None, 'ofType': {'kind': 'SCALAR', 'name': 'String', 'ofType': None}}}, {'args': [], 'deprecationReason': None, 'description': None, 'isDeprecated': False, 'name': 'description', 'type': {'kind': 'SCALAR', 'name': 'String', 'ofType': None}}, {'args': [], 'deprecationReason': None, 'description': None, 'isDeprecated': False, 'name': 'type', 'type': {'kind': 'NON_NULL', 'name': None, 'ofType': {'kind': 'OBJECT', 'name': '__Type', 'ofType': None}}}, {'args': [], 'deprecationReason': None, 'description': None, 'isDeprecated': False, 'name': 'defaultValue', 'type': {'kind': 'SCALAR', 'name': 'String', 'ofType': None}}], 'inputFields': None, 'interfaces': [], 'kind': 'OBJECT', 'name': '__InputValue', 'possibleTypes': None}, {'description': None, 'enumValues': None, 'fields': [], 'inputFields': None, 'interfaces': [], 'kind': 'OBJECT', 'name': 'QueryRoot', 'possibleTypes': None}]}} assert sort_lists(result.data) == sort_lists(expected)
def test_executes_an_introspection_query(): EmptySchema = GraphQLSchema(GraphQLObjectType('QueryRoot', {'f': GraphQLField(GraphQLString)})) result = graphql(EmptySchema, introspection_query) assert not result.errors expected = { '__schema': {'directives': [{'args': [{'defaultValue': None, 'description': u'Directs the executor to include this field or fragment only when the `if` argument is true.', 'name': u'if', 'type': {'kind': 'NON_NULL', 'name': None, 'ofType': {'kind': 'SCALAR', 'name': u'Boolean', 'ofType': None}}}], 'description': None, 'name': u'include', 'onField': True, 'onFragment': True, 'onOperation': False}, {'args': [{'defaultValue': None, 'description': u'Directs the executor to skip this field or fragment only when the `if` argument is true.', 'name': u'if', 'type': {'kind': 'NON_NULL', 'name': None, 'ofType': {'kind': 'SCALAR', 'name': u'Boolean', 'ofType': None}}}], 'description': None, 'name': u'skip', 'onField': True, 'onFragment': True, 'onOperation': False}], 'mutationType': None, 'queryType': {'name': u'QueryRoot'}, 'subscriptionType': None, 'types': [{'description': None, 'enumValues': None, 'fields': [{'args': [], 'deprecationReason': None, 'description': None, 'isDeprecated': False, 'name': u'f', 'type': {'kind': 'SCALAR', 'name': u'String', 'ofType': None}}], 'inputFields': None, 'interfaces': [], 'kind': 'OBJECT', 'name': u'QueryRoot', 'possibleTypes': None}, { 'description': u'The `String` scalar type represents textual data, represented as UTF-8 character sequences. The String type is most often used by GraphQL to represent free-form human-readable text.', 'enumValues': None, 'fields': None, 'inputFields': None, 'interfaces': None, 'kind': 'SCALAR', 'name': u'String', 'possibleTypes': None}, { 'description': u'A GraphQL Schema defines the capabilities of a GraphQL server. It exposes all available types and directives on the server, as well as the entry points for query, mutation and subscription operations.', 'enumValues': None, 'fields': [{'args': [], 'deprecationReason': None, 'description': u'A list of all types supported by this server.', 'isDeprecated': False, 'name': u'types', 'type': {'kind': 'NON_NULL', 'name': None, 'ofType': {'kind': 'LIST', 'name': None, 'ofType': {'kind': 'NON_NULL', 'name': None, 'ofType': {'kind': 'OBJECT', 'name': u'__Type'}}}}}, {'args': [], 'deprecationReason': None, 'description': u'The type that query operations will be rooted at.', 'isDeprecated': False, 'name': u'queryType', 'type': {'kind': 'NON_NULL', 'name': None, 'ofType': {'kind': 'OBJECT', 'name': u'__Type', 'ofType': None}}}, {'args': [], 'deprecationReason': None, 'description': u'If this server supports mutation, the type that mutation operations will be rooted at.', 'isDeprecated': False, 'name': u'mutationType', 'type': {'kind': 'OBJECT', 'name': u'__Type', 'ofType': None}}, {'args': [], 'deprecationReason': None, 'description': u'If this server support subscription, the type that subscription operations will be rooted at.', 'isDeprecated': False, 'name': u'subscriptionType', 'type': {'kind': 'OBJECT', 'name': u'__Type', 'ofType': None}}, {'args': [], 'deprecationReason': None, 'description': u'A list of all directives supported by this server.', 'isDeprecated': False, 'name': u'directives', 'type': {'kind': 'NON_NULL', 'name': None, 'ofType': {'kind': 'LIST', 'name': None, 'ofType': {'kind': 'NON_NULL', 'name': None, 'ofType': {'kind': 'OBJECT', 'name': u'__Directive'}}}}}], 'inputFields': None, 'interfaces': [], 'kind': 'OBJECT', 'name': u'__Schema', 'possibleTypes': None}, { 'description': u'The fundamental unit of any GraphQL Schema is the type. There are many kinds of types in GraphQL as represented by the `__TypeKind` enum.\n\nDepending on the kind of a type, certain fields describe information about that type. Scalar types provide no information beyond a name and description, while Enum types provide their values. Object and Interface types provide the fields they describe. Abstract types, Union and Interface, provide the Object types possible at runtime. List and NonNull types compose other types.', 'enumValues': None, 'fields': [{'args': [], 'deprecationReason': None, 'description': None, 'isDeprecated': False, 'name': u'kind', 'type': {'kind': 'NON_NULL', 'name': None, 'ofType': {'kind': 'ENUM', 'name': u'__TypeKind', 'ofType': None}}}, {'args': [], 'deprecationReason': None, 'description': None, 'isDeprecated': False, 'name': u'name', 'type': {'kind': 'SCALAR', 'name': u'String', 'ofType': None}}, {'args': [], 'deprecationReason': None, 'description': None, 'isDeprecated': False, 'name': u'description', 'type': {'kind': 'SCALAR', 'name': u'String', 'ofType': None}}, {'args': [{'defaultValue': u'false', 'description': None, 'name': u'includeDeprecated', 'type': {'kind': 'SCALAR', 'name': u'Boolean', 'ofType': None}}], 'deprecationReason': None, 'description': None, 'isDeprecated': False, 'name': u'fields', 'type': {'kind': 'LIST', 'name': None, 'ofType': {'kind': 'NON_NULL', 'name': None, 'ofType': {'kind': 'OBJECT', 'name': u'__Field', 'ofType': None}}}}, {'args': [], 'deprecationReason': None, 'description': None, 'isDeprecated': False, 'name': u'interfaces', 'type': {'kind': 'LIST', 'name': None, 'ofType': {'kind': 'NON_NULL', 'name': None, 'ofType': {'kind': 'OBJECT', 'name': u'__Type', 'ofType': None}}}}, {'args': [], 'deprecationReason': None, 'description': None, 'isDeprecated': False, 'name': u'possibleTypes', 'type': {'kind': 'LIST', 'name': None, 'ofType': {'kind': 'NON_NULL', 'name': None, 'ofType': {'kind': 'OBJECT', 'name': u'__Type', 'ofType': None}}}}, {'args': [{'defaultValue': u'false', 'description': None, 'name': u'includeDeprecated', 'type': {'kind': 'SCALAR', 'name': u'Boolean', 'ofType': None}}], 'deprecationReason': None, 'description': None, 'isDeprecated': False, 'name': u'enumValues', 'type': {'kind': 'LIST', 'name': None, 'ofType': {'kind': 'NON_NULL', 'name': None, 'ofType': {'kind': 'OBJECT', 'name': u'__EnumValue', 'ofType': None}}}}, {'args': [], 'deprecationReason': None, 'description': None, 'isDeprecated': False, 'name': u'inputFields', 'type': {'kind': 'LIST', 'name': None, 'ofType': {'kind': 'NON_NULL', 'name': None, 'ofType': {'kind': 'OBJECT', 'name': u'__InputValue', 'ofType': None}}}}, {'args': [], 'deprecationReason': None, 'description': None, 'isDeprecated': False, 'name': u'ofType', 'type': {'kind': 'OBJECT', 'name': u'__Type', 'ofType': None}}], 'inputFields': None, 'interfaces': [], 'kind': 'OBJECT', 'name': u'__Type', 'possibleTypes': None}, {'description': u'An enum describing what kind of type a given `__Type` is', 'enumValues': [{'deprecationReason': None, 'description': u'Indicates this type is a scalar.', 'isDeprecated': False, 'name': u'SCALAR'}, {'deprecationReason': None, 'description': u'Indicates this type is an object. `fields` and `interfaces` are valid fields.', 'isDeprecated': False, 'name': u'OBJECT'}, {'deprecationReason': None, 'description': u'Indicates this type is an interface. `fields` and `possibleTypes` are valid fields.', 'isDeprecated': False, 'name': u'INTERFACE'}, {'deprecationReason': None, 'description': u'Indicates this type is a union. `possibleTypes` is a valid field.', 'isDeprecated': False, 'name': u'UNION'}, {'deprecationReason': None, 'description': u'Indicates this type is an enum. `enumValues` is a valid field.', 'isDeprecated': False, 'name': u'ENUM'}, {'deprecationReason': None, 'description': u'Indicates this type is an input object. `inputFields` is a valid field.', 'isDeprecated': False, 'name': u'INPUT_OBJECT'}, {'deprecationReason': None, 'description': u'Indicates this type is a list. `ofType` is a valid field.', 'isDeprecated': False, 'name': u'LIST'}, {'deprecationReason': None, 'description': u'Indicates this type is a non-null. `ofType` is a valid field.', 'isDeprecated': False, 'name': u'NON_NULL'}], 'fields': None, 'inputFields': None, 'interfaces': None, 'kind': 'ENUM', 'name': u'__TypeKind', 'possibleTypes': None}, {'description': u'The `Boolean` scalar type represents `true` or `false`.', 'enumValues': None, 'fields': None, 'inputFields': None, 'interfaces': None, 'kind': 'SCALAR', 'name': u'Boolean', 'possibleTypes': None}, { 'description': u'Object and Interface types are described by a list of Fields, each of which has a name, potentially a list of arguments, and a return type.', 'enumValues': None, 'fields': [{'args': [], 'deprecationReason': None, 'description': None, 'isDeprecated': False, 'name': u'name', 'type': {'kind': 'NON_NULL', 'name': None, 'ofType': {'kind': 'SCALAR', 'name': u'String', 'ofType': None}}}, {'args': [], 'deprecationReason': None, 'description': None, 'isDeprecated': False, 'name': u'description', 'type': {'kind': 'SCALAR', 'name': u'String', 'ofType': None}}, {'args': [], 'deprecationReason': None, 'description': None, 'isDeprecated': False, 'name': u'args', 'type': {'kind': 'NON_NULL', 'name': None, 'ofType': {'kind': 'LIST', 'name': None, 'ofType': {'kind': 'NON_NULL', 'name': None, 'ofType': {'kind': 'OBJECT', 'name': u'__InputValue'}}}}}, {'args': [], 'deprecationReason': None, 'description': None, 'isDeprecated': False, 'name': u'type', 'type': {'kind': 'NON_NULL', 'name': None, 'ofType': {'kind': 'OBJECT', 'name': u'__Type', 'ofType': None}}}, {'args': [], 'deprecationReason': None, 'description': None, 'isDeprecated': False, 'name': u'isDeprecated', 'type': {'kind': 'NON_NULL', 'name': None, 'ofType': {'kind': 'SCALAR', 'name': u'Boolean', 'ofType': None}}}, {'args': [], 'deprecationReason': None, 'description': None, 'isDeprecated': False, 'name': u'deprecationReason', 'type': {'kind': 'SCALAR', 'name': u'String', 'ofType': None}}], 'inputFields': None, 'interfaces': [], 'kind': 'OBJECT', 'name': u'__Field', 'possibleTypes': None}, { 'description': u'Arguments provided to Fields or Directives and the input fields of an InputObject are represented as Input Values which describe their type and optionally a default value.', 'enumValues': None, 'fields': [{'args': [], 'deprecationReason': None, 'description': None, 'isDeprecated': False, 'name': u'name', 'type': {'kind': 'NON_NULL', 'name': None, 'ofType': {'kind': 'SCALAR', 'name': u'String', 'ofType': None}}}, {'args': [], 'deprecationReason': None, 'description': None, 'isDeprecated': False, 'name': u'description', 'type': {'kind': 'SCALAR', 'name': u'String', 'ofType': None}}, {'args': [], 'deprecationReason': None, 'description': None, 'isDeprecated': False, 'name': u'type', 'type': {'kind': 'NON_NULL', 'name': None, 'ofType': {'kind': 'OBJECT', 'name': u'__Type', 'ofType': None}}}, {'args': [], 'deprecationReason': None, 'description': None, 'isDeprecated': False, 'name': u'defaultValue', 'type': {'kind': 'SCALAR', 'name': u'String', 'ofType': None}}], 'inputFields': None, 'interfaces': [], 'kind': 'OBJECT', 'name': u'__InputValue', 'possibleTypes': None}, { 'description': u'One possible value for a given Enum. Enum values are unique values, not a placeholder for a string or numeric value. However an Enum value is returned in a JSON response as a string.', 'enumValues': None, 'fields': [{'args': [], 'deprecationReason': None, 'description': None, 'isDeprecated': False, 'name': u'name', 'type': {'kind': 'NON_NULL', 'name': None, 'ofType': {'kind': 'SCALAR', 'name': u'String', 'ofType': None}}}, {'args': [], 'deprecationReason': None, 'description': None, 'isDeprecated': False, 'name': u'description', 'type': {'kind': 'SCALAR', 'name': u'String', 'ofType': None}}, {'args': [], 'deprecationReason': None, 'description': None, 'isDeprecated': False, 'name': u'isDeprecated', 'type': {'kind': 'NON_NULL', 'name': None, 'ofType': {'kind': 'SCALAR', 'name': u'Boolean', 'ofType': None}}}, {'args': [], 'deprecationReason': None, 'description': None, 'isDeprecated': False, 'name': u'deprecationReason', 'type': {'kind': 'SCALAR', 'name': u'String', 'ofType': None}}], 'inputFields': None, 'interfaces': [], 'kind': 'OBJECT', 'name': u'__EnumValue', 'possibleTypes': None}, { 'description': u"A Directive provides a way to describe alternate runtime execution and type validation behavior in a GraphQL document.\n\nIn some cases, you need to provide options to alter GraphQL's execution behavior in ways field arguments will not suffice, such as conditionally including or skipping a field. Directives provide this by describing additional information to the executor.", 'enumValues': None, 'fields': [{'args': [], 'deprecationReason': None, 'description': None, 'isDeprecated': False, 'name': u'name', 'type': {'kind': 'NON_NULL', 'name': None, 'ofType': {'kind': 'SCALAR', 'name': u'String', 'ofType': None}}}, {'args': [], 'deprecationReason': None, 'description': None, 'isDeprecated': False, 'name': u'description', 'type': {'kind': 'SCALAR', 'name': u'String', 'ofType': None}}, {'args': [], 'deprecationReason': None, 'description': None, 'isDeprecated': False, 'name': u'args', 'type': {'kind': 'NON_NULL', 'name': None, 'ofType': {'kind': 'LIST', 'name': None, 'ofType': {'kind': 'NON_NULL', 'name': None, 'ofType': {'kind': 'OBJECT', 'name': u'__InputValue'}}}}}, {'args': [], 'deprecationReason': None, 'description': None, 'isDeprecated': False, 'name': u'onOperation', 'type': {'kind': 'NON_NULL', 'name': None, 'ofType': {'kind': 'SCALAR', 'name': u'Boolean', 'ofType': None}}}, {'args': [], 'deprecationReason': None, 'description': None, 'isDeprecated': False, 'name': u'onFragment', 'type': {'kind': 'NON_NULL', 'name': None, 'ofType': {'kind': 'SCALAR', 'name': u'Boolean', 'ofType': None}}}, {'args': [], 'deprecationReason': None, 'description': None, 'isDeprecated': False, 'name': u'onField', 'type': {'kind': 'NON_NULL', 'name': None, 'ofType': {'kind': 'SCALAR', 'name': u'Boolean', 'ofType': None}}}], 'inputFields': None, 'interfaces': [], 'kind': 'OBJECT', 'name': u'__Directive', 'possibleTypes': None}]}} assert result.data == expected
}) QueryRoot = GraphQLObjectType('QueryRoot', { 'human': GraphQLField(Human, { 'id': GraphQLArgument(GraphQLID), }), 'dog': GraphQLField(Dog), 'pet': GraphQLField(Pet), 'catOrDog': GraphQLField(CatOrDog), 'humanOrAlien': GraphQLField(HumanOrAlien), 'complicatedArgs': GraphQLField(ComplicatedArgs), }) test_schema = GraphQLSchema(query=QueryRoot, directives=[ GraphQLDirective(name='operationOnly', on_operation=True), GraphQLIncludeDirective, GraphQLSkipDirective ]) def expect_valid(schema, rules, query): errors = validate(schema, parse(query), rules) assert errors == [], 'Should validate' def sort_lists(value): if isinstance(value, dict): new_mapping = [] for k, v in value.items(): new_mapping.append((k, sort_lists(v))) return sorted(new_mapping)
def resolver(root, args, *_): return 'Hello ' + args.get('who', 'World') TestSchema = GraphQLSchema( query=GraphQLObjectType( 'Root', fields=lambda: { 'test': GraphQLField( GraphQLString, args={ 'who': GraphQLArgument( type=GraphQLString ) }, resolver=resolver ), 'thrower': GraphQLField( GraphQLNonNull(GraphQLString), resolver=raises ) } ) ) def test_GET_functionality_allows_GET_with_query_param(): wsgi = graphql_wsgi(TestSchema) c = Client(wsgi)
resolver=input_to_json), 'listNN': GraphQLField( GraphQLString, args={'input': GraphQLArgument( GraphQLList(GraphQLNonNull(GraphQLString)) )}, resolver=input_to_json), 'nnListNN': GraphQLField( GraphQLString, args={'input': GraphQLArgument( GraphQLNonNull(GraphQLList(GraphQLNonNull(GraphQLString))) )}, resolver=input_to_json), }) schema = GraphQLSchema(TestType) def check(doc, expected, args=None): ast = parse(doc) response = execute(schema, None, ast, args=args) if response.errors: result = { 'data': response.data, 'errors': [format_error(e) for e in response.errors] } else: result = { 'data': response.data }
def test_resolve_type_on_union_yields_useful_error(): DogType = GraphQLObjectType(name='Dog', fields={ 'name': GraphQLField(GraphQLString), 'woofs': GraphQLField(GraphQLBoolean) }) HumanType = GraphQLObjectType(name='Human', fields={ 'name': GraphQLField(GraphQLString), }) CatType = GraphQLObjectType(name='Cat', fields={ 'name': GraphQLField(GraphQLString), 'meows': GraphQLField(GraphQLBoolean) }) PetType = GraphQLUnionType( name='Pet', types=[DogType, CatType], resolve_type=make_type_resolver( lambda: [(Dog, DogType), (Cat, CatType), (Human, HumanType)])) schema = GraphQLSchema( query=GraphQLObjectType(name='Query', fields={ 'pets': GraphQLField(GraphQLList(PetType), resolver=lambda *_: [ Dog('Odie', True), Cat('Garfield', False), Human('Jon') ]) })) query = ''' { pets { ... on Dog { name woofs } ... on Cat { name meows } } } ''' result = graphql(schema, query) assert result.errors[ 0].message == 'Runtime Object type "Human" is not a possible type for "Pet".' assert result.data == { 'pets': [{ 'woofs': True, 'name': 'Odie' }, { 'name': 'Garfield', 'meows': False }, None] }
def test_synchronous_error_nulls_out_error_subtrees(): doc = ''' { sync syncError syncReturnError syncReturnErrorList async asyncReject asyncEmptyReject asyncReturnError } ''' class Data: def sync(self): return 'sync' def syncError(self): raise Exception('Error getting syncError') def syncReturnError(self): return Exception("Error getting syncReturnError") def syncReturnErrorList(self): return [ 'sync0', Exception('Error getting syncReturnErrorList1'), 'sync2', Exception('Error getting syncReturnErrorList3') ] def async (self): return succeed('async') def asyncReject(self): return fail(Exception('Error getting asyncReject')) def asyncEmptyReject(self): return fail() def asyncReturnError(self): return succeed(Exception('Error getting asyncReturnError')) schema = GraphQLSchema(query=GraphQLObjectType( name='Type', fields={ 'sync': GraphQLField(GraphQLString), 'syncError': GraphQLField(GraphQLString), 'syncReturnError': GraphQLField(GraphQLString), 'syncReturnErrorList': GraphQLField(GraphQLList(GraphQLString)), 'async': GraphQLField(GraphQLString), 'asyncReject': GraphQLField(GraphQLString), 'asyncEmptyReject': GraphQLField(GraphQLString), 'asyncReturnError': GraphQLField(GraphQLString), })) executor = Executor(map_type=OrderedDict) def handle_results(result): assert result.data == { 'async': 'async', 'asyncEmptyReject': None, 'asyncReject': None, 'asyncReturnError': None, 'sync': 'sync', 'syncError': None, 'syncReturnError': None, 'syncReturnErrorList': ['sync0', None, 'sync2', None] } assert list(map(format_error, result.errors)) == [{ 'locations': [{ 'line': 4, 'column': 9 }], 'message': 'Error getting syncError' }, { 'locations': [{ 'line': 5, 'column': 9 }], 'message': 'Error getting syncReturnError' }, { 'locations': [{ 'line': 6, 'column': 9 }], 'message': 'Error getting syncReturnErrorList1' }, { 'locations': [{ 'line': 6, 'column': 9 }], 'message': 'Error getting syncReturnErrorList3' }, { 'locations': [{ 'line': 8, 'column': 9 }], 'message': 'Error getting asyncReject' }, { 'locations': [{ 'line': 9, 'column': 9 }], 'message': 'An unknown error occurred.' }, { 'locations': [{ 'line': 10, 'column': 9 }], 'message': 'Error getting asyncReturnError' }] raise_callback_results(executor.execute(schema, doc, Data()), handle_results)
from graphql.core.execution import execute from graphql.core.language.parser import parse from graphql.core.type import GraphQLSchema, GraphQLObjectType, GraphQLField, GraphQLString schema = GraphQLSchema( query=GraphQLObjectType(name='TestType', fields={ 'a': GraphQLField(GraphQLString), 'b': GraphQLField(GraphQLString), })) class Data(object): a = 'a' b = 'b' def execute_test_query(doc): return execute(schema, Data, parse(doc)) def test_basic_query_works(): result = execute_test_query('{ a, b }') assert not result.errors assert result.data == {'a': 'a', 'b': 'b'} def test_if_true_includes_scalar(): result = execute_test_query('{ a, b @include(if: true) }') assert not result.errors assert result.data == {'a': 'a', 'b': 'b'}
def test_executes_an_introspection_query(): EmptySchema = GraphQLSchema(GraphQLObjectType('QueryRoot', {})) result = graphql(EmptySchema, introspection_query) assert not result.errors assert sort_lists(result.data) == sort_lists({ '__schema': { 'directives': [{ 'args': [{ 'defaultValue': None, 'name': 'if', 'type': { 'kind': 'NON_NULL', 'name': None, 'ofType': { 'kind': 'SCALAR', 'name': 'Boolean', 'ofType': None } } }], 'name': 'include', 'onField': True, 'onFragment': True, 'onOperation': False }, { 'args': [{ 'defaultValue': None, 'name': 'if', 'type': { 'kind': 'NON_NULL', 'name': None, 'ofType': { 'kind': 'SCALAR', 'name': 'Boolean', 'ofType': None } } }], 'name': 'skip', 'onField': True, 'onFragment': True, 'onOperation': False }], 'mutationType': None, 'queryType': { 'name': 'QueryRoot' }, 'types': [{ 'enumValues': None, 'fields': [], 'inputFields': None, 'interfaces': [], 'kind': 'OBJECT', 'name': 'QueryRoot', 'possibleTypes': None }, { 'enumValues': None, 'fields': [{ 'args': [], 'deprecationReason': None, 'isDeprecated': False, 'name': 'types', 'type': { 'kind': 'NON_NULL', 'name': None, 'ofType': { 'kind': 'LIST', 'name': None, 'ofType': { 'kind': 'NON_NULL', 'name': None, 'ofType': { 'kind': 'OBJECT', 'name': '__Type' } } } } }, { 'args': [], 'deprecationReason': None, 'isDeprecated': False, 'name': 'queryType', 'type': { 'kind': 'NON_NULL', 'name': None, 'ofType': { 'kind': 'OBJECT', 'name': '__Type', 'ofType': None } } }, { 'args': [], 'deprecationReason': None, 'isDeprecated': False, 'name': 'mutationType', 'type': { 'kind': 'OBJECT', 'name': '__Type', 'ofType': None } }, { 'args': [], 'deprecationReason': None, 'isDeprecated': False, 'name': 'directives', 'type': { 'kind': 'NON_NULL', 'name': None, 'ofType': { 'kind': 'LIST', 'name': None, 'ofType': { 'kind': 'NON_NULL', 'name': None, 'ofType': { 'kind': 'OBJECT', 'name': '__Directive' } } } } }], 'inputFields': None, 'interfaces': [], 'kind': 'OBJECT', 'name': '__Schema', 'possibleTypes': None }, { 'enumValues': None, 'fields': [{ 'args': [], 'deprecationReason': None, 'isDeprecated': False, 'name': 'kind', 'type': { 'kind': 'NON_NULL', 'name': None, 'ofType': { 'kind': 'ENUM', 'name': '__TypeKind', 'ofType': None } } }, { 'args': [], 'deprecationReason': None, 'isDeprecated': False, 'name': 'name', 'type': { 'kind': 'SCALAR', 'name': 'String', 'ofType': None } }, { 'args': [], 'deprecationReason': None, 'isDeprecated': False, 'name': 'description', 'type': { 'kind': 'SCALAR', 'name': 'String', 'ofType': None } }, { 'args': [{ 'defaultValue': 'false', 'name': 'includeDeprecated', 'type': { 'kind': 'SCALAR', 'name': 'Boolean', 'ofType': None } }], 'deprecationReason': None, 'isDeprecated': False, 'name': 'fields', 'type': { 'kind': 'LIST', 'name': None, 'ofType': { 'kind': 'NON_NULL', 'name': None, 'ofType': { 'kind': 'OBJECT', 'name': '__Field', 'ofType': None } } } }, { 'args': [], 'deprecationReason': None, 'isDeprecated': False, 'name': 'interfaces', 'type': { 'kind': 'LIST', 'name': None, 'ofType': { 'kind': 'NON_NULL', 'name': None, 'ofType': { 'kind': 'OBJECT', 'name': '__Type', 'ofType': None } } } }, { 'args': [], 'deprecationReason': None, 'isDeprecated': False, 'name': 'possibleTypes', 'type': { 'kind': 'LIST', 'name': None, 'ofType': { 'kind': 'NON_NULL', 'name': None, 'ofType': { 'kind': 'OBJECT', 'name': '__Type', 'ofType': None } } } }, { 'args': [{ 'defaultValue': 'false', 'name': 'includeDeprecated', 'type': { 'kind': 'SCALAR', 'name': 'Boolean', 'ofType': None } }], 'deprecationReason': None, 'isDeprecated': False, 'name': 'enumValues', 'type': { 'kind': 'LIST', 'name': None, 'ofType': { 'kind': 'NON_NULL', 'name': None, 'ofType': { 'kind': 'OBJECT', 'name': '__EnumValue', 'ofType': None } } } }, { 'args': [], 'deprecationReason': None, 'isDeprecated': False, 'name': 'inputFields', 'type': { 'kind': 'LIST', 'name': None, 'ofType': { 'kind': 'NON_NULL', 'name': None, 'ofType': { 'kind': 'OBJECT', 'name': '__InputValue', 'ofType': None } } } }, { 'args': [], 'deprecationReason': None, 'isDeprecated': False, 'name': 'ofType', 'type': { 'kind': 'OBJECT', 'name': '__Type', 'ofType': None } }], 'inputFields': None, 'interfaces': [], 'kind': 'OBJECT', 'name': '__Type', 'possibleTypes': None }, { 'enumValues': [{ 'deprecationReason': None, 'isDeprecated': False, 'name': 'SCALAR' }, { 'deprecationReason': None, 'isDeprecated': False, 'name': 'OBJECT' }, { 'deprecationReason': None, 'isDeprecated': False, 'name': 'INTERFACE' }, { 'deprecationReason': None, 'isDeprecated': False, 'name': 'UNION' }, { 'deprecationReason': None, 'isDeprecated': False, 'name': 'ENUM' }, { 'deprecationReason': None, 'isDeprecated': False, 'name': 'INPUT_OBJECT' }, { 'deprecationReason': None, 'isDeprecated': False, 'name': 'LIST' }, { 'deprecationReason': None, 'isDeprecated': False, 'name': 'NON_NULL' }], 'fields': None, 'inputFields': None, 'interfaces': None, 'kind': 'ENUM', 'name': '__TypeKind', 'possibleTypes': None }, { 'enumValues': None, 'fields': None, 'inputFields': None, 'interfaces': None, 'kind': 'SCALAR', 'name': 'String', 'possibleTypes': None }, { 'enumValues': None, 'fields': None, 'inputFields': None, 'interfaces': None, 'kind': 'SCALAR', 'name': 'Boolean', 'possibleTypes': None }, { 'enumValues': None, 'fields': [{ 'args': [], 'deprecationReason': None, 'isDeprecated': False, 'name': 'name', 'type': { 'kind': 'NON_NULL', 'name': None, 'ofType': { 'kind': 'SCALAR', 'name': 'String', 'ofType': None } } }, { 'args': [], 'deprecationReason': None, 'isDeprecated': False, 'name': 'description', 'type': { 'kind': 'SCALAR', 'name': 'String', 'ofType': None } }, { 'args': [], 'deprecationReason': None, 'isDeprecated': False, 'name': 'args', 'type': { 'kind': 'NON_NULL', 'name': None, 'ofType': { 'kind': 'LIST', 'name': None, 'ofType': { 'kind': 'NON_NULL', 'name': None, 'ofType': { 'kind': 'OBJECT', 'name': '__InputValue' } } } } }, { 'args': [], 'deprecationReason': None, 'isDeprecated': False, 'name': 'type', 'type': { 'kind': 'NON_NULL', 'name': None, 'ofType': { 'kind': 'OBJECT', 'name': '__Type', 'ofType': None } } }, { 'args': [], 'deprecationReason': None, 'isDeprecated': False, 'name': 'isDeprecated', 'type': { 'kind': 'NON_NULL', 'name': None, 'ofType': { 'kind': 'SCALAR', 'name': 'Boolean', 'ofType': None } } }, { 'args': [], 'deprecationReason': None, 'isDeprecated': False, 'name': 'deprecationReason', 'type': { 'kind': 'SCALAR', 'name': 'String', 'ofType': None } }], 'inputFields': None, 'interfaces': [], 'kind': 'OBJECT', 'name': '__Field', 'possibleTypes': None }, { 'enumValues': None, 'fields': [{ 'args': [], 'deprecationReason': None, 'isDeprecated': False, 'name': 'name', 'type': { 'kind': 'NON_NULL', 'name': None, 'ofType': { 'kind': 'SCALAR', 'name': 'String', 'ofType': None } } }, { 'args': [], 'deprecationReason': None, 'isDeprecated': False, 'name': 'description', 'type': { 'kind': 'SCALAR', 'name': 'String', 'ofType': None } }, { 'args': [], 'deprecationReason': None, 'isDeprecated': False, 'name': 'type', 'type': { 'kind': 'NON_NULL', 'name': None, 'ofType': { 'kind': 'OBJECT', 'name': '__Type', 'ofType': None } } }, { 'args': [], 'deprecationReason': None, 'isDeprecated': False, 'name': 'defaultValue', 'type': { 'kind': 'SCALAR', 'name': 'String', 'ofType': None } }], 'inputFields': None, 'interfaces': [], 'kind': 'OBJECT', 'name': '__InputValue', 'possibleTypes': None }, { 'enumValues': None, 'fields': [{ 'args': [], 'deprecationReason': None, 'isDeprecated': False, 'name': 'name', 'type': { 'kind': 'NON_NULL', 'name': None, 'ofType': { 'kind': 'SCALAR', 'name': 'String', 'ofType': None } } }, { 'args': [], 'deprecationReason': None, 'isDeprecated': False, 'name': 'description', 'type': { 'kind': 'SCALAR', 'name': 'String', 'ofType': None } }, { 'args': [], 'deprecationReason': None, 'isDeprecated': False, 'name': 'isDeprecated', 'type': { 'kind': 'NON_NULL', 'name': None, 'ofType': { 'kind': 'SCALAR', 'name': 'Boolean', 'ofType': None } } }, { 'args': [], 'deprecationReason': None, 'isDeprecated': False, 'name': 'deprecationReason', 'type': { 'kind': 'SCALAR', 'name': 'String', 'ofType': None } }], 'inputFields': None, 'interfaces': [], 'kind': 'OBJECT', 'name': '__EnumValue', 'possibleTypes': None }, { 'enumValues': None, 'fields': [{ 'args': [], 'deprecationReason': None, 'isDeprecated': False, 'name': 'name', 'type': { 'kind': 'NON_NULL', 'name': None, 'ofType': { 'kind': 'SCALAR', 'name': 'String', 'ofType': None } } }, { 'args': [], 'deprecationReason': None, 'isDeprecated': False, 'name': 'description', 'type': { 'kind': 'SCALAR', 'name': 'String', 'ofType': None } }, { 'args': [], 'deprecationReason': None, 'isDeprecated': False, 'name': 'args', 'type': { 'kind': 'NON_NULL', 'name': None, 'ofType': { 'kind': 'LIST', 'name': None, 'ofType': { 'kind': 'NON_NULL', 'name': None, 'ofType': { 'kind': 'OBJECT', 'name': '__InputValue' } } } } }, { 'args': [], 'deprecationReason': None, 'isDeprecated': False, 'name': 'onOperation', 'type': { 'kind': 'SCALAR', 'name': 'Boolean', 'ofType': None } }, { 'args': [], 'deprecationReason': None, 'isDeprecated': False, 'name': 'onFragment', 'type': { 'kind': 'SCALAR', 'name': 'Boolean', 'ofType': None } }, { 'args': [], 'deprecationReason': None, 'isDeprecated': False, 'name': 'onField', 'type': { 'kind': 'SCALAR', 'name': 'Boolean', 'ofType': None } }], 'inputFields': None, 'interfaces': [], 'kind': 'OBJECT', 'name': '__Directive', 'possibleTypes': None }] } })
GraphQLField(type=ColorType, args={'color': GraphQLArgument(ColorType)}, resolver=lambda value, args, info: args.get('color')) }) SubscriptionType = GraphQLObjectType( name='Subscription', fields={ 'subscribeToEnum': GraphQLField(type=ColorType, args={'color': GraphQLArgument(ColorType)}, resolver=lambda value, args, info: args.get('color')) }) Schema = GraphQLSchema(query=QueryType, mutation=MutationType, subscription=SubscriptionType) def test_accepts_enum_literals_as_input(): result = graphql(Schema, '{ colorInt(fromEnum: GREEN) }') assert not result.errors assert result.data == {'colorInt': 1} def test_enum_may_be_output_type(): result = graphql(Schema, '{ colorEnum(fromInt: 1) }') assert not result.errors assert result.data == {'colorEnum': 'GREEN'}
def test_introspects_on_input_object(): TestInputObject = GraphQLInputObjectType( 'TestInputObject', { 'a': GraphQLInputObjectField(GraphQLString, default_value='foo'), 'b': GraphQLInputObjectField(GraphQLList(GraphQLString)), }) TestType = GraphQLObjectType( 'TestType', { 'field': GraphQLField(type=GraphQLString, args={'complex': GraphQLArgument(TestInputObject)}, resolver=lambda obj, args, info: json.dumps( args.get('complex'))) }) schema = GraphQLSchema(TestType) request = ''' { __schema { types { kind name inputFields { name type { ...TypeRef } defaultValue } } } } fragment TypeRef on __Type { kind name ofType { kind name ofType { kind name ofType { kind name } } } } ''' result = graphql(schema, request) assert not result.errors assert sort_lists({'kind': 'INPUT_OBJECT', 'name': 'TestInputObject', 'inputFields': [{'name': 'a', 'type': {'kind': 'SCALAR', 'name': 'String', 'ofType': None}, 'defaultValue': '"foo"'}, {'name': 'b', 'type': {'kind': 'LIST', 'name': None, 'ofType': {'kind': 'SCALAR', 'name': 'String', 'ofType': None}}, 'defaultValue': None}]}) in \ sort_lists(result.data['__schema']['types'])
def test_executes_arbitary_code(): class Data(object): a = 'Apple' b = 'Banana' c = 'Cookie' d = 'Donut' e = 'Egg' f = 'Fish' def pic(self, size=50): return 'Pic of size: {}'.format(size) def deep(self): return DeepData() def promise(self): # FIXME: promise is unsupported return Data() class DeepData(object): a = 'Already Been Done' b = 'Boring' c = ['Contrived', None, 'Confusing'] def deeper(self): return [Data(), None, Data()] doc = ''' query Example($size: Int) { a, b, x: c ...c f ...on DataType { pic(size: $size) promise { a } } deep { a b c deeper { a b } } } fragment c on DataType { d e } ''' ast = parse(doc) expected = { 'a': 'Apple', 'b': 'Banana', 'x': 'Cookie', 'd': 'Donut', 'e': 'Egg', 'f': 'Fish', 'pic': 'Pic of size: 100', 'promise': {'a': 'Apple'}, 'deep': { 'a': 'Already Been Done', 'b': 'Boring', 'c': ['Contrived', None, 'Confusing'], 'deeper': [ {'a': 'Apple', 'b': 'Banana'}, None, {'a': 'Apple', 'b': 'Banana'}]} } DataType = GraphQLObjectType('DataType', lambda: { 'a': GraphQLField(GraphQLString), 'b': GraphQLField(GraphQLString), 'c': GraphQLField(GraphQLString), 'd': GraphQLField(GraphQLString), 'e': GraphQLField(GraphQLString), 'f': GraphQLField(GraphQLString), 'pic': GraphQLField( args={'size': GraphQLArgument(GraphQLInt)}, type=GraphQLString, resolver=lambda obj, args, *_: obj.pic(args['size']), ), 'deep': GraphQLField(DeepDataType), 'promise': GraphQLField(DataType), }) DeepDataType = GraphQLObjectType('DeepDataType', { 'a': GraphQLField(GraphQLString), 'b': GraphQLField(GraphQLString), 'c': GraphQLField(GraphQLList(GraphQLString)), 'deeper': GraphQLField(GraphQLList(DataType)), }) schema = GraphQLSchema(query=DataType) result = execute(schema, Data(), ast, 'Example', {'size': 100}) assert not result.errors assert result.data == expected
def nest(self): return NullingData() def nonNullNest(self): return NullingData() DataType = GraphQLObjectType( 'DataType', lambda: { 'sync': GraphQLField(GraphQLString), 'nonNullSync': GraphQLField(GraphQLNonNull(GraphQLString)), 'nest': GraphQLField(DataType), 'nonNullNest': GraphQLField(GraphQLNonNull(DataType)), }) schema = GraphQLSchema(DataType) def test_nulls_a_nullable_field_that_throws_sync(): doc = ''' query Q { sync } ''' ast = parse(doc) result = execute(schema, ThrowingData(), ast, 'Q', {}) assert len(result.errors) == 1 # TODO: check error location assert result.errors[0]['message'] == sync_error.message assert result.data == {'sync': None}
resolver=lambda payload, *_: getShip(payload.shipId) ), 'faction': GraphQLField( factionType, resolver=lambda payload, *_: getFaction(payload.factionId) ) }, mutate_and_get_payload=mutate_and_get_payload ) # This is the type that will be the root of our mutations, and the # entry point into performing writes in our schema. # # This implements the following type system shorthand: # type Mutation { # introduceShip(input IntroduceShipInput!): IntroduceShipPayload # } mutationType = GraphQLObjectType( 'Mutation', fields=lambda: { 'introduceShip': shipMutation } ) # Finally, we construct our schema (whose starting query type is the query # type we defined above) and export it. StarWarsSchema = GraphQLSchema( query=queryType, mutation=mutationType )