Skip to content

Fix coerse scalar list variables #54

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 3 commits into from
Aug 19, 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
15 changes: 15 additions & 0 deletions graphql/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,21 @@ local function coerceValue(node, schemaType, variables, opts)
node.name.value, variables[node.name.value], schemaType.name
))
end
elseif type(value) == 'table' then
if schemaType.__type == 'List' and type(schemaType.ofType) == 'table' and schemaType.ofType.parseValue ~= nil then
for k, v in ipairs(value) do
value[k] = schemaType.ofType.parseValue(v)
end
end

if schemaType.__type == 'InputObject' then
for k, v in pairs(value) do
if schemaType.fields[k] ~= nil and type(schemaType.fields[k].kind) == 'table' and
schemaType.fields[k].kind.parseValue ~= nil then
value[k] = schemaType.fields[k].kind.parseValue(v)
end
end
end
end
return value
end
Expand Down
63 changes: 59 additions & 4 deletions test/integration/graphql_test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -562,8 +562,8 @@ function g.test_custom_type_scalar_variables()
if args.field == nil then
return nil
end
assert(type(args.field) == 'table', "Field is not a table! ")
assert(args.field.test ~= nil, "No field 'test' in object!")
t.assert_type(args.field, 'table', "Field is not a table! ")
t.assert_not_equals(args.field.test, nil, "No field 'test' in object!")
return args.field
end
},
Expand All @@ -576,6 +576,20 @@ function g.test_custom_type_scalar_variables()
return args.fields[1]
end
},
['test_json_type_list'] = {
arguments = {
array = types.list(json_type),
},
kind = types.list(json_type),
resolve = function(_, args)
if args.array == nil then
return nil
end
t.assert_type(args.array[1], 'table', "Array element is not a table! ")
t.assert_not_equals(args.array[1].test, nil, "No field 'test' in array element!")
return args.array
end
},
['test_custom_type_scalar_inputObject'] = {
kind = types.string,
arguments = {
Expand All @@ -594,7 +608,28 @@ function g.test_custom_type_scalar_variables()
resolve = function(_, args)
return args.object.nested_object.field
end
}
},
['test_json_type_inputObject'] = {
kind = json_type,
arguments = {
object = types.inputObject({
name = 'ComplexJsonInputObject',
fields = {
nested_object = types.inputObject({
name = 'ComplexJsonNestedInputObject',
fields = {
field = json_type,
}
}),
}
}),
},
resolve = function(_, args)
t.assert_type(args.object.nested_object.field, 'table', "Object element is not a table! ")
t.assert_not_equals(args.object.nested_object.field.test, nil, "No field 'test' in object element!")
return args.object.nested_object.field
end
},
}

t.assert_equals(check_request([[
Expand All @@ -617,6 +652,16 @@ function g.test_custom_type_scalar_variables()
variables = {field = box.NULL},
}), {test_json_type = 'null'})

t.assert_equals(check_request([[
query($array: [Json]) {
test_json_type_list(
array: $array
)
}
]], query_schema, nil, nil, {
variables = {array = {json.encode({test = 123})}},
}), {test_json_type_list = {'{"test":123}'}})

t.assert_equals(check_request([[
query {
test_json_type(
Expand Down Expand Up @@ -752,9 +797,19 @@ function g.test_custom_type_scalar_variables()
object: { nested_object: { field: $field } }
)
}
]], query_schema, nil, nil, {variables = {fields = {'echo'}}})
]], query_schema, nil, nil, {variables = {field = {'echo'}}})
end
)

t.assert_equals(check_request([[
query($nested_object: ComplexJsonNestedInputObject!) {
test_json_type_inputObject(
object: { nested_object: $nested_object }
)
}
]], query_schema, nil, nil, {
variables = {nested_object = { field = json.encode({test = 123})}},
}), {test_json_type_inputObject = '{"test":123}'})
end

function g.test_output_type_mismatch_error()
Expand Down