1
1
import collections
2
+ import json
2
3
from six import string_types
3
4
from ..error import GraphQLError
4
5
from ..language .printer import print_ast
11
12
is_input_type
12
13
)
13
14
from ..utils .is_nullish import is_nullish
15
+ from ..utils .is_valid_value import is_valid_value
14
16
from ..utils .type_from_ast import type_from_ast
15
17
from ..utils .value_from_ast import value_from_ast
16
18
@@ -22,11 +24,13 @@ def get_variable_values(schema, definition_asts, inputs):
22
24
If the input cannot be parsed to match the variable definitions, a GraphQLError will be thrown."""
23
25
if inputs is None :
24
26
inputs = {}
27
+
25
28
values = {}
26
29
for def_ast in definition_asts :
27
30
var_name = def_ast .variable .name .value
28
31
value = get_variable_value (schema , def_ast , inputs .get (var_name ))
29
32
values [var_name ] = value
33
+
30
34
return values
31
35
32
36
@@ -65,10 +69,12 @@ def get_variable_value(schema, definition_ast, input):
65
69
"""Given a variable definition, and any value of input, return a value which adheres to the variable definition,
66
70
or throw an error."""
67
71
type = type_from_ast (schema , definition_ast .type )
72
+ variable = definition_ast .variable
73
+
68
74
if not type or not is_input_type (type ):
69
75
raise GraphQLError (
70
- 'Variable ${} expected value of type {} which cannot be used as an input type.' .format (
71
- definition_ast . variable .name .value ,
76
+ 'Variable " ${}" expected value of type "{}" which cannot be used as an input type.' .format (
77
+ variable .name .value ,
72
78
print_ast (definition_ast .type ),
73
79
),
74
80
[definition_ast ]
@@ -81,55 +87,25 @@ def get_variable_value(schema, definition_ast, input):
81
87
return value_from_ast (type , default_value , None )
82
88
return coerce_value (type , input )
83
89
90
+ if is_nullish (input ):
91
+ raise GraphQLError (
92
+ 'Variable "${}" of required type "{}" was not provided.' .format (
93
+ variable .name .value ,
94
+ print_ast (definition_ast .type )
95
+ ),
96
+ [definition_ast ]
97
+ )
98
+
84
99
raise GraphQLError (
85
- 'Variable ${} expected value of type {} but got: {}' .format (
86
- definition_ast . variable .name .value ,
100
+ 'Variable " ${}" expected value of type "{}" but got: {}' .format (
101
+ variable .name .value ,
87
102
print_ast (definition_ast .type ),
88
- repr (input )
103
+ json . dumps (input )
89
104
),
90
105
[definition_ast ]
91
106
)
92
107
93
108
94
- def is_valid_value (type , value ):
95
- """Given a type and any value, return True if that value is valid."""
96
- if isinstance (type , GraphQLNonNull ):
97
- if is_nullish (value ):
98
- return False
99
- return is_valid_value (type .of_type , value )
100
-
101
- if is_nullish (value ):
102
- return True
103
-
104
- if isinstance (type , GraphQLList ):
105
- item_type = type .of_type
106
- if not isinstance (value , string_types ) and \
107
- isinstance (value , collections .Iterable ):
108
- return all (is_valid_value (item_type , item ) for item in value )
109
- else :
110
- return is_valid_value (item_type , value )
111
-
112
- if isinstance (type , GraphQLInputObjectType ):
113
- if not isinstance (value , collections .Mapping ):
114
- return False
115
- fields = type .get_fields ()
116
-
117
- # Ensure every provided field is defined.
118
- if any (field_name not in fields for field_name in value .keys ()):
119
- return False
120
-
121
- # Ensure every defined field is valid.
122
- return all (
123
- is_valid_value (fields [field_name ].type , value .get (field_name ))
124
- for field_name in fields
125
- )
126
-
127
- assert isinstance (type , (GraphQLScalarType , GraphQLEnumType )), \
128
- 'Must be input type'
129
-
130
- return not is_nullish (type .parse_value (value ))
131
-
132
-
133
109
def coerce_value (type , value ):
134
110
"""Given a type and any value, return a runtime value coerced to match the type."""
135
111
if isinstance (type , GraphQLNonNull ):
@@ -153,9 +129,12 @@ def coerce_value(type, value):
153
129
obj = {}
154
130
for field_name , field in fields .items ():
155
131
field_value = coerce_value (field .type , value [field_name ])
156
- if field_value is None :
132
+ if is_nullish ( field_value ) :
157
133
field_value = field .default_value
158
- obj [field_name ] = field_value
134
+
135
+ if not is_nullish (field_value ):
136
+ obj [field_name ] = field_value
137
+
159
138
return obj
160
139
161
140
assert isinstance (type , (GraphQLScalarType , GraphQLEnumType )), \
0 commit comments