Skip to content

Commit

Permalink
support default variables
Browse files Browse the repository at this point in the history
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
```
  • Loading branch information
olegrok committed Feb 11, 2021
1 parent 9653d02 commit 470d4c3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
18 changes: 16 additions & 2 deletions graphql/execute.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ local introspection = require(path .. '.introspection')
local query_util = require(path .. '.query_util')
local validate_variables = require(path .. '.validate_variables')

local function error(...)
return _G.error(..., 0)
end

local function getFieldResponseKey(field)
return field.alias and field.alias.name.value or field.name.value
end
Expand Down Expand Up @@ -236,16 +240,26 @@ local function getFieldEntry(objectType, object, fields, context)
argumentMap[argument.name.value] = argument
end

local defaultValues = {}
if context.operation.variableDefinitions ~= nil then
for _, value in ipairs(context.operation.variableDefinitions) do
if value.defaultValue ~= nil then
local variableType = query_util.typeFromAST(value.type, context.schema)
defaultValues[value.variable.name.value] = util.coerceValue(value.defaultValue, variableType)
end
end
end

local arguments = util.map(fieldType.arguments or {}, function(argument, name)
local supplied = argumentMap[name] and argumentMap[name].value

supplied = util.coerceValue(supplied, argument, context.variables,
{strict_non_null = true})
if supplied ~= nil then
if type(supplied) ~= 'nil' then
return supplied
end

return argument.defaultValue
return defaultValues[name]
end)

--[[
Expand Down
3 changes: 2 additions & 1 deletion graphql/rules.lua
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,8 @@ function rules.variableDefaultValuesHaveCorrectType(node, context)
if definition.type.kind == 'nonNullType' and definition.defaultValue then
error('Non-null variables can not have default values')
elseif definition.defaultValue then
util.coerceValue(definition.defaultValue, context.schema:getType(definition.type.name.value))
local variableType = query_util.typeFromAST(definition.type, context.schema)
util.coerceValue(definition.defaultValue, variableType)
end
end
end
Expand Down

0 comments on commit 470d4c3

Please sign in to comment.