def test_cannot_use_client_schema_for_general_execution(): customScalar = GraphQLScalarType(name='CustomScalar', serialize=lambda: None) schema = GraphQLSchema(query=GraphQLObjectType( name='Query', fields={ 'foo': GraphQLField(GraphQLString, args=OrderedDict([('custom1', GraphQLArgument(customScalar)), ('custom2', GraphQLArgument(customScalar))])) })) introspection = graphql(schema, introspection_query) client_schema = build_client_schema(introspection.data) class data: foo = 'bar' result = graphql( client_schema, 'query NoNo($v: CustomScalar) { foo(custom1: 123, custom2: $v) }', data, {'v': 'baz'}) assert result.data == {'foo': None} assert [format_error(e) for e in result.errors] == [{ 'locations': [{ 'column': 32, 'line': 1 }], 'message': 'Client Schema cannot be used for execution.' }]
def run_check(self): test_type = self.type data = Data(test=test_data) DataType = GraphQLObjectType( name='DataType', fields=lambda: { 'test': GraphQLField(test_type), 'nest': GraphQLField(DataType, resolver=lambda *_: data) }) schema = GraphQLSchema(query=DataType) response = executor.execute(schema, ast, data) assert response.called response = response.result if response.errors: result = { 'data': response.data, 'errors': [format_error(e) for e in response.errors] } else: result = {'data': response.data} assert result == expected
def run_check(self): test_type = self.type data = Data(test=test_data) DataType = GraphQLObjectType( name='DataType', fields=lambda: { 'test': GraphQLField(test_type), 'nest': GraphQLField(DataType, resolver=lambda *_: data) } ) schema = GraphQLSchema(query=DataType) response = executor.execute(schema, ast, data) assert response.called response = response.result if response.errors: result = { 'data': response.data, 'errors': [format_error(e) for e in response.errors] } else: result = { 'data': response.data } assert result == expected
def test_parse_error(): query = ''' qeury ''' result = graphql(StarWarsSchema, query) assert result.invalid formatted_error = format_error(result.errors[0]) assert formatted_error['locations'] == [{'column': 9, 'line': 2}] assert 'Syntax Error GraphQL request (2:9) Unexpected Name "qeury"' in formatted_error['message'] assert result.data is None
def test_parse_error(): query = """ qeury """ result = graphql(StarWarsSchema, query) assert result.invalid formatted_error = format_error(result.errors[0]) assert formatted_error["locations"] == [{"column": 9, "line": 2}] assert 'Syntax Error GraphQL request (2:9) Unexpected Name "qeury"' in formatted_error["message"] assert result.data is None
def test_errors_on_addition_of_unknown_input_field(self): params = {'input': {'a': 'foo', 'b': 'bar', 'c': 'baz', 'f': 'dog'}} with raises(GraphQLError) as excinfo: check(self.doc, {}, params) assert format_error(excinfo.value) == { 'locations': [{'column': 13, 'line': 2}], 'message': 'Variable "$input" got invalid value {}.\n' 'In field "f": Unknown field.'.format(stringify(params['input'])) }
def test_errors_on_addition_of_input_field_of_incorrect_type(self): params = {'input': {'a': 'foo', 'b': 'bar', 'c': 'baz', 'd': 'dog'}} with raises(GraphQLError) as excinfo: check(self.doc, {}, params) assert format_error(excinfo.value) == { 'locations': [{'column': 13, 'line': 2}], 'message': 'Variable "$input" got invalid value {}.\n' 'In field "d": Expected type "ComplexScalar", found "dog".'.format(stringify(params['input'])) }
def test_errors_on_omission_of_nested_non_null(self): params = {'input': {'a': 'foo', 'b': 'bar'}} with raises(GraphQLError) as excinfo: check(self.doc, {}, params) assert format_error(excinfo.value) == { 'locations': [{'column': 13, 'line': 2}], 'message': 'Variable "$input" got invalid value {}.\n' 'In field "c": Expected "String!", found null.'.format(stringify(params['input'])) }
def test_errors_on_incorrect_type(self): params = {'input': 'foo bar'} with raises(GraphQLError) as excinfo: check(self.doc, {}, params) assert format_error(excinfo.value) == { 'locations': [{'column': 13, 'line': 2}], 'message': 'Variable "$input" got invalid value {}.\n' 'Expected "TestInputObject", found not an object.'.format(stringify(params['input'])) }
def test_errors_on_addition_of_unknown_input_field(self): params = {'input': {'a': 'foo', 'b': 'bar', 'c': 'baz', 'd': 'dog'}} with raises(GraphQLError) as excinfo: check(self.doc, {}, params) assert format_error(excinfo.value) == { 'locations': [{'column': 13, 'line': 2}], 'message': 'Variable "$input" expected value of type "TestInputObject" but got: ' '{}.'.format(stringify(params['input'])) }
def test_errors_on_incorrect_type(self): params = {'input': 'foo bar'} with raises(GraphQLError) as excinfo: check(self.doc, {}, params) assert format_error(excinfo.value) == { 'locations': [{'column': 13, 'line': 2}], 'message': 'Variable "$input" expected value of type "TestInputObject" but got: ' '{}.'.format(stringify(params['input'])) }
def test_does_not_allow_non_null_lists_of_non_nulls_to_be_null(): doc = ''' query q($input: [String!]!) { nnListNN(input: $input) } ''' with raises(GraphQLError) as excinfo: check(doc, {}, {'input': None}) assert format_error(excinfo.value) == { 'locations': [{'column': 13, 'line': 2}], 'message': 'Variable "$input" of required type "[String!]!" was not provided.' }
def test_does_not_allow_non_nullable_inputs_to_be_omitted_in_a_variable(): doc = ''' query SetsNonNullable($value: String!) { fieldWithNonNullableStringInput(input: $value) } ''' with raises(GraphQLError) as excinfo: check(doc, {}) assert format_error(excinfo.value) == { 'locations': [{'column': 27, 'line': 2}], 'message': 'Variable "$value" of required type "String!" was not provided.' }
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} assert result == expected
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 } assert result == expected
def test_does_not_allow_unknown_types_to_be_used_as_values(): doc = ''' query q($input: UnknownType!) { fieldWithObjectInput(input: $input) } ''' params = {'input': 'whoknows'} with raises(GraphQLError) as excinfo: check(doc, {}, params) assert format_error(excinfo.value) == { 'locations': [{'column': 13, 'line': 2}], 'message': 'Variable "$input" expected value of type "UnknownType!" which cannot be used as an input type.' }
def test_fails_as_expected_on_the_type_root_field_without_an_arg(): TestType = GraphQLObjectType("TestType", {"testField": GraphQLField(GraphQLString)}) schema = GraphQLSchema(TestType) request = """ { __type { name } }""" result = graphql(schema, request) expected_error = { "message": ProvidedNonNullArguments.missing_field_arg_message("__type", "name", "String!"), "locations": [dict(line=3, column=9)], } assert expected_error in [format_error(error) for error in result.errors]
def test_fails_as_expected_on_the_type_root_field_without_an_arg(): TestType = GraphQLObjectType('TestType', { 'testField': GraphQLField(GraphQLString) }) schema = GraphQLSchema(TestType) request = ''' { __type { name } }''' result = graphql(schema, request) expected_error = {'message': ProvidedNonNullArguments.missing_field_arg_message('__type', 'name', 'String!'), 'locations': [dict(line=3, column=9)]} assert (expected_error in [format_error(error) for error in result.errors])
def check(doc, data, expected): ast = parse(doc) response = executor.execute(schema, ast, data) assert response.called response = response.result if response.errors: result = { 'data': response.data, 'errors': [format_error(e) for e in response.errors] } else: result = {'data': response.data} assert result == expected
def test_does_not_allow_non_null_lists_of_non_nulls_to_contain_null(): doc = ''' query q($input: [String!]!) { nnListNN(input: $input) } ''' params = {'input': ['A', None, 'B']} with raises(GraphQLError) as excinfo: check(doc, {}, params) assert format_error(excinfo.value) == { 'locations': [{'column': 13, 'line': 2}], 'message': 'Variable "$input" expected value of type "[String!]!" but got: ' '{}.'.format(stringify(params['input'])) }
def test_errors_on_deep_nested_errors_and_with_many_errors(self): nested_doc = ''' query q($input: TestNestedInputObject) { fieldWithNestedObjectInput(input: $input) } ''' params = {'input': {'na': {'a': 'foo'}}} with raises(GraphQLError) as excinfo: check(nested_doc, {}, params) assert format_error(excinfo.value) == { 'locations': [{'column': 19, 'line': 2}], 'message': 'Variable "$input" got invalid value {}.\n' 'In field "na": In field "c": Expected "String!", found null.\n' 'In field "nb": Expected "String!", found null.'.format(stringify(params['input'])) }
def test_does_not_allow_non_null_lists_of_non_nulls_to_contain_null(): doc = ''' query q($input: [String!]!) { nnListNN(input: $input) } ''' params = {'input': ['A', None, 'B']} with raises(GraphQLError) as excinfo: check(doc, {}, params) assert format_error(excinfo.value) == { 'locations': [{'column': 13, 'line': 2}], 'message': 'Variable "$input" got invalid value {}.\n' 'In element #1: Expected "String!", found null.'.format(stringify(params['input'])) }
def check(doc, data, expected): ast = parse(doc) response = executor.execute(schema, ast, data) assert response.called response = response.result if response.errors: result = { 'data': response.data, 'errors': [format_error(e) for e in response.errors] } else: result = { 'data': response.data } assert result == expected
def test_does_not_allow_lists_of_non_nulls_to_contain_null(): doc = ''' query q($input: [String!]) { listNN(input: $input) } ''' params = {'input': ['A', None, 'B']} with raises(GraphQLError) as excinfo: check(doc, {}, params) assert format_error(excinfo.value) == { 'locations': [{'column': 13, 'line': 2}], 'message': 'Variable "$input" got invalid value {}.\n' 'In element #1: Expected "String!", found null.'.format(stringify(params['input'])) }
def test_cannot_use_client_schema_for_general_execution(): customScalar = GraphQLScalarType( name='CustomScalar', serialize=lambda: None ) schema = GraphQLSchema( query=GraphQLObjectType( name='Query', fields={ 'foo': GraphQLField( GraphQLString, args=OrderedDict([ ('custom1', GraphQLArgument(customScalar)), ('custom2', GraphQLArgument(customScalar)) ]) ) } ) ) introspection = graphql(schema, introspection_query) client_schema = build_client_schema(introspection.data) class data: foo = 'bar' result = graphql( client_schema, 'query NoNo($v: CustomScalar) { foo(custom1: 123, custom2: $v) }', data, {'v': 'baz'} ) assert result.data == {'foo': None} assert [format_error(e) for e in result.errors] == [ {'locations': [{'column': 32, 'line': 1}], 'message': 'Client Schema cannot be used for execution.'} ]
def handle_invalid_graph_error(graphql_error): error_message = format_error(graphql_error) logger.error(error_message) return {'error': error_message}, status.HTTP_400_BAD_REQUEST
def form_error(error): if isinstance(error, GraphQLError): return format_error(error) return error
try: query, variables, operation_name = get_graphql_params( request, data) except Error, e: return error_response(e, pretty) result = graphql(schema, query, root_value, variables, operation_name) if result.invalid: status = 400 else: status = 200 d = {'data': result.data} if result.errors: d['errors'] = [ format_error(error) if hasattr(error, 'locations') else {type(error).__name__: error.message} for error in result.errors ] return Response(status=status, content_type='application/json', body=json_dump(d, pretty)) return handle def graphql_wsgi(schema, root_value=None, pretty=None): def get_options(request): return schema, root_value, pretty return graphql_wsgi_dynamic(get_options)