Skip to content

Support nested execution contexts and contexts created in a separate async execution within the same stack #2459

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions packages/graphql-modules/tests/execution-context.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
testkit,
} from '../src';

import { executionContext } from '../src/application/execution-context';
import {
getExecutionContextDependencyStore,
getExecutionContextStore,
Expand Down Expand Up @@ -510,3 +511,66 @@ test('accessing a singleton provider with execution context in another singleton
getDependencyName: expectedName,
});
});

it('should provide context when created in separate async execution within same stack', async () => {
const create = async () =>
executionContext.create({
getApplicationContext() {
return 'app' as any;
},
getModuleContext() {
return 'mod' as any;
},
});
const destroy = await create();

expect(executionContext.getApplicationContext()).toBe('app');
expect(executionContext.getModuleContext('')).toBe('mod');

destroy();
});

it('should provide context in nested async execution', async () => {
let destroy = () => {
// noop
};
await new Promise<void>((resolve) => {
destroy = executionContext.create({
getApplicationContext() {
return 'app' as any;
},
getModuleContext() {
return 'mod' as any;
},
});
resolve();
})
.then(() => {
expect(executionContext.getApplicationContext()).toBe('app');
expect(executionContext.getModuleContext('')).toBe('mod');
})
.finally(() => {
destroy();
});
});

it('should provide nested contexts', async () => {
const createPicker = (i: number) => ({
getApplicationContext() {
return ('app ' + i) as any;
},
getModuleContext() {
return ('mod ' + i) as any;
},
});

const destroy0 = executionContext.create(createPicker(0));
expect(executionContext.getApplicationContext()).toBe('app 0');

const destroy1 = executionContext.create(createPicker(1));
expect(executionContext.getApplicationContext()).toBe('app 1');
destroy1();

expect(executionContext.getApplicationContext()).toBe('app 0');
destroy0();
});