11from math import nan
22from typing import Any , Dict , Optional
33
4+ from graphql .error import GraphQLError
45from graphql .execution import ExecutionResult , execute_sync
56from graphql .execution .values import get_variable_values
67from graphql .language import OperationDefinitionNode , StringValueNode , ValueNode , parse
2122 GraphQLString ,
2223)
2324
25+ TestFaultyScalarGraphQLError = GraphQLError (
26+ "FaultyScalarErrorMessage" , extensions = {"code" : "FaultyScalarExtensionCode" }
27+ )
28+
29+
30+ def faulty_parse_value (value : str ) -> str :
31+ raise TestFaultyScalarGraphQLError
32+
33+
34+ def faulty_parse_literal (ast : ValueNode , _variables = None ) -> str :
35+ raise TestFaultyScalarGraphQLError
36+
37+
38+ TestFaultyScalar = GraphQLScalarType (
39+ name = "FaultyScalar" ,
40+ parse_value = faulty_parse_value ,
41+ parse_literal = faulty_parse_literal ,
42+ )
43+
2444
2545def parse_serialized_value (value : str ) -> str :
2646 assert value == "SerializedValue"
@@ -47,6 +67,7 @@ def parse_literal_value(ast: ValueNode, _variables=None) -> str:
4767 "b" : GraphQLInputField (GraphQLList (GraphQLString )),
4868 "c" : GraphQLInputField (GraphQLNonNull (GraphQLString )),
4969 "d" : GraphQLInputField (TestComplexScalar ),
70+ "e" : GraphQLInputField (TestFaultyScalar ),
5071 },
5172)
5273
@@ -253,6 +274,27 @@ def properly_runs_parse_literal_on_complex_scalar_types():
253274 None ,
254275 )
255276
277+ def errors_on_faulty_scalar_type_input ():
278+ result = execute_query (
279+ """
280+ {
281+ fieldWithObjectInput(input: {c: "foo", e: "bar"})
282+ }
283+ """
284+ )
285+
286+ assert result == (
287+ {"fieldWithObjectInput" : None },
288+ [
289+ {
290+ "message" : "Argument 'input' has invalid value"
291+ ' { c: "foo", e: "bar" }.' ,
292+ "path" : ["fieldWithObjectInput" ],
293+ "locations" : [(3 , 51 )],
294+ }
295+ ],
296+ )
297+
256298 def describe_using_variables ():
257299 doc = """
258300 query ($input: TestInputObject) {
@@ -365,6 +407,22 @@ def executes_with_complex_scalar_input():
365407 None ,
366408 )
367409
410+ def errors_on_faulty_scalar_type_input ():
411+ params = {"input" : {"c" : "foo" , "e" : "SerializedValue" }}
412+ result = execute_query (doc , params )
413+
414+ assert result == (
415+ None ,
416+ [
417+ {
418+ "message" : "Variable '$input' got invalid value"
419+ " 'SerializedValue' at 'input.e'; FaultyScalarErrorMessage" ,
420+ "locations" : [(2 , 24 )],
421+ "extensions" : {"code" : "FaultyScalarExtensionCode" },
422+ }
423+ ],
424+ )
425+
368426 def errors_on_null_for_nested_non_null ():
369427 params = {"input" : {"a" : "foo" , "b" : "bar" , "c" : None }}
370428 result = execute_query (doc , params )
@@ -676,8 +734,8 @@ def reports_error_for_array_passed_into_string_input():
676734 )
677735
678736 errors = result .errors
679- assert errors is not None
680- assert errors [0 ].original_error is None
737+ assert errors
738+ assert errors [0 ].original_error
681739
682740 def reports_error_for_non_provided_variables_for_non_nullable_inputs ():
683741 # Note: this test would typically fail validation before
0 commit comments