33from .is_nullish import is_nullish
44
55
6- def value_from_ast (type , value_ast , variables ):
6+ def value_from_ast (value_ast , type , variables = None ):
77 """Given a type and a value AST node known to match this type, build a
88 runtime value."""
99 if isinstance (type , GraphQLNonNull ):
1010 # Note: we're not checking that the result of coerceValueAST is non-null.
1111 # We're assuming that this query has been validated and the value used here is of the correct type.
12- return value_from_ast (type .of_type , value_ast , variables )
12+ return value_from_ast (value_ast , type .of_type , variables )
1313
1414 if not value_ast :
1515 return None
@@ -18,37 +18,46 @@ def value_from_ast(type, value_ast, variables):
1818 variable_name = value_ast .name .value
1919 if not variables or variable_name not in variables :
2020 return None
21+
2122 # Note: we're not doing any checking that this variable is correct. We're assuming that this query
2223 # has been validated and the variable usage here is of the correct type.
2324 return variables [variable_name ]
2425
2526 if isinstance (type , GraphQLList ):
2627 item_type = type .of_type
2728 if isinstance (value_ast , ast .ListValue ):
28- return [value_from_ast (item_type , item_ast , variables )
29+ return [value_from_ast (item_ast , item_type , variables )
2930 for item_ast in value_ast .values ]
31+
3032 else :
31- return [value_from_ast (item_type , value_ast , variables )]
33+ return [value_from_ast (value_ast , item_type , variables )]
3234
3335 if isinstance (type , GraphQLInputObjectType ):
3436 fields = type .get_fields ()
3537 if not isinstance (value_ast , ast .ObjectValue ):
3638 return None
39+
3740 field_asts = {}
41+
3842 for field in value_ast .fields :
3943 field_asts [field .name .value ] = field
44+
4045 obj = {}
4146 for field_name , field in fields .items ():
4247 field_ast = field_asts .get (field_name )
4348 field_value_ast = None
49+
4450 if field_ast :
4551 field_value_ast = field_ast .value
52+
4653 field_value = value_from_ast (
47- field .type , field_value_ast , variables
54+ field_value_ast , field .type , variables
4855 )
4956 if field_value is None :
5057 field_value = field .default_value
58+
5159 obj [field_name ] = field_value
60+
5261 return obj
5362
5463 assert isinstance (type , (GraphQLScalarType , GraphQLEnumType )), \
0 commit comments