Skip to content

graphql: strict type validation #689

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Mar 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 65 additions & 56 deletions cartridge/graphql/execute.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,8 @@ local path = (...):gsub('%.[^%.]+$', '')
local types = require(path .. '.types')
local util = require(path .. '.util')
local introspection = require(path .. '.introspection')

local function typeFromAST(node, schema)
local innerType
if node.kind == 'listType' then
innerType = typeFromAST(node.type)
return innerType and types.list(innerType)
elseif node.kind == 'nonNullType' then
innerType = typeFromAST(node.type)
return innerType and types.nonNull(innerType)
else
assert(node.kind == 'namedType', 'Variable must be a named type')
return schema:getType(node.name.value)
end
end
local query_util = require(path .. '.query_util')
local validate_variables = require(path .. '.validate_variables')

local function getFieldResponseKey(field)
return field.alias and field.alias.name.value or field.name.value
Expand Down Expand Up @@ -50,7 +38,7 @@ end
local function doesFragmentApply(fragment, type, context)
if not fragment.typeCondition then return true end

local innerType = typeFromAST(fragment.typeCondition, context.schema)
local innerType = query_util.typeFromAST(fragment.typeCondition, context.schema)

if innerType == type then
return true
Expand Down Expand Up @@ -83,48 +71,69 @@ local function defaultResolver(object, arguments, info)
return object[info.fieldASTs[1].name.value]
end

local function buildContext(schema, tree, rootValue, variables, operationName)
local context = {
schema = schema,
rootValue = rootValue,
variables = variables,
operation = nil,
fragmentMap = {},
defaultValues = {},
request_cache = {},
}
local function getOperation(tree, operationName)
local operation

for _, definition in ipairs(tree.definitions) do
if definition.kind == 'operation' then
if not operationName and context.operation then
error('Operation name must be specified if more than one operation exists.')
end
for _, definition in ipairs(tree.definitions) do
if definition.kind == 'operation' then
if not operationName and operation then
error('Operation name must be specified if more than one operation exists.')
end

if not operationName or definition.name.value == operationName then
context.operation = definition
end
if not operationName or definition.name.value == operationName then
operation = definition
end
end
end

for _, variableDefinition in ipairs(definition.variableDefinitions or {}) do
if variableDefinition.defaultValue ~= nil then
context.defaultValues[variableDefinition.variable.name.value] =
variableDefinition.defaultValue.value
if not operation then
if operationName then
error('Unknown operation "' .. operationName .. '"')
else
error('Must provide an operation')
end
end

end
end
elseif definition.kind == 'fragmentDefinition' then
context.fragmentMap[definition.name.value] = definition
return operation
end

local function getFragmentDefinitions(tree)
local fragmentMap = {}

for _, definition in ipairs(tree.definitions) do
if definition.kind == 'fragmentDefinition' then
fragmentMap[definition.name.value] = definition
end
end
end

if not context.operation then
if operationName then
error('Unknown operation "' .. operationName .. '"')
else
error('Must provide an operation')
return fragmentMap
end

-- Extract variableTypes from the operation.
local function getVariableTypes(schema, operation)
local variableTypes = {}

for _, definition in ipairs(operation.variableDefinitions or {}) do
variableTypes[definition.variable.name.value] =
query_util.typeFromAST(definition.type, schema)
end
end

return context
return variableTypes
end

local function buildContext(schema, tree, rootValue, variables, operationName)
local operation = getOperation(tree, operationName)
local fragmentMap = getFragmentDefinitions(tree)
local variableTypes = getVariableTypes(schema, operation)
return {
schema = schema,
rootValue = rootValue,
variables = variables,
operation = operation,
fragmentMap = fragmentMap,
variableTypes = variableTypes,
request_cache = {},
}
end

local function collectFields(objectType, selections, visitedFragments, result, context)
Expand Down Expand Up @@ -153,7 +162,7 @@ local function collectFields(objectType, selections, visitedFragments, result, c
return result
end

local evaluateSelection
local evaluateSelections
local serializemap = {__serialize='map'}

local function completeValue(fieldType, result, subSelections, context, opts)
Expand Down Expand Up @@ -216,7 +225,6 @@ end
local function getFieldEntry(objectType, object, fields, context)
local firstField = fields[1]
local fieldName = firstField.name.value
local responseKey = getFieldResponseKey(firstField)
local fieldType = introspection.fieldMap[fieldName] or objectType.fields[fieldName]

if fieldType == nil then
Expand All @@ -231,11 +239,10 @@ local function getFieldEntry(objectType, object, fields, context)
local arguments = util.map(fieldType.arguments or {}, function(argument, name)
local supplied = argumentMap[name] and argumentMap[name].value

local value = supplied and util.coerceValue(supplied, argument,
context.variables,
context.defaultValues)
if value ~= nil then
return value
supplied = util.coerceValue(supplied, argument, context.variables,
{strict_non_null = true})
if supplied ~= nil then
return supplied
end

return argument.defaultValue
Expand Down Expand Up @@ -310,6 +317,8 @@ local function execute(schema, tree, rootValue, variables, operationName)
error('Unsupported operation "' .. context.operation.operation .. '"')
end

validate_variables.validate_variables(context)

return evaluateSelections(rootType, rootValue, context.operation.selectionSet.selections, context)
end

Expand Down
21 changes: 21 additions & 0 deletions cartridge/graphql/query_util.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
local path = (...):gsub('%.[^%.]+$', '')
local types = require(path .. '.types')
local util = require(path .. '.util')

local function typeFromAST(node, schema)
local innerType
if node.kind == 'listType' then
innerType = typeFromAST(node.type, schema)
return innerType and types.list(innerType)
elseif node.kind == 'nonNullType' then
innerType = typeFromAST(node.type, schema)
return innerType and types.nonNull(innerType)
else
assert(node.kind == 'namedType', 'Variable must be a named type')
return schema:getType(node.name.value)
end
end

return {
typeFromAST = typeFromAST,
}
Loading