Skip to content

Commit e5c9a71

Browse files
committed
remove completePromisedValue
1 parent 37c9148 commit e5c9a71

File tree

2 files changed

+104
-91
lines changed

2 files changed

+104
-91
lines changed

src/execution/__tests__/nonnull-test.ts

+15-15
Original file line numberDiff line numberDiff line change
@@ -259,16 +259,6 @@ 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-
},
272262
{
273263
message: syncError.message,
274264
path: ['syncNest', 'promiseNest', 'sync'],
@@ -284,6 +274,21 @@ describe('Execute: handles non-nullable types', () => {
284274
path: ['promiseNest', 'syncNest', 'sync'],
285275
locations: [{ line: 12, column: 22 }],
286276
},
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+
},
287292
{
288293
message: promiseError.message,
289294
path: ['syncNest', 'promiseNest', 'promise'],
@@ -299,11 +304,6 @@ describe('Execute: handles non-nullable types', () => {
299304
path: ['promiseNest', 'syncNest', 'promise'],
300305
locations: [{ line: 12, column: 27 }],
301306
},
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

+89-76
Original file line numberDiff line numberDiff line change
@@ -763,15 +763,33 @@ function executeField(
763763
const result = resolveFn(source, args, contextValue, info);
764764

765765
if (isPromise(result)) {
766-
return completePromisedValue(
767-
exeContext,
768-
returnType,
769-
fieldGroup,
770-
info,
771-
path,
772-
result,
773-
incrementalContext,
774-
deferMap,
766+
return (
767+
result
768+
.then((resolved) =>
769+
completeValue(
770+
exeContext,
771+
returnType,
772+
fieldGroup,
773+
info,
774+
path,
775+
resolved,
776+
incrementalContext,
777+
deferMap,
778+
),
779+
)
780+
// Note: we don't rely on a `catch` method, but we do expect "thenable"
781+
// to take a second callback for the error case.
782+
.then(undefined, (rawError) => {
783+
handleFieldError(
784+
rawError,
785+
exeContext,
786+
returnType,
787+
fieldGroup,
788+
path,
789+
incrementalContext,
790+
);
791+
return null;
792+
})
775793
);
776794
}
777795

@@ -987,46 +1005,6 @@ function completeValue(
9871005
);
9881006
}
9891007

990-
async function completePromisedValue(
991-
exeContext: ExecutionContext,
992-
returnType: GraphQLOutputType,
993-
fieldGroup: FieldGroup,
994-
info: GraphQLResolveInfo,
995-
path: Path,
996-
result: Promise<unknown>,
997-
incrementalContext: IncrementalContext | undefined,
998-
deferMap: ReadonlyMap<DeferUsage, DeferredFragmentRecord> | undefined,
999-
): Promise<unknown> {
1000-
try {
1001-
const resolved = await result;
1002-
let completed = completeValue(
1003-
exeContext,
1004-
returnType,
1005-
fieldGroup,
1006-
info,
1007-
path,
1008-
resolved,
1009-
incrementalContext,
1010-
deferMap,
1011-
);
1012-
1013-
if (isPromise(completed)) {
1014-
completed = await completed;
1015-
}
1016-
return completed;
1017-
} catch (rawError) {
1018-
handleFieldError(
1019-
rawError,
1020-
exeContext,
1021-
returnType,
1022-
fieldGroup,
1023-
path,
1024-
incrementalContext,
1025-
);
1026-
return null;
1027-
}
1028-
}
1029-
10301008
/**
10311009
* Returns an object containing info for streaming if a field should be
10321010
* streamed based on the experimental flag, stream directive present and
@@ -1464,16 +1442,32 @@ function completeListItemValue(
14641442
): boolean {
14651443
if (isPromise(item)) {
14661444
completedResults.push(
1467-
completePromisedValue(
1468-
exeContext,
1469-
itemType,
1470-
fieldGroup,
1471-
info,
1472-
itemPath,
1473-
item,
1474-
incrementalContext,
1475-
deferMap,
1476-
),
1445+
item
1446+
.then((resolved) =>
1447+
completeValue(
1448+
exeContext,
1449+
itemType,
1450+
fieldGroup,
1451+
info,
1452+
itemPath,
1453+
resolved,
1454+
incrementalContext,
1455+
deferMap,
1456+
),
1457+
)
1458+
// Note: we don't rely on a `catch` method, but we do expect "thenable"
1459+
// to take a second callback for the error case.
1460+
.then(undefined, (rawError) => {
1461+
handleFieldError(
1462+
rawError,
1463+
exeContext,
1464+
itemType,
1465+
fieldGroup,
1466+
itemPath,
1467+
incrementalContext,
1468+
);
1469+
return null;
1470+
}),
14771471
);
14781472
return true;
14791473
}
@@ -2510,23 +2504,42 @@ function completeStreamItems(
25102504
itemType: GraphQLOutputType,
25112505
): PromiseOrValue<StreamItemsResult> {
25122506
if (isPromise(item)) {
2513-
return completePromisedValue(
2514-
exeContext,
2515-
itemType,
2516-
fieldGroup,
2517-
info,
2518-
itemPath,
2519-
item,
2520-
incrementalContext,
2521-
new Map(),
2522-
).then(
2523-
(resolvedItem) =>
2524-
buildStreamItemsResult(incrementalContext, streamRecord, resolvedItem),
2525-
(error) => ({
2526-
streamRecord,
2527-
errors: withError(incrementalContext.errors, error),
2528-
}),
2529-
);
2507+
return item
2508+
.then((resolved) =>
2509+
completeValue(
2510+
exeContext,
2511+
itemType,
2512+
fieldGroup,
2513+
info,
2514+
itemPath,
2515+
resolved,
2516+
incrementalContext,
2517+
new Map(),
2518+
),
2519+
)
2520+
.then(undefined, (rawError) => {
2521+
handleFieldError(
2522+
rawError,
2523+
exeContext,
2524+
itemType,
2525+
fieldGroup,
2526+
itemPath,
2527+
incrementalContext,
2528+
);
2529+
return null;
2530+
})
2531+
.then(
2532+
(resolvedItem) =>
2533+
buildStreamItemsResult(
2534+
incrementalContext,
2535+
streamRecord,
2536+
resolvedItem,
2537+
),
2538+
(error) => ({
2539+
streamRecord,
2540+
errors: withError(incrementalContext.errors, error),
2541+
}),
2542+
);
25302543
}
25312544

25322545
let completedItem;

0 commit comments

Comments
 (0)