3
3
from .is_nullish import is_nullish
4
4
5
5
6
- def value_from_ast (type , value_ast , variables ):
6
+ def value_from_ast (value_ast , type , variables = None ):
7
7
"""Given a type and a value AST node known to match this type, build a
8
8
runtime value."""
9
9
if isinstance (type , GraphQLNonNull ):
10
10
# Note: we're not checking that the result of coerceValueAST is non-null.
11
11
# 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 )
13
13
14
14
if not value_ast :
15
15
return None
@@ -18,37 +18,46 @@ def value_from_ast(type, value_ast, variables):
18
18
variable_name = value_ast .name .value
19
19
if not variables or variable_name not in variables :
20
20
return None
21
+
21
22
# Note: we're not doing any checking that this variable is correct. We're assuming that this query
22
23
# has been validated and the variable usage here is of the correct type.
23
24
return variables [variable_name ]
24
25
25
26
if isinstance (type , GraphQLList ):
26
27
item_type = type .of_type
27
28
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 )
29
30
for item_ast in value_ast .values ]
31
+
30
32
else :
31
- return [value_from_ast (item_type , value_ast , variables )]
33
+ return [value_from_ast (value_ast , item_type , variables )]
32
34
33
35
if isinstance (type , GraphQLInputObjectType ):
34
36
fields = type .get_fields ()
35
37
if not isinstance (value_ast , ast .ObjectValue ):
36
38
return None
39
+
37
40
field_asts = {}
41
+
38
42
for field in value_ast .fields :
39
43
field_asts [field .name .value ] = field
44
+
40
45
obj = {}
41
46
for field_name , field in fields .items ():
42
47
field_ast = field_asts .get (field_name )
43
48
field_value_ast = None
49
+
44
50
if field_ast :
45
51
field_value_ast = field_ast .value
52
+
46
53
field_value = value_from_ast (
47
- field .type , field_value_ast , variables
54
+ field_value_ast , field .type , variables
48
55
)
49
56
if field_value is None :
50
57
field_value = field .default_value
58
+
51
59
obj [field_name ] = field_value
60
+
52
61
return obj
53
62
54
63
assert isinstance (type , (GraphQLScalarType , GraphQLEnumType )), \
0 commit comments