Skip to content

Commit 526d563

Browse files
committed
use complete helpers within executeField
1 parent 09181f2 commit 526d563

File tree

2 files changed

+39
-53
lines changed

2 files changed

+39
-53
lines changed

src/execution/__tests__/nonnull-test.ts

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,16 @@ describe('Execute: handles non-nullable types', () => {
259259
path: ['syncNest', 'syncNest', 'sync'],
260260
locations: [{ line: 6, column: 22 }],
261261
},
262+
{
263+
message: promiseError.message,
264+
path: ['syncNest', 'promise'],
265+
locations: [{ line: 5, column: 11 }],
266+
},
267+
{
268+
message: promiseError.message,
269+
path: ['syncNest', 'syncNest', 'promise'],
270+
locations: [{ line: 6, column: 27 }],
271+
},
262272
{
263273
message: syncError.message,
264274
path: ['syncNest', 'promiseNest', 'sync'],
@@ -274,21 +284,6 @@ describe('Execute: handles non-nullable types', () => {
274284
path: ['promiseNest', 'syncNest', 'sync'],
275285
locations: [{ line: 12, column: 22 }],
276286
},
277-
{
278-
message: promiseError.message,
279-
path: ['syncNest', 'promise'],
280-
locations: [{ line: 5, column: 11 }],
281-
},
282-
{
283-
message: promiseError.message,
284-
path: ['syncNest', 'syncNest', 'promise'],
285-
locations: [{ line: 6, column: 27 }],
286-
},
287-
{
288-
message: syncError.message,
289-
path: ['promiseNest', 'promiseNest', 'sync'],
290-
locations: [{ line: 13, column: 25 }],
291-
},
292287
{
293288
message: promiseError.message,
294289
path: ['syncNest', 'promiseNest', 'promise'],
@@ -304,6 +299,11 @@ describe('Execute: handles non-nullable types', () => {
304299
path: ['promiseNest', 'syncNest', 'promise'],
305300
locations: [{ line: 12, column: 27 }],
306301
},
302+
{
303+
message: syncError.message,
304+
path: ['promiseNest', 'promiseNest', 'sync'],
305+
locations: [{ line: 13, column: 25 }],
306+
},
307307
{
308308
message: promiseError.message,
309309
path: ['promiseNest', 'promiseNest', 'promise'],

src/execution/execute.ts

Lines changed: 24 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -690,6 +690,7 @@ function executeField(
690690
);
691691

692692
// Get the resolve function, regardless of if its result is normal or abrupt (error).
693+
let result: PromiseOrValue<unknown>;
693694
try {
694695
// Build a JS object of arguments from the field.arguments AST, using the
695696
// variables scope to fulfill any variable references.
@@ -705,50 +706,35 @@ function executeField(
705706
// used to represent an authenticated user, or request-specific caches.
706707
const contextValue = exeContext.contextValue;
707708

708-
const result = resolveFn(source, args, contextValue, info);
709-
710-
let completed;
711-
if (isPromise(result)) {
712-
completed = result.then((resolved) =>
713-
completeValue(
714-
exeContext,
715-
returnType,
716-
fieldNodes,
717-
info,
718-
path,
719-
resolved,
720-
asyncPayloadRecord,
721-
),
722-
);
723-
} else {
724-
completed = completeValue(
725-
exeContext,
726-
returnType,
727-
fieldNodes,
728-
info,
729-
path,
730-
result,
731-
asyncPayloadRecord,
732-
);
733-
}
734-
735-
if (isPromise(completed)) {
736-
// Note: we don't rely on a `catch` method, but we do expect "thenable"
737-
// to take a second callback for the error case.
738-
return completed.then(undefined, (rawError) => {
739-
const error = locatedError(rawError, fieldNodes, pathToArray(path));
740-
const handledError = handleFieldError(error, returnType, errors);
741-
filterSubsequentPayloads(exeContext, path, asyncPayloadRecord);
742-
return handledError;
743-
});
744-
}
745-
return completed;
709+
result = resolveFn(source, args, contextValue, info);
746710
} catch (rawError) {
747711
const error = locatedError(rawError, fieldNodes, pathToArray(path));
748712
const handledError = handleFieldError(error, returnType, errors);
749713
filterSubsequentPayloads(exeContext, path, asyncPayloadRecord);
750714
return handledError;
751715
}
716+
717+
if (isPromise(result)) {
718+
return completePromiseCatchingErrors(
719+
exeContext,
720+
returnType,
721+
fieldNodes,
722+
info,
723+
path,
724+
result,
725+
asyncPayloadRecord,
726+
);
727+
}
728+
729+
return completeValueCatchingErrors(
730+
exeContext,
731+
returnType,
732+
fieldNodes,
733+
info,
734+
path,
735+
result,
736+
asyncPayloadRecord,
737+
);
752738
}
753739

754740
/**

0 commit comments

Comments
 (0)