Skip to content

Commit dd425ea

Browse files
committed
return data and errors acc. to GraphQL spec #7.1.2
1 parent 9d56aae commit dd425ea

File tree

3 files changed

+38
-5
lines changed

3 files changed

+38
-5
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,5 @@ luacov.*.out*
2626
/node_modules
2727
/package-lock.json
2828
*.mo
29+
.history
30+
.vscode

graphql/execute.lua

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -301,30 +301,35 @@ local function getFieldEntry(objectType, object, fields, context)
301301
}
302302

303303
local resolvedObject, err = (fieldType.resolve or defaultResolver)(object, arguments, info)
304-
if err ~= nil then
304+
if resolvedObject == nil and err ~= nil then
305305
error(err)
306306
end
307307

308308
local subSelections = mergeSelectionSets(fields)
309309
return completeValue(fieldType.kind, resolvedObject, subSelections, context,
310310
{fieldName = fieldName}
311-
)
311+
), err
312312
end
313313

314314
evaluateSelections = function(objectType, object, selections, context)
315315
local result = {}
316+
local errors
317+
local err
316318
local fields = collectFields(objectType, selections, {}, {}, context)
317319
for _, field in ipairs(fields) do
318320
assert(result[field.name] == nil,
319321
'two selections into the one field: ' .. field.name)
320-
result[field.name] = getFieldEntry(objectType, object, {field.selection},
322+
result[field.name], err = getFieldEntry(objectType, object, {field.selection},
321323
context)
322-
324+
if err ~= nil then
325+
errors = errors or {}
326+
table.insert(errors, err)
327+
end
323328
if result[field.name] == nil then
324329
result[field.name] = box.NULL
325330
end
326331
end
327-
return result
332+
return result, errors
328333
end
329334

330335
local function execute(schema, tree, rootValue, variables, operationName)

test/integration/graphql_test.lua

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1129,3 +1129,29 @@ function g.test_validation_non_null_argument_error()
11291129
end
11301130
)
11311131
end
1132+
1133+
function g.test_both_data_and_error_result()
1134+
local query = [[
1135+
{ test(arg: "A") }
1136+
]]
1137+
1138+
local function callback(_, args)
1139+
return args[1].value, {message = 'Simple error'}
1140+
end
1141+
1142+
local query_schema = {
1143+
['test'] = {
1144+
kind = types.string.nonNull,
1145+
arguments = {
1146+
arg = types.string.nonNull,
1147+
arg2 = types.string,
1148+
arg3 = types.int,
1149+
arg4 = types.long,
1150+
},
1151+
resolve = callback,
1152+
}
1153+
}
1154+
local data, errors = check_request(query, query_schema)
1155+
t.assert_equals(data, {test = 'A'})
1156+
t.assert_equals(errors, {{message = 'Simple error'}})
1157+
end

0 commit comments

Comments
 (0)