Commit 0708d2a
committed
feat(execution): track pending async work
access finalization of all async work beyond that included in the result via experimental hook
Access the asyncWorkFinished hook via `experimentalHooks` on execution args:
```ts
const result = execute({
schema,
document: parse('{ test }'),
experimentalHooks: {
queryOrMutationOrSubscriptionEventAsyncWorkFinished({
validatedExecutionArgs,
}) {
const operationName =
validatedExecutionArgs.operationName ?? '<anonymous>';
console.log(`async work finished for operation: ${operationName}`);
},
},
});
```
`execute(...)` can return synchronously (for example after a synchronous bubbled
error) while tracked async work is still pending. To always get a Promise that
resolves only after this hook is called:
```ts
const args: ExecutionArgs = {
schema,
document: parse('{ test }'),
};
let hookCalled = false;
let resolveAsyncWorkFinished: (() => void) | undefined;
const asyncWorkFinished = new Promise<void>((resolve) => {
resolveAsyncWorkFinished = resolve;
});
const existingHook =
args.experimentalHooks
?.queryOrMutationOrSubscriptionEventAsyncWorkFinished;
const result = execute({
...args,
experimentalHooks: {
...(args.experimentalHooks ?? {}),
queryOrMutationOrSubscriptionEventAsyncWorkFinished(info) {
try {
existingHook?.(info);
} finally {
hookCalled = true;
resolveAsyncWorkFinished?.();
}
},
},
});
const resultAfterAsyncWorkFinished: Promise<ExecutionResult> =
Promise.resolve(result).then((executionResult) =>
hookCalled
? executionResult
: asyncWorkFinished.then(() => executionResult),
);
```
This is safe whether `result` is sync or async, and whether the hook callback is
reached immediately or later.
Async work started inside resolvers is not automatically guaranteed to be tracked.
For example, `Promise.all([...])` rejects on the first rejection, so sibling promises
may still be pending after `execute(...)` has already produced a result.
Use `info.getAsyncHelpers()` to include that work in async tracking:
```ts
resolve(_source, _args, _context, info) {
const { promiseAll } = info.getAsyncHelpers();
return promiseAll([
fetchFromA(),
fetchFromB(),
fetchFromC(),
]);
}
```
`promiseAny` and `promiseRace` similarly track non-winning/non-settled promises,
and `trackPromise` can be used for any additional Promise you want included:
```ts
resolve(_source, _args, _context, info) {
const { trackPromise } = info.getAsyncHelpers();
trackPromise(cleanupAfterResponse());
return 'ok';
}
```1 parent d03118a commit 0708d2a
14 files changed
Lines changed: 819 additions & 87 deletions
File tree
- src
- execution
- __tests__
- incremental
- jsutils
- type
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
32 | 32 | | |
33 | 33 | | |
34 | 34 | | |
| 35 | + | |
35 | 36 | | |
36 | 37 | | |
37 | 38 | | |
| |||
60 | 61 | | |
61 | 62 | | |
62 | 63 | | |
| 64 | + | |
| 65 | + | |
63 | 66 | | |
64 | 67 | | |
65 | 68 | | |
| |||
115 | 118 | | |
116 | 119 | | |
117 | 120 | | |
| 121 | + | |
118 | 122 | | |
119 | 123 | | |
120 | 124 | | |
| |||
231 | 235 | | |
232 | 236 | | |
233 | 237 | | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
234 | 243 | | |
235 | 244 | | |
236 | 245 | | |
| |||
249 | 258 | | |
250 | 259 | | |
251 | 260 | | |
252 | | - | |
| 261 | + | |
| 262 | + | |
253 | 263 | | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
254 | 267 | | |
255 | 268 | | |
256 | 269 | | |
| |||
261 | 274 | | |
262 | 275 | | |
263 | 276 | | |
264 | | - | |
265 | | - | |
266 | | - | |
267 | | - | |
| 277 | + | |
268 | 278 | | |
269 | 279 | | |
270 | 280 | | |
| |||
326 | 336 | | |
327 | 337 | | |
328 | 338 | | |
| 339 | + | |
329 | 340 | | |
330 | 341 | | |
331 | 342 | | |
| |||
349 | 360 | | |
350 | 361 | | |
351 | 362 | | |
352 | | - | |
| 363 | + | |
353 | 364 | | |
354 | 365 | | |
355 | 366 | | |
| |||
359 | 370 | | |
360 | 371 | | |
361 | 372 | | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
362 | 380 | | |
363 | 381 | | |
364 | 382 | | |
| 383 | + | |
| 384 | + | |
| 385 | + | |
| 386 | + | |
| 387 | + | |
| 388 | + | |
| 389 | + | |
365 | 390 | | |
366 | 391 | | |
367 | 392 | | |
| |||
508 | 533 | | |
509 | 534 | | |
510 | 535 | | |
511 | | - | |
512 | | - | |
| 536 | + | |
| 537 | + | |
| 538 | + | |
513 | 539 | | |
514 | 540 | | |
515 | 541 | | |
| |||
522 | 548 | | |
523 | 549 | | |
524 | 550 | | |
525 | | - | |
| 551 | + | |
526 | 552 | | |
527 | 553 | | |
528 | 554 | | |
| |||
559 | 585 | | |
560 | 586 | | |
561 | 587 | | |
| 588 | + | |
562 | 589 | | |
563 | 590 | | |
564 | 591 | | |
| |||
855 | 882 | | |
856 | 883 | | |
857 | 884 | | |
858 | | - | |
859 | | - | |
| 885 | + | |
860 | 886 | | |
861 | | - | |
| 887 | + | |
| 888 | + | |
| 889 | + | |
862 | 890 | | |
863 | 891 | | |
864 | 892 | | |
865 | 893 | | |
866 | 894 | | |
867 | 895 | | |
868 | 896 | | |
869 | | - | |
870 | | - | |
| 897 | + | |
871 | 898 | | |
872 | 899 | | |
873 | 900 | | |
874 | 901 | | |
875 | | - | |
| 902 | + | |
| 903 | + | |
| 904 | + | |
876 | 905 | | |
877 | 906 | | |
878 | 907 | | |
| |||
993 | 1022 | | |
994 | 1023 | | |
995 | 1024 | | |
996 | | - | |
997 | | - | |
998 | | - | |
999 | | - | |
| 1025 | + | |
| 1026 | + | |
| 1027 | + | |
1000 | 1028 | | |
| 1029 | + | |
1001 | 1030 | | |
1002 | 1031 | | |
1003 | 1032 | | |
1004 | | - | |
| 1033 | + | |
| 1034 | + | |
| 1035 | + | |
1005 | 1036 | | |
1006 | 1037 | | |
1007 | 1038 | | |
| |||
0 commit comments