Skip to content

Commit 489d8be

Browse files
Fix variable validation for type as table
Before this patch, describing argument type in schema with a table resulted in "unsupported Lua type 'function'" error on variable type validation. Based on PR #22 by @no1seman
1 parent 009b3a2 commit 489d8be

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

graphql/rules.lua

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,13 @@ local function isVariableTypesValid(argument, argumentType, context,
517517
variableType = types.nonNull(variableType)
518518
end
519519

520+
-- This line of code provides support to using
521+
-- `arg = { kind = type, description = desc }`
522+
-- type declaration in query input arguments
523+
-- instead of `arg = type` one when passing
524+
-- argument with a variable.
525+
if argumentType.kind ~= nil then argumentType = argumentType.kind end
526+
520527
if not isTypeSubTypeOf(variableType, argumentType, context) then
521528
return false, ('Variable "%s" type mismatch: the variable type "%s" ' ..
522529
'is not compatible with the argument type "%s"'):format(variableName,

test/integration/graphql_test.lua

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1613,3 +1613,29 @@ function g.test_schema_input_arg_described_with_kind()
16131613
local _, errors = check_request(query, query_schema, {})
16141614
t.assert_equals(errors, nil)
16151615
end
1616+
1617+
function g.test_schema_input_arg_described_with_kind_variable_pass()
1618+
local function callback(_, args)
1619+
return args[1].value
1620+
end
1621+
1622+
local query_schema = {
1623+
['test'] = {
1624+
kind = types.string.nonNull,
1625+
arguments = {
1626+
arg = {
1627+
kind = types.string.nonNull,
1628+
},
1629+
},
1630+
resolve = callback,
1631+
}
1632+
}
1633+
1634+
local query = [[
1635+
query ($arg: String!) { test(arg: $arg) }
1636+
]]
1637+
local variables = { arg = 'B' }
1638+
1639+
local _, errors = check_request(query, query_schema, nil, nil, { variables = variables })
1640+
t.assert_equals(errors, nil)
1641+
end

0 commit comments

Comments
 (0)