def test_does_not_allow_non_null_lists_of_non_nulls_to_contain_null(): doc = ''' query q($input:[String!]!) { nnListNN(input: $input) } ''' ast = parse(doc) with raises(GraphQLError) as excinfo: execute(schema, None, ast, None, {'input': ['A', None, 'B']}) e = excinfo.value
def test_does_not_allow_non_null_lists_to_be_null(): doc = ''' query q($input:[String]!) { nnList(input: $input) } ''' ast = parse(doc) with raises(GraphQLError) as excinfo: execute(schema, None, ast, None, {'input': None}) e = excinfo.value
def test_does_not_allow_non_nullable_inputs_to_be_set_to_null_in_a_variable(): doc = ''' query SetsNonNullable($value: String!) { fieldWithNonNullableStringInput(input: $value) } ''' ast = parse(doc) with raises(GraphQLError) as excinfo: execute(schema, None, ast, None, {'value': None}) e = excinfo.value
def test_does_not_allow_unknown_types_to_be_used_as_values(): doc = ''' query q($input: UnknownType!) { fieldWithObjectInput(input: $input) } ''' ast = parse(doc) with raises(GraphQLError) as excinfo: execute(schema, None, ast) e = excinfo.value
def test_errors_on_incorrect_type(): doc = ''' query q($input:TestInputObject) { fieldWithObjectInput(input: $input) } ''' params = {'input': 'foo bar'} ast = parse(doc) with raises(GraphQLError) as excinfo: execute(schema, None, ast, None, params) e = excinfo.value
def test_errors_on_addition_of_unknown_input_field(): doc = ''' query q($input:TestInputObject) { fieldWithObjectInput(input: $input) } ''' params = {'input': {'a': 'foo', 'b': 'bar', 'c': 'baz', 'd': 'dog'}} ast = parse(doc) with raises(GraphQLError) as excinfo: execute(schema, None, ast, None, params) e = excinfo.value
def test_errors_on_omission_of_nested_non_null(): doc = ''' query q($input:TestInputObject) { fieldWithObjectInput(input: $input) } ''' params = {'input': {'a': 'foo', 'b': 'bar'}} ast = parse(doc) with raises(GraphQLError) as excinfo: execute(schema, None, ast, None, params) e = excinfo.value
def test_raises_the_inline_operation_if_no_operation_is_provided(): doc = 'query Example { a } query OtherExample { a }' class Data(object): a = 'b' ast = parse(doc) Type = GraphQLObjectType('Type', { 'a': GraphQLField(GraphQLString) }) with raises(GraphQLError) as excinfo: execute(GraphQLSchema(Type), Data(), ast) assert 'Must provide operation name if query contains multiple operations' in str(excinfo.value)
def test_raises_the_inline_operation_if_no_operation_is_provided(): doc = 'query Example { a } query OtherExample { a }' class Data(object): a = 'b' ast = parse(doc) Type = GraphQLObjectType('Type', {'a': GraphQLField(GraphQLString)}) with raises(GraphQLError) as excinfo: execute(GraphQLSchema(Type), Data(), ast) assert 'Must provide operation name if query contains multiple operations' in str( excinfo.value)
def test_fails_to_execute_a_query_containing_a_type_definition(): query = parse(''' { foo } type Query { foo: String } ''') schema = GraphQLSchema( GraphQLObjectType(name='Query', fields={'foo': GraphQLField(GraphQLString)})) with raises(GraphQLError) as excinfo: execute(schema, None, query) assert excinfo.value.message == 'GraphQL cannot execute a request containing a ObjectTypeDefinition.'
def test_fails_when_an_is_type_of_check_is_not_met(): class Special(object): def __init__(self, value): self.value = value class NotSpecial(object): def __init__(self, value): self.value = value SpecialType = GraphQLObjectType( "SpecialType", fields={"value": GraphQLField(GraphQLString)}, is_type_of=lambda obj, info: isinstance(obj, Special), ) schema = GraphQLSchema( GraphQLObjectType( name="Query", fields={"specials": GraphQLField(GraphQLList(SpecialType), resolver=lambda root, *_: root["specials"])}, ) ) query = parse("{ specials { value } }") value = {"specials": [Special("foo"), NotSpecial("bar")]} result = execute(schema, value, query) assert result.data == {"specials": [{"value": "foo"}, None]} assert 'Expected value of type "SpecialType" but got NotSpecial.' in str(result.errors)
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_when_argument_provided_cannot_be_coerced(): ast = parse('''{ fieldWithDefaultArgumentValue(input: WRONG_TYPE) }''') result = execute(schema, None, ast) assert not result.errors assert result.data == {'fieldWithDefaultArgumentValue': '"Hello World"'}
def test_when_nullable_variable_provided(): ast = parse('''query optionalVariable($optional: String) { fieldWithDefaultArgumentValue(input: $optional) }''') result = execute(schema, None, ast) assert not result.errors assert result.data == {'fieldWithDefaultArgumentValue': '"Hello World"'}
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_fails_when_an_is_type_of_check_is_not_met(): class Special(object): def __init__(self, value): self.value = value class NotSpecial(object): def __init__(self, value): self.value = value SpecialType = GraphQLObjectType( 'SpecialType', fields={ 'value': GraphQLField(GraphQLString), }, is_type_of=lambda obj, info: isinstance(obj, Special)) schema = GraphQLSchema( GraphQLObjectType(name='Query', fields={ 'specials': GraphQLField( GraphQLList(SpecialType), resolver=lambda root, *_: root['specials']) })) query = parse('{ specials { value } }') value = {'specials': [Special('foo'), NotSpecial('bar')]} result = execute(schema, value, query) assert result.data == {'specials': [{'value': 'foo'}, None]} assert 'Expected value of type "SpecialType" but got NotSpecial.' in str( result.errors)
def test_executes_using_union_types(): # NOTE: This is an *invalid* query, but it should be an *executable* query. ast = parse(''' { __typename name pets { __typename name barks meows } } ''') result = execute(schema, john, ast) assert not result.errors assert result.data == { '__typename': 'Person', 'name': 'John', 'pets': [{ '__typename': 'Cat', 'name': 'Garfield', 'meows': False }, { '__typename': 'Dog', 'name': 'Odie', 'barks': True }] }
def test_correctly_threads_arguments(): doc = """ query Example { b(numArg: 123, stringArg: "foo") } """ def resolver(_, args, *_args): assert args["numArg"] == 123 assert args["stringArg"] == "foo" resolver.got_here = True resolver.got_here = False doc_ast = parse(doc) Type = GraphQLObjectType( "Type", { "b": GraphQLField( GraphQLString, args={"numArg": GraphQLArgument(GraphQLInt), "stringArg": GraphQLArgument(GraphQLString)}, resolver=resolver, ) }, ) result = execute(GraphQLSchema(Type), None, doc_ast, "Example", {}) assert not result.errors assert resolver.got_here
def test_executes_using_interface_types(): # NOTE: This is an *invalid* query, but it should be an *executable* query. ast = parse(''' { __typename name friends { __typename name barks meows } } ''') result = execute(schema, john, ast) assert not result.errors assert result.data == { '__typename': 'Person', 'name': 'John', 'friends': [{ '__typename': 'Person', 'name': 'Liz' }, { '__typename': 'Dog', 'name': 'Odie', 'barks': True }] }
def test_executes_union_types_with_inline_fragment(): # This is the valid version of the query in the above test. ast = parse(''' { __typename name pets { __typename ... on Dog { name barks } ... on Cat { name meows } } } ''') result = execute(schema, john, ast) assert not result.errors assert result.data == { '__typename': 'Person', 'name': 'John', 'pets': [ {'__typename': 'Cat', 'name': 'Garfield', 'meows': False}, {'__typename': 'Dog', 'name': 'Odie', 'barks': True} ] }
def test_executes_interface_types_with_inline_fragment(): # This is the valid version of the query in the above test. ast = parse(''' { __typename name friends { __typename name ... on Dog { barks } ... on Cat { meows } } } ''') result = execute(schema, john, ast) assert not result.errors assert result.data == { '__typename': 'Person', 'name': 'John', 'friends': [{ '__typename': 'Person', 'name': 'Liz' }, { '__typename': 'Dog', 'name': 'Odie', 'barks': True }] }
def test_nulls_a_complex_tree_of_nullable_fields_that_returns_null(): doc = ''' query Q { nest { sync #promise nest { sync #promise } #promiseNest { # sync # promise #} } #promiseNest { # sync # promise # nest { # sync # promise # } # promiseNest { # sync # promise # } #} } ''' ast = parse(doc) result = execute(schema, NullingData(), ast, 'Q', {}) assert not result.errors assert result.data == {'nest': {'sync': None, 'nest': {'sync': None}}}
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 }, fifth: immediatelyChangeTheNumber(newNumber: 5) { theNumber } }''' ast = parse(doc) result = execute(schema, Root(6), ast, 'M') assert not result.errors assert result.data == \ { 'first': {'theNumber': 1}, 'second': {'theNumber': 2}, 'third': {'theNumber': 3}, 'fourth': {'theNumber': 4}, 'fifth': {'theNumber': 5}, }
def test_correctly_threads_arguments(): doc = ''' query Example { b(numArg: 123, stringArg: "foo") } ''' def resolver(_, args, *_args): assert args['numArg'] == 123 assert args['stringArg'] == 'foo' resolver.got_here = True resolver.got_here = False doc_ast = parse(doc) Type = GraphQLObjectType('Type', { 'b': GraphQLField( GraphQLString, args={ 'numArg': GraphQLArgument(GraphQLInt), 'stringArg': GraphQLArgument(GraphQLString), }, resolver=resolver), }) result = execute(GraphQLSchema(Type), None, doc_ast, 'Example', {}) assert not result.errors assert resolver.got_here
def test_supports_the_type_root_field(): TestType = GraphQLObjectType("TestType", {"testField": GraphQLField(GraphQLString)}) schema = GraphQLSchema(TestType) request = '{ __type(name: "TestType") { name } }' result = execute(schema, object(), parse(request)) assert not result.errors assert result.data == {"__type": {"name": "TestType"}}
def test_executes_interface_types_with_inline_fragment(): # This is the valid version of the query in the above test. ast = parse(''' { __typename name friends { __typename name ... on Dog { barks } ... on Cat { meows } } } ''') result = execute(schema, john, ast) assert not result.errors assert result.data == { '__typename': 'Person', 'name': 'John', 'friends': [ {'__typename': 'Person', 'name': 'Liz'}, {'__typename': 'Dog', 'name': 'Odie', 'barks': True} ] }
def test_does_not_include_illegal_fields_in_output(): doc = "mutation M { thisIsIllegalDontIncludeMe }" ast = parse(doc) Q = GraphQLObjectType("Q", {"a": GraphQLField(GraphQLString)}) M = GraphQLObjectType("M", {"c": GraphQLField(GraphQLString)}) result = execute(GraphQLSchema(Q, M), None, ast) assert not result.errors assert result.data == {}
def test_supports_the_type_root_field(): TestType = GraphQLObjectType('TestType', {'testField': GraphQLField(GraphQLString)}) schema = GraphQLSchema(TestType) request = '{ __type(name: "TestType") { name } }' result = execute(schema, object(), parse(request)) assert not result.errors assert result.data == {'__type': {'name': 'TestType'}}
def test_does_not_include_illegal_fields_in_output(): doc = 'mutation M { thisIsIllegalDontIncludeMe }' ast = parse(doc) Q = GraphQLObjectType('Q', {'a': GraphQLField(GraphQLString)}) M = GraphQLObjectType('M', {'c': GraphQLField(GraphQLString)}) result = execute(GraphQLSchema(Q, M), None, ast) assert not result.errors assert result.data == {}
def test_supports_the_type_root_field(): TestType = GraphQLObjectType('TestType', { 'testField': GraphQLField(GraphQLString) }) schema = GraphQLSchema(TestType) request = '{ __type(name: "TestType") { name } }' result = execute(schema, object(), parse(request)) assert not result.errors assert result.data == {'__type': {'name': 'TestType'}}
def test_can_introspect_on_union_and_intersection_types(): ast = parse(''' { Named: __type(name: "Named") { kind name fields { name } interfaces { name } possibleTypes { name } enumValues { name } inputFields { name } } Pet: __type(name: "Pet") { kind name fields { name } interfaces { name } possibleTypes { name } enumValues { name } inputFields { name } } }''') result = execute(schema, None, ast) assert result.data == { 'Named': { 'enumValues': None, 'name': 'Named', 'kind': 'INTERFACE', 'interfaces': None, 'fields': [{ 'name': 'name' }], 'possibleTypes': [{ 'name': 'Dog' }, { 'name': 'Cat' }, { 'name': 'Person' }], 'inputFields': None }, 'Pet': { 'enumValues': None, 'name': 'Pet', 'kind': 'UNION', 'interfaces': None, 'fields': None, 'possibleTypes': [{ 'name': 'Dog' }, { 'name': 'Cat' }], 'inputFields': None } }
def test_allows_lists_to_contain_values(): doc = ''' query q($input:[String]) { list(input: $input) } ''' ast = parse(doc) result = execute(schema, None, ast, None, {'input': ['A']}) assert not result.errors assert result.data == {'list': '["A"]'}
def test_nulls_the_top_level_if_sync_non_nullable_field_returns_null(): doc = ''' query Q { nonNullSync } ''' ast = parse(doc) result = execute(schema, NullingData(), ast) assert result.data is None assert len(result.errors) == 1 # TODO: check error location assert result.errors[0]['message'] == 'Cannot return null for non-nullable type.'
def test_nulls_the_top_level_if_sync_non_nullable_field_throws(): doc = ''' query Q { nonNullSync } ''' ast = parse(doc) result = execute(schema, ThrowingData(), ast) assert result.data is None assert len(result.errors) == 1 # TODO: check error location assert result.errors[0]['message'] == non_null_sync_error.message
def test_allows_non_null_lists_to_contain_null(): doc = ''' query q($input:[String]!) { nnList(input: $input) } ''' ast = parse(doc) result = execute(schema, None, ast, None, {'input': ['A', None, 'B']}) assert not result.errors assert result.data == {'nnList': '["A", null, "B"]'}
def test_allows_lists_of_non_nulls_to_be_null(): doc = ''' query q($input:[String!]) { listNN(input: $input) } ''' ast = parse(doc) result = execute(schema, None, ast, None, {'input': None}) assert not result.errors assert result.data == {'listNN': 'null'}
def test_nulls_the_top_level_if_sync_non_nullable_field_throws(): doc = ''' query Q { nonNullSync } ''' ast = parse(doc) result = execute(schema, ThrowingData(), ast) assert result.data is None assert len(result.errors) == 1 # TODO: check error location assert result.errors[0]['message'] == str(non_null_sync_error)
def test_allows_non_null_lists_of_non_nulls_to_contain_values(): doc = ''' query q($input:[String!]!) { nnListNN(input: $input) } ''' ast = parse(doc) result = execute(schema, None, ast, None, {'input': ['A']}) assert not result.errors assert result.data == {'nnListNN': '["A"]'}
def test_inline_does_not_use_incorrect_value(): doc = ''' { fieldWithObjectInput(input: ["foo", "bar", "baz"]) } ''' ast = parse(doc) result = execute(schema, None, ast) assert not result.errors assert json.loads(result.data['fieldWithObjectInput']) is None
def test_nulls_a_nullable_field_that_returns_null(): doc = ''' query Q { sync } ''' ast = parse(doc) result = execute(schema, NullingData(), ast, 'Q', {}) assert not result.errors assert result.data == {'sync': None}
def test_inline_executes_with_complex_input(): doc = ''' { fieldWithObjectInput(input: {a: "foo", b: ["bar"], c: "baz"}) } ''' ast = parse(doc) result = execute(schema, None, ast) assert not result.errors assert json.loads(result.data['fieldWithObjectInput']) == \ {"a": "foo", "b": ["bar"], "c": "baz"}
def test_passes_along_null_for_non_nullable_inputs_if_explicitly_set_in_the_query(): doc = ''' { fieldWithNonNullableStringInput } ''' ast = parse(doc) result = execute(schema, None, ast) assert not result.errors assert result.data == \ {"fieldWithNonNullableStringInput": 'null'}
def test_inline_properly_coerces_single_value_to_array(): doc = ''' { fieldWithObjectInput(input: {a: "foo", b: "bar", c: "baz"}) } ''' ast = parse(doc) result = execute(schema, None, ast) assert not result.errors assert json.loads(result.data['fieldWithObjectInput']) == \ {"a": "foo", "b": ["bar"], "c": "baz"}