Skip to content

Commit 9653d02

Browse files
committed
couple of bugfixes
See also tarantool/cartridge@81dabfb ``` 1. Improve graphql tests 2. Make "isValueOfTheType" required field for scalar types In order to avoid possible misuse. It will throw an error anyway. This infact moves such check from runtime[1] to declaration time. Closes tarantool/cartridge#854 [1] https://github.com/tarantool/cartridge/blob/a9165b69af471e2c9d7ee1dce516c62c8389b9e8/cartridge/graphql/validate_variables.lua#L98 3. graphql: fix possible type mismatch between field type and variable type Closes tarantool/cartridge#853 ```
1 parent 7e18a40 commit 9653d02

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

graphql/rules.lua

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ local util = require(path .. '.util')
44
local introspection = require(path .. '.introspection')
55
local query_util = require(path .. '.query_util')
66

7+
local function error(...)
8+
return _G.error(..., 0)
9+
end
10+
711
local function getParentField(context, name, count)
812
if introspection.fieldMap[name] then return introspection.fieldMap[name] end
913

@@ -517,6 +521,19 @@ local function isVariableTypesValid(argument, argumentType, context,
517521
'is not compatible with the argument type "%s"'):format(variableName,
518522
util.getTypeName(variableType), util.getTypeName(argumentType))
519523
end
524+
elseif argument.value.kind == 'list' then
525+
-- find variables deeper
526+
local parentType = argumentType
527+
if parentType.__type == 'NonNull' then
528+
parentType = parentType.ofType
529+
end
530+
local childType = parentType.ofType
531+
532+
for _, child in ipairs(argument.value.values) do
533+
local ok, err = isVariableTypesValid({value = child}, childType, context,
534+
variableMap)
535+
if not ok then return false, err end
536+
end
520537
elseif argument.value.kind == 'inputObject' then
521538
-- find variables deeper
522539
for _, child in ipairs(argument.value.values) do

graphql/types.lua

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ end
7777
function types.scalar(config)
7878
assert(type(config.name) == 'string', 'type name must be provided as a string')
7979
assert(type(config.serialize) == 'function', 'serialize must be a function')
80+
assert(type(config.isValueOfTheType) == 'function', 'isValueOfTheType must be a function')
8081
if config.parseValue or config.parseLiteral then
8182
assert(
8283
type(config.parseValue) == 'function' and type(config.parseLiteral) == 'function',
@@ -366,13 +367,22 @@ types.boolean = types.scalar({
366367
end,
367368
})
368369

370+
--[[
371+
The ID scalar type represents a unique identifier,
372+
often used to refetch an object or as the key for a cache.
373+
The ID type is serialized in the same way as a String;
374+
however, defining it as an ID signifies that it is not intended to be human‐readable.
375+
--]]
369376
types.id = types.scalar({
370377
name = 'ID',
371378
serialize = tostring,
372379
parseValue = tostring,
373380
parseLiteral = function(node)
374381
return node.kind == 'string' or node.kind == 'int' and node.value or nil
375-
end
382+
end,
383+
isValueOfTheType = function(value)
384+
return type(value) == 'string'
385+
end,
376386
})
377387

378388
function types.directive(config)

0 commit comments

Comments
 (0)