Support nested execution contexts and contexts created in a separate async execution within the same stack #2459
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.
Closes #2227
Often found behaviour, especially when using
@envelop/graphql-modules
:onContextBuilding
creates,onExecute
uses)createOperationController
, they are creating a nested context since the plugin itself [already created an operation controller]](https://github.com/n1ru4l/envelop/blob/main/packages/plugins/graphql-modules/src/index.ts#L19C1-L22)Note that creating an operation controller creates an execution context.
The current implementation of execution context handler has 2 work modes: using
AsyncLocalStorage.enterWith
(Node v14+) orasync_hooks.createHook
(fallback).When using
AsyncLocalStorage.enterWith
, 1. and 2. will work because it binds to the stack. However, 3. will create wonky behaviour (probably related to nodejs/node#45848 and nodejs/help#3041). Node doesn't recommend usingAsyncLocalStorage.enterWith
because if its "magical" behaviour, reduced visibility and harder debugging (see comments from the related issues above). Additionally, even thoughAsyncLocalStorage.enterWith
was introduced with Node v14, it's still considered unstable and Node's team doesn't want to promote it to stable because of the aforementioned reasons (see nodejs/node#35286). AsyncLocalStorage was introduced in #2395.On the other hand, when using
async_hooks.createHook
, both 1., 2. and 3. will fail. Where 1. and 2. is much more important (because of@envelop/graphql-modules
). The reason it fails is because of how the createHook's init is implemented - it only considers nested async executions, not parallel ones within the same stack.TODO