2424
2525
2626def describe_execute_handles_basic_execution_tasks ():
27-
2827 # noinspection PyTypeChecker
2928 def throws_if_no_document_is_provided ():
3029 schema = GraphQLSchema (
@@ -45,6 +44,39 @@ def throws_if_no_schema_is_provided():
4544
4645 assert str (exc_info .value ) == "Expected None to be a GraphQL schema."
4746
47+ def throws_on_invalid_variables ():
48+ schema = GraphQLSchema (
49+ GraphQLObjectType (
50+ "Type" ,
51+ {
52+ "fieldA" : GraphQLField (
53+ GraphQLString , args = {"argA" : GraphQLArgument (GraphQLInt )}
54+ )
55+ },
56+ )
57+ )
58+ document = parse (
59+ """
60+ query ($a: Int) {
61+ fieldA(argA: $a)
62+ }
63+ """
64+ )
65+ variable_values = "{'a': 1}"
66+
67+ with raises (TypeError ) as exc_info :
68+ assert execute (
69+ schema = schema ,
70+ document = document ,
71+ variable_values = variable_values , # type: ignore
72+ )
73+
74+ assert str (exc_info .value ) == (
75+ "Variable values must be provided as a dictionary"
76+ " with variable names as keys. Perhaps look to see"
77+ " if an unparsed JSON string was provided."
78+ )
79+
4880 def accepts_positional_arguments ():
4981 schema = GraphQLSchema (
5082 GraphQLObjectType (
@@ -59,7 +91,6 @@ def accepts_positional_arguments():
5991
6092 @mark .asyncio
6193 async def executes_arbitrary_code ():
62-
6394 # noinspection PyMethodMayBeStatic,PyMethodMayBeStatic
6495 class Data :
6596 def a (self , _info ):
@@ -80,7 +111,7 @@ def e(self, _info):
80111 f = "Fish"
81112
82113 # Called only by DataType::pic static resolver
83- def pic (self , _info , size = 50 ):
114+ def pic (self , _info , size ):
84115 return f"Pic of size: { size } "
85116
86117 def deep (self , _info ):
@@ -766,6 +797,17 @@ class Data:
766797
767798 assert result == ({"a" : "b" }, None )
768799
800+ def ignores_missing_sub_selections_on_fields ():
801+ some_type = GraphQLObjectType ("SomeType" , {"b" : GraphQLField (GraphQLString )})
802+ schema = GraphQLSchema (
803+ GraphQLObjectType ("Query" , {"a" : GraphQLField (some_type )})
804+ )
805+ document = parse ("{ a }" )
806+ root_value = {"a" : {"b" : "c" }}
807+
808+ result = execute (schema , document , root_value )
809+ assert result == ({"a" : {}}, None )
810+
769811 def does_not_include_illegal_fields_in_output ():
770812 schema = GraphQLSchema (
771813 GraphQLObjectType ("Q" , {"a" : GraphQLField (GraphQLString )})
@@ -804,7 +846,8 @@ def does_not_include_arguments_that_were_not_set():
804846 None ,
805847 )
806848
807- def fails_when_an_is_type_of_check_is_not_met ():
849+ @mark .asyncio
850+ async def fails_when_is_type_of_check_is_not_met ():
808851 class Special :
809852 value : str
810853
@@ -817,10 +860,20 @@ class NotSpecial:
817860 def __init__ (self , value ):
818861 self .value = value
819862
863+ def is_type_of_special (obj , _info ):
864+ is_special = isinstance (obj , Special )
865+ if not _info .context ["async" ]:
866+ return is_special
867+
868+ async def async_is_special ():
869+ return is_special
870+
871+ return async_is_special ()
872+
820873 SpecialType = GraphQLObjectType (
821874 "SpecialType" ,
822875 {"value" : GraphQLField (GraphQLString )},
823- is_type_of = lambda obj , _info : isinstance ( obj , Special ) ,
876+ is_type_of = is_type_of_special ,
824877 )
825878
826879 schema = GraphQLSchema (
@@ -832,7 +885,8 @@ def __init__(self, value):
832885 document = parse ("{ specials { value } }" )
833886 root_value = {"specials" : [Special ("foo" ), NotSpecial ("bar" )]}
834887
835- result = execute (schema , document , root_value )
888+ result = execute (schema , document , root_value , {"async" : False })
889+ assert not isinstance (result , Awaitable )
836890 assert result == (
837891 {"specials" : [{"value" : "foo" }, None ]},
838892 [
@@ -845,6 +899,11 @@ def __init__(self, value):
845899 ],
846900 )
847901
902+ async_result = execute (schema , document , root_value , {"async" : True })
903+ assert isinstance (async_result , Awaitable )
904+ awaited_result = await async_result
905+ assert awaited_result == result
906+
848907 def executes_ignoring_invalid_non_executable_definitions ():
849908 schema = GraphQLSchema (
850909 GraphQLObjectType ("Query" , {"foo" : GraphQLField (GraphQLString )})
0 commit comments