Skip to content

Commit 761f37e

Browse files
committed
implement suggestions and remove Union handling in coerceValue
1 parent ea4edaa commit 761f37e

File tree

2 files changed

+13
-25
lines changed

2 files changed

+13
-25
lines changed

graphql/rules.lua

+4-4
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ end
4040

4141
function rules.argumentsDefinedOnType(node, context)
4242
if node.arguments then
43-
local parentField = util.getParentField(context, node.name.value, 1)
43+
local parentField = util.getParentField(context, node.name.value)
4444
for _, argument in pairs(node.arguments) do
4545
local name = argument.name.value
4646
if not parentField.arguments[name] then
@@ -178,7 +178,7 @@ end
178178

179179
function rules.argumentsOfCorrectType(node, context)
180180
if node.arguments then
181-
local parentField = util.getParentField(context, node.name.value, 1)
181+
local parentField = util.getParentField(context, node.name.value)
182182
for _, argument in pairs(node.arguments) do
183183
local name = argument.name.value
184184
local argumentType = parentField.arguments[name]
@@ -189,7 +189,7 @@ end
189189

190190
function rules.requiredArgumentsPresent(node, context)
191191
local arguments = node.arguments or {}
192-
local parentField = util.getParentField(context, node.name.value, 1)
192+
local parentField = util.getParentField(context, node.name.value)
193193
for name, argument in pairs(parentField.arguments) do
194194
if argument.__type == 'NonNull' then
195195
local present = util.find(arguments, function(argument)
@@ -448,7 +448,7 @@ function rules.variableUsageAllowed(node, context)
448448
if not arguments then return end
449449

450450
for field in pairs(arguments) do
451-
local parentField = util.getParentField(context, field, 1)
451+
local parentField = util.getParentField(context, field)
452452
for i = 1, #arguments[field] do
453453
local argument = arguments[field][i]
454454
if argument.value.kind == 'variable' then

graphql/util.lua

+9-21
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,14 @@ function util.bind1(func, x)
2323
end
2424
end
2525

26-
function util.getParentField(context, name, step_back)
27-
local obj = context.objects[#context.objects - step_back]
28-
if obj.__type == 'List' then
29-
return obj.ofType.fields[name]
30-
else
31-
return obj.fields[name]
32-
end
26+
function util.getParentField(context, name, count)
27+
count = count == nil and 1 or count
28+
local obj = context.objects[#context.objects - count]
29+
if obj.__type == 'List' then
30+
return obj.ofType.fields[name]
31+
else
32+
return obj.fields[name]
33+
end
3334
end
3435

3536
function util.coerceValue(node, schemaType, variables)
@@ -47,20 +48,6 @@ function util.coerceValue(node, schemaType, variables)
4748
return variables[node.name.value]
4849
end
4950

50-
if schemaType.__type == 'Union' then
51-
local matched, result
52-
for k, v in ipairs(schemaType.types) do
53-
matched, result = pcall(util.coerceValue, node, v, variables)
54-
if matched then
55-
return result
56-
end
57-
end
58-
59-
local valid_types = util.map(schemaType.types, function (t) return t.name end)
60-
--TODO! how to better report this error?
61-
error('Could not coerce "' .. tostring(node.kind) .. '" to one of union types ['..table.concat(valid_types, ",")..']')
62-
end
63-
6451
if schemaType.__type == 'List' then
6552
if node.kind ~= 'list' then
6653
error('Expected a list')
@@ -104,6 +91,7 @@ function util.coerceValue(node, schemaType, variables)
10491

10592
return schemaType.parseLiteral(node)
10693
end
94+
10795
end
10896

10997
return util

0 commit comments

Comments
 (0)