Skip to content

Fix returning data and error for multiple resolvers #25

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
Nov 18, 2021
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
7 changes: 3 additions & 4 deletions graphql/execute.lua
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,6 @@ end

evaluateSelections = function(objectType, object, selections, context)
local result = {}
local errors
local err
local fields = collectFields(objectType, selections, {}, {}, context)
for _, field in ipairs(fields) do
Expand All @@ -322,14 +321,14 @@ evaluateSelections = function(objectType, object, selections, context)
result[field.name], err = getFieldEntry(objectType, object, {field.selection},
context)
if err ~= nil then
errors = errors or {}
table.insert(errors, err)
context.errors = context.errors or {}
table.insert(context.errors, err)
end
if result[field.name] == nil then
result[field.name] = box.NULL
end
end
return result, errors
return result, context.errors
end

local function execute(schema, tree, rootValue, variables, operationName)
Expand Down
45 changes: 45 additions & 0 deletions test/integration/graphql_test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1158,4 +1158,49 @@ function g.test_both_data_and_error_result()
{message = 'Simple error A'},
{message = 'Simple error B'},
})

query = [[{
prefix {
test_A: test(arg: "A")
test_B: test(arg: "B")
}
}]]

local function callback_external()
return {}, {message = 'Simple error from external resolver'}
end

local function callback_internal(_, args)
return args[1].value, {message = 'Simple error from internal resolver ' .. args[1].value}
end

query_schema = {
['prefix'] = {
kind = types.object({
name = 'prefix',
fields = {
['test'] = {
kind = types.string.nonNull,
arguments = {
arg = types.string.nonNull,
arg2 = types.string,
arg3 = types.int,
arg4 = types.long,
},
resolve = callback_internal,
}
},
}),
arguments = {},
resolve = callback_external,
}
}

data, errors = check_request(query, query_schema)
t.assert_equals(data, {prefix = {test_A = 'A', test_B = 'B'}})
t.assert_equals(errors, {
{message = "Simple error from internal resolver A"},
{message = "Simple error from internal resolver B"},
{message = "Simple error from external resolver"},
}, "Errors from each resolver were returned")
end