Skip to content
This repository was archived by the owner on Apr 14, 2022. It is now read-only.

Commit d2c2dce

Browse files
committed
Deduplicate typeFromAST code
1 parent 2f383fe commit d2c2dce

File tree

2 files changed

+5
-17
lines changed

2 files changed

+5
-17
lines changed

graphql/core/query_util.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ local query_util = {}
77
function query_util.typeFromAST(node, schema)
88
local innerType
99
if node.kind == 'listType' then
10-
innerType = query_util.typeFromAST(node.type)
10+
innerType = query_util.typeFromAST(node.type, schema)
1111
return innerType and types.list(innerType)
1212
elseif node.kind == 'nonNullType' then
13-
innerType = query_util.typeFromAST(node.type)
13+
innerType = query_util.typeFromAST(node.type, schema)
1414
return innerType and types.nonNull(innerType)
1515
else
1616
assert(node.kind == 'namedType', 'Variable must be a named type')

graphql/core/rules.lua

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ local types = require(path .. '.types')
33
local util = require(path .. '.util')
44
local schema = require(path .. '.schema')
55
local introspection = require(path .. '.introspection')
6+
local query_util = require(path .. '.query_util')
67

78
local function getParentField(context, name, count)
89
if introspection.fieldMap[name] then return introspection.fieldMap[name] end
@@ -490,21 +491,8 @@ function rules.variableUsageAllowed(node, context)
490491
local variableDefinition = variableMap[variableName]
491492
local hasDefault = variableDefinition.defaultValue ~= nil
492493

493-
local function typeFromAST(variable)
494-
local innerType
495-
if variable.kind == 'listType' then
496-
innerType = typeFromAST(variable.type)
497-
return innerType and types.list(innerType)
498-
elseif variable.kind == 'nonNullType' then
499-
innerType = typeFromAST(variable.type)
500-
return innerType and types.nonNull(innerType)
501-
else
502-
assert(variable.kind == 'namedType', 'Variable must be a named type')
503-
return context.schema:getType(variable.name.value)
504-
end
505-
end
506-
507-
local variableType = typeFromAST(variableDefinition.type)
494+
local variableType = query_util.typeFromAST(variableDefinition.type,
495+
context.schema)
508496

509497
if hasDefault and variableType.__type ~= 'NonNull' then
510498
variableType = types.nonNull(variableType)

0 commit comments

Comments
 (0)