Skip to content

Commit 470d4c3

Browse files
committed
support default variables
Imported from tarantool/cartridge@6eb4a43 ``` See: https://graphql.org/learn/queries/#default-variables Note: there is a difference between explicitly passed "null" value and just omitted variable. See graphql/graphql-spec#418 Closes tarantool/cartridge#866 ```
1 parent 9653d02 commit 470d4c3

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

graphql/execute.lua

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ local introspection = require(path .. '.introspection')
55
local query_util = require(path .. '.query_util')
66
local validate_variables = require(path .. '.validate_variables')
77

8+
local function error(...)
9+
return _G.error(..., 0)
10+
end
11+
812
local function getFieldResponseKey(field)
913
return field.alias and field.alias.name.value or field.name.value
1014
end
@@ -236,16 +240,26 @@ local function getFieldEntry(objectType, object, fields, context)
236240
argumentMap[argument.name.value] = argument
237241
end
238242

243+
local defaultValues = {}
244+
if context.operation.variableDefinitions ~= nil then
245+
for _, value in ipairs(context.operation.variableDefinitions) do
246+
if value.defaultValue ~= nil then
247+
local variableType = query_util.typeFromAST(value.type, context.schema)
248+
defaultValues[value.variable.name.value] = util.coerceValue(value.defaultValue, variableType)
249+
end
250+
end
251+
end
252+
239253
local arguments = util.map(fieldType.arguments or {}, function(argument, name)
240254
local supplied = argumentMap[name] and argumentMap[name].value
241255

242256
supplied = util.coerceValue(supplied, argument, context.variables,
243257
{strict_non_null = true})
244-
if supplied ~= nil then
258+
if type(supplied) ~= 'nil' then
245259
return supplied
246260
end
247261

248-
return argument.defaultValue
262+
return defaultValues[name]
249263
end)
250264

251265
--[[

graphql/rules.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,8 @@ function rules.variableDefaultValuesHaveCorrectType(node, context)
410410
if definition.type.kind == 'nonNullType' and definition.defaultValue then
411411
error('Non-null variables can not have default values')
412412
elseif definition.defaultValue then
413-
util.coerceValue(definition.defaultValue, context.schema:getType(definition.type.name.value))
413+
local variableType = query_util.typeFromAST(definition.type, context.schema)
414+
util.coerceValue(definition.defaultValue, variableType)
414415
end
415416
end
416417
end

0 commit comments

Comments
 (0)