Skip to content

Commit

Permalink
remove completePromisedValue
Browse files Browse the repository at this point in the history
  • Loading branch information
yaacovCR committed Apr 8, 2024
1 parent d996635 commit 5c427d1
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 92 deletions.
30 changes: 15 additions & 15 deletions src/execution/__tests__/nonnull-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,16 +259,6 @@ describe('Execute: handles non-nullable types', () => {
path: ['syncNest', 'syncNest', 'sync'],
locations: [{ line: 6, column: 22 }],
},
{
message: promiseError.message,
path: ['syncNest', 'promise'],
locations: [{ line: 5, column: 11 }],
},
{
message: promiseError.message,
path: ['syncNest', 'syncNest', 'promise'],
locations: [{ line: 6, column: 27 }],
},
{
message: syncError.message,
path: ['syncNest', 'promiseNest', 'sync'],
Expand All @@ -284,6 +274,21 @@ describe('Execute: handles non-nullable types', () => {
path: ['promiseNest', 'syncNest', 'sync'],
locations: [{ line: 12, column: 22 }],
},
{
message: promiseError.message,
path: ['syncNest', 'promise'],
locations: [{ line: 5, column: 11 }],
},
{
message: promiseError.message,
path: ['syncNest', 'syncNest', 'promise'],
locations: [{ line: 6, column: 27 }],
},
{
message: syncError.message,
path: ['promiseNest', 'promiseNest', 'sync'],
locations: [{ line: 13, column: 25 }],
},
{
message: promiseError.message,
path: ['syncNest', 'promiseNest', 'promise'],
Expand All @@ -299,11 +304,6 @@ describe('Execute: handles non-nullable types', () => {
path: ['promiseNest', 'syncNest', 'promise'],
locations: [{ line: 12, column: 27 }],
},
{
message: syncError.message,
path: ['promiseNest', 'promiseNest', 'sync'],
locations: [{ line: 13, column: 25 }],
},
{
message: promiseError.message,
path: ['promiseNest', 'promiseNest', 'promise'],
Expand Down
167 changes: 90 additions & 77 deletions src/execution/execute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -748,15 +748,33 @@ function executeField(
const result = resolveFn(source, args, contextValue, info);

if (isPromise(result)) {
return completePromisedValue(
exeContext,
returnType,
fieldGroup,
info,
path,
result,
incrementalContext,
deferMap,
return (
result
.then((resolved) =>
completeValue(
exeContext,
returnType,
fieldGroup,
info,
path,
resolved,
incrementalContext,
deferMap,
),
)
// Note: we don't rely on a `catch` method, but we do expect "thenable"
// to take a second callback for the error case.
.then(undefined, (rawError) => {
handleFieldError(
rawError,
exeContext,
returnType,
fieldGroup,
path,
incrementalContext,
);
return null;
})
);
}

Expand Down Expand Up @@ -972,46 +990,6 @@ function completeValue(
);
}

async function completePromisedValue(
exeContext: ExecutionContext,
returnType: GraphQLOutputType,
fieldGroup: FieldGroup,
info: GraphQLResolveInfo,
path: Path,
result: Promise<unknown>,
incrementalContext: IncrementalContext | undefined,
deferMap: ReadonlyMap<DeferUsage, DeferredFragmentRecord> | undefined,
): Promise<unknown> {
try {
const resolved = await result;
let completed = completeValue(
exeContext,
returnType,
fieldGroup,
info,
path,
resolved,
incrementalContext,
deferMap,
);

if (isPromise(completed)) {
completed = await completed;
}
return completed;
} catch (rawError) {
handleFieldError(
rawError,
exeContext,
returnType,
fieldGroup,
path,
incrementalContext,
);
return null;
}
}

/**
* Returns an object containing info for streaming if a field should be
* streamed based on the experimental flag, stream directive present and
Expand Down Expand Up @@ -1454,16 +1432,32 @@ function completeListItemValue(
): boolean {
if (isPromise(item)) {
completedResults.push(
completePromisedValue(
exeContext,
itemType,
fieldGroup,
info,
itemPath,
item,
incrementalContext,
deferMap,
),
item
.then((resolved) =>
completeValue(
exeContext,
itemType,
fieldGroup,
info,
itemPath,
resolved,
incrementalContext,
deferMap,
),
)
// Note: we don't rely on a `catch` method, but we do expect "thenable"
// to take a second callback for the error case.
.then(undefined, (rawError) => {
handleFieldError(
rawError,
exeContext,
itemType,
fieldGroup,
itemPath,
incrementalContext,
);
return null;
}),
);
return true;
}
Expand Down Expand Up @@ -2490,24 +2484,43 @@ function completeStreamItems(
itemType: GraphQLOutputType,
): PromiseOrValue<StreamItemsResult> {
if (isPromise(item)) {
return completePromisedValue(
exeContext,
itemType,
fieldGroup,
info,
itemPath,
item,
incrementalContext,
new Map(),
).then(
(resolvedItem) =>
buildStreamItemsResult(incrementalContext, streamRecord, resolvedItem),
(error) => ({
streamRecord,
result: null,
errors: withError(incrementalContext.errors, error),
}),
);
return item
.then((resolved) =>
completeValue(
exeContext,
itemType,
fieldGroup,
info,
itemPath,
resolved,
incrementalContext,
new Map(),
),
)
.then(undefined, (rawError) => {
handleFieldError(
rawError,
exeContext,
itemType,
fieldGroup,
itemPath,
incrementalContext,
);
return null;
})
.then(
(resolvedItem) =>
buildStreamItemsResult(
incrementalContext,
streamRecord,
resolvedItem,
),
(error) => ({
streamRecord,
result: null,
errors: withError(incrementalContext.errors, error),
}),
);
}

let completedItem;
Expand Down

0 comments on commit 5c427d1

Please sign in to comment.