Skip to content

Commit ba99503

Browse files
committed
support "null" arguments
Before this patch graphql parser doesn't support "null" literal. Since parser was changed we could support "null" literal specification.
1 parent b91a567 commit ba99503

File tree

1 file changed

+28
-8
lines changed

1 file changed

+28
-8
lines changed

graphql/util.lua

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,16 @@ local function coerceValue(node, schemaType, variables, opts)
6666
variables = variables or {}
6767
opts = opts or {}
6868
local strict_non_null = opts.strict_non_null or false
69+
local defaultValues = opts.defaultValues or {}
6970

7071
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+
7176
local res = coerceValue(node, schemaType.ofType, variables, opts)
7277
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)))
7579
end
7680
return res
7781
end
@@ -86,7 +90,24 @@ local function coerceValue(node, schemaType, variables, opts)
8690
end
8791

8892
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
90111
end
91112

92113
if schemaType.__type == 'List' then
@@ -144,12 +165,11 @@ local function coerceValue(node, schemaType, variables, opts)
144165
end
145166

146167
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))
150171
end
151-
152-
return schemaType.parseLiteral(node)
172+
return value
153173
end
154174
end
155175

0 commit comments

Comments
 (0)