Skip to content

Propagate defaults to operation callback #45

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 1 commit into from
Feb 1, 2022
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
19 changes: 19 additions & 0 deletions graphql/execute.lua
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,7 @@ local function getFieldEntry(objectType, object, fields, context)
variableValues = context.variables,
defaultValues = context.defaultValues,
directives = directives,
directivesDefaultValues = context.schema.directivesDefaultValues,
}

local resolvedObject, err = (fieldType.resolve or defaultResolver)(object, arguments, info)
Expand All @@ -352,9 +353,27 @@ evaluateSelections = function(objectType, object, selections, context)
local result = {}
local err
local fields = collectFields(objectType, selections, {}, {}, context)
local defaultValues

if context.defaultValues == nil then
if context.schema.defaultValues ~= nil and type(context.schema.defaultValues) == 'table' then
local operationDefaults = context.schema.defaultValues[context.operation.operation]
if operationDefaults ~= nil and type(operationDefaults) == 'table' then
defaultValues = context.schema.defaultValues[context.operation.operation]
end
end
else
defaultValues = context.defaultValues
end

for _, field in ipairs(fields) do
assert(result[field.name] == nil,
'two selections into the one field: ' .. field.name)

if defaultValues ~= nil then
context.defaultValues = defaultValues[field.name]
end

result[field.name], err = getFieldEntry(objectType, object, {field.selection},
context)
if err ~= nil then
Expand Down
95 changes: 94 additions & 1 deletion graphql/schema.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ end
local schema = {}
schema.__index = schema

function schema.create(config, name)
function schema.create(config, name, opts)
assert(type(config.query) == 'table', 'must provide query object')
assert(not config.mutation or type(config.mutation) == 'table', 'mutation must be a table if provided')

opts = opts or {}
local self = setmetatable({}, schema)

for k, v in pairs(config) do
Expand All @@ -34,9 +35,101 @@ function schema.create(config, name)
self:generateTypeMap(introspection.__Schema)
self:generateDirectiveMap()

if opts.defaultValues == true then
self.defaultValues = {}
self.defaultValues.mutation = self:extractDefaults(self.mutation)
self.defaultValues.query = self:extractDefaults(self.query)
end

if opts.directivesDefaultValues == true then
self.directivesDefaultValues = {}

for directiveName, directive in pairs(self.directiveMap or {}) do
self.directivesDefaultValues[directiveName] = self:extractDefaults(directive)
end
end

return self
end

function schema:extractDefaults(node)
if not node then return end

local defaultValues
local nodeType = node.__type ~= nil and node or node.kind

if nodeType.__type == 'NonNull' then
return self:extractDefaults(nodeType.ofType)
end

if nodeType.__type == 'Enum' then
return node.defaultValue
end

if nodeType.__type == 'Scalar' then
return node.defaultValue
end

node.fields = type(node.fields) == 'function' and node.fields() or node.fields

if nodeType.__type == 'Object' or nodeType.__type == 'InputObject' then
for fieldName, field in pairs(nodeType.fields or {}) do
local fieldDefaultValue = self:extractDefaults(field)
if fieldDefaultValue ~= nil then
defaultValues = defaultValues or {}
defaultValues[fieldName] = fieldDefaultValue
end

for argumentName, argument in pairs(field.arguments or {}) do
-- BEGIN_HACK: resolve type names to real types
if type(argument) == 'string' then
argument = types.resolve(argument, self.name)
field.arguments[argumentName] = argument
end

if type(argument.kind) == 'string' then
argument.kind = types.resolve(argument.kind, self.name)
end
-- END_HACK: resolve type names to real types

local argumentDefaultValue = self:extractDefaults(argument)
if argumentDefaultValue ~= nil then
defaultValues = defaultValues or {}
defaultValues[fieldName] = defaultValues[fieldName] or {}
defaultValues[fieldName][argumentName] = argumentDefaultValue
end
end
end
return defaultValues
end

if nodeType.__type =='Directive' then
for argumentName, argument in pairs(nodeType.arguments or {}) do
-- BEGIN_HACK: resolve type names to real types
if type(argument) == 'string' then
argument = types.resolve(argument, self.name)
nodeType.arguments[argumentName] = argument
end

if type(argument.kind) == 'string' then
argument.kind = types.resolve(argument.kind, self.name)
end
-- END_HACK: resolve type names to real types

local argumentDefaultValue = self:extractDefaults(argument)
if argumentDefaultValue ~= nil then
defaultValues = defaultValues or {}
defaultValues[argumentName] = argumentDefaultValue
end
end
return defaultValues
end

if nodeType.__type == 'List' then
return self:extractDefaults(nodeType.ofType)
end
end

function schema:generateTypeMap(node)
if not node or (self.typeMap[node.name] and self.typeMap[node.name] == node) then return end

Expand Down
182 changes: 181 additions & 1 deletion test/integration/graphql_test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ local function check_request(query, query_schema, mutation_schema, directives, o
directives = directives,
}

local compiled_schema = schema.create(root, test_schema_name)
local compiled_schema = schema.create(root, test_schema_name, opts)

local parsed = parse.parse(query)

Expand Down Expand Up @@ -1871,3 +1871,183 @@ function g.test_mutation_and_directive_arguments_default_values()
-- directive default values used on the same argument type
t.assert_equals(compiled_schema.typeMap['Int'].defaultValue, nil)
end

g.test_propagate_defaults_to_callback = function()
local query = '{test_mutation}'

local function callback(_, _, info)
return json.encode({
defaultValues = info.defaultValues,
directivesDefaultValues = info.directivesDefaultValues,
})
end

local input_object = types.inputObject({
name = 'test_input_object',
fields = {
nested_int_arg = {
kind = types.int,
defaultValue = 2,
},
nested_string_arg = {
kind = types.string,
defaultValue = 'default nested value',
},
nested_boolean_arg = {
kind = types.boolean,
defaultValue = true,
},
nested_float_arg = {
kind = types.float,
defaultValue = 1.1,
},
nested_long_arg = {
kind = types.long,
defaultValue = 2^50,
},
nested_list_scalar_arg = {
kind = types.list(types.string),
-- defaultValue seems illogical
}
},
kind = types.string,
})

local mutation_schema = {
['test_mutation'] = {
kind = types.string.nonNull,
arguments = {
int_arg = {
kind = types.int,
defaultValue = 1,
},
string_arg = {
kind = types.string,
defaultValue = 'string_arg'
},
boolean_arg = {
kind = types.boolean,
defaultValue = false,
},
float_arg = {
kind = types.float,
defaultValue = 1.1,
},
long_arg = {
kind = types.long,
defaultValue = 2^50,
},
object_arg = {
kind = input_object,
-- defaultValue seems illogical
},
list_scalar_arg = {
kind = types.list(types.string),
-- defaultValue seems illogical
}
},
resolve = callback,
}
}

local directives = {
types.directive({
schema = schema,
name = 'timeout',
description = 'Request execute timeout',
arguments = {
int_arg = {
kind = types.int,
defaultValue = 1,
},
string_arg = {
kind = types.string,
defaultValue = 'string_arg'
},
boolean_arg = {
kind = types.boolean,
defaultValue = false,
},
float_arg = {
kind = types.float,
defaultValue = 1.1,
},
long_arg = {
kind = types.long,
defaultValue = 2^50,
},
object = input_object
},
onField = true,
})
}

local result = {
defaultValues = {
boolean_arg = false,
int_arg = 1,
float_arg = 1.1,
long_arg = 2^50,
object_arg = {
nested_boolean_arg = true,
nested_float_arg = 1.1,
nested_int_arg = 2,
nested_long_arg = 2^50,
nested_string_arg = "default nested value",
},
string_arg = "string_arg",
},
directivesDefaultValues = {
timeout = {
boolean_arg = false,
float_arg = 1.1,
int_arg = 1,
long_arg = 2^50,
object = {
nested_boolean_arg = true,
nested_float_arg = 1.1,
nested_int_arg = 2,
nested_long_arg = 2^50,
nested_string_arg = "default nested value",
},
string_arg = "string_arg",
},
},
}

local data, errors = check_request(
query,
mutation_schema,
nil,
directives,
{ defaultValues = true, directivesDefaultValues = true, }
)

t.assert_equals(errors, nil)
t.assert_items_equals(json.decode(data.test_mutation), result)

query = '{prefix{test_mutation}}'

local mutation_schema_with_prefix = {
['prefix'] = {
kind = types.object({
name = 'prefix',
fields = mutation_schema,
}),
arguments = {},
resolve = function()
return {}
end,
}
}

data, errors = check_request(
query,
mutation_schema_with_prefix,
nil,
directives,
{ defaultValues = true, directivesDefaultValues = true, }
)
t.assert_equals(errors, nil)
t.assert_items_equals(json.decode(data.prefix.test_mutation), result)
end