feat(execution): expose asyncWorkFinished execution hook#4658
Merged
yaacovCR merged 1 commit intographql:17.x.xfrom Apr 12, 2026
Merged
feat(execution): expose asyncWorkFinished execution hook#4658yaacovCR merged 1 commit intographql:17.x.xfrom
yaacovCR merged 1 commit intographql:17.x.xfrom
Conversation
|
@yaacovCR is attempting to deploy a commit to the The GraphQL Foundation Team on Vercel. A member of the Team first needs to authorize it. |
This was referenced Apr 5, 2026
fcf5a8a to
419725a
Compare
3f32497 to
4dc9171
Compare
yaacovCR
added a commit
that referenced
this pull request
Apr 12, 2026
canonize async work tracking so promises started during execution are still observed after early errors or abort paths route defaultTypeResolver promise handling through tracked async helpers motivation: - #4658
634c9ea to
0f40f15
Compare
Expose an execution hook that runs once tracked async work has finished,
including work that can outlive a returned execution result.
The hook is available through `hooks.asyncWorkFinished` on execution args.
An `execute` wrapper that waits for all tracked async work can be written as:
```ts
function executeAndWaitForAsyncWorkFinished(
args: ExecutionArgs,
): PromiseOrValue<ExecutionResult> {
let hookHasFired = false;
const { promise: hookFinished, resolve: resolveHookFinished } =
Promise.withResolvers<void>();
const userAsyncWorkFinishedHook = args.hooks?.asyncWorkFinished;
const result = execute({
...args,
hooks: {
...args.hooks,
asyncWorkFinished(info) {
try {
userAsyncWorkFinishedHook?.(info);
} finally {
hookHasFired = true;
resolveHookFinished();
}
},
},
});
return hookHasFired ? result : hookFinished.then(() => result);
}
```
To ensure resolver-side async work is also tracked and awaited, use
`info.getAsyncHelpers().track(...)` or `info.getAsyncHelpers().promiseAll(...)`.
`promiseAll(...)` is optimized for the common case where the returned promise is
awaited (or returned) from the resolver. It only starts tracking on rejection,
and does so as a side-effect. Un-awaited async side effects are an anti-pattern:
```ts
resolve(_source, _args, _context, info) {
const { promiseAll } = info.getAsyncHelpers();
promiseAll([Promise.reject(new Error('bad')), pendingCleanup]).catch(
() => undefined,
);
return 'ok';
}
```
In that anti-pattern, tracking starts only after rejection (on a later
microtask), so this work is not guaranteed to delay
`hooks.asyncWorkFinished`.
Use `track(...)` for un-awaited async side effects:
```ts
resolve(_source, _args, _context, info) {
const { track } = info.getAsyncHelpers();
track([doCleanupAsync().catch(() => undefined)]);
return 'ok';
}
```
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Cancelled async work may still be running even after the result returns. This hook allows interested execution harnesses to track when this async work completes.
Depends on:
The hook is available through
hooks.asyncWorkFinishedon execution args.An
executewrapper that waits for all tracked async work can be written as:To ensure resolver-side async work is also tracked and awaited, use
info.getAsyncHelpers().track(...)orinfo.getAsyncHelpers().promiseAll(...).promiseAll(...)is optimized for the common case where the returned promise is awaited (or returned) from the resolver. It only starts tracking on rejection, and does so as a side-effect. Un-awaited async side effects are an anti-pattern:In that anti-pattern, tracking starts only after rejection (on a later microtask), so this work is not guaranteed to delay
hooks.asyncWorkFinished.Use
track(...)for un-awaited async side effects: