@@ -66,12 +66,16 @@ local function coerceValue(node, schemaType, variables, opts)
66
66
variables = variables or {}
67
67
opts = opts or {}
68
68
local strict_non_null = opts .strict_non_null or false
69
+ local defaultValues = opts .defaultValues or {}
69
70
70
71
if schemaType .__type == ' NonNull' then
72
+ if node .kind == ' null' then
73
+ error ((' Expected non-null for "%s", got null' ):format (getTypeName (schemaType )))
74
+ end
75
+
71
76
local res = coerceValue (node , schemaType .ofType , variables , opts )
72
77
if strict_non_null and res == nil then
73
- error ((' Expected non-null for "%s", got null' ):format (
74
- getTypeName (schemaType )))
78
+ error ((' Expected non-null for "%s", got null' ):format (getTypeName (schemaType )))
75
79
end
76
80
return res
77
81
end
@@ -86,7 +90,24 @@ local function coerceValue(node, schemaType, variables, opts)
86
90
end
87
91
88
92
if node .kind == ' variable' then
89
- return variables [node .name .value ]
93
+ local value = variables [node .name .value ]
94
+ local defaultValue = defaultValues [node .name .value ]
95
+ if type (value ) == ' nil' and type (defaultValue ) ~= ' nil' then
96
+ -- default value was parsed by parseLiteral
97
+ value = defaultValue
98
+ elseif schemaType .parseValue ~= nil then
99
+ value = schemaType .parseValue (value )
100
+ if strict_non_null and type (value ) == ' nil' then
101
+ error ((' Could not coerce variable "%s" with value "%s" to type "%s"' ):format (
102
+ node .name .value , variables [node .name .value ], schemaType .name
103
+ ))
104
+ end
105
+ end
106
+ return value
107
+ end
108
+
109
+ if node .kind == ' null' then
110
+ return box .NULL
90
111
end
91
112
92
113
if schemaType .__type == ' List' then
@@ -144,12 +165,11 @@ local function coerceValue(node, schemaType, variables, opts)
144
165
end
145
166
146
167
if schemaType .__type == ' Scalar' then
147
- if schemaType .parseLiteral (node ) == nil then
148
- error (( ' Could not coerce "%s" to "%s" ' ): format (
149
- node .value or node .kind , schemaType .name ))
168
+ local value = schemaType .parseLiteral (node )
169
+ if strict_non_null and type ( value ) == ' nil ' then
170
+ error (( ' Could not coerce value "%s" to type "%s" ' ): format ( node .value or node .kind , schemaType .name ))
150
171
end
151
-
152
- return schemaType .parseLiteral (node )
172
+ return value
153
173
end
154
174
end
155
175
0 commit comments