Skip to content
Merged
Show file tree
Hide file tree
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
8 changes: 8 additions & 0 deletions packages/apollo/tests/e2e/request-scoped.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,14 @@ describe('Request scope', () => {
expect(Guard.COUNTER).toEqual(3);
expect(Guard.REQUEST_SCOPED_DATA).toEqual([1, 1, 1]);
});

it(`should inject REQUEST context into request-scoped services`, async () => {
expect(UsersService.REQUEST_CONTEXTS).toHaveLength(3);
UsersService.REQUEST_CONTEXTS.forEach((context) => {
expect(context).toBeDefined();
expect(context.req).toBeDefined();
});
});
});

afterAll(async () => {
Expand Down
9 changes: 8 additions & 1 deletion packages/apollo/tests/graphql/hello/users/users.service.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
import { Inject, Injectable, Scope } from '@nestjs/common';
import { CONTEXT } from '@nestjs/graphql';

@Injectable({ scope: Scope.REQUEST })
export class UsersService {
static COUNTER = 0;
constructor(@Inject('META') private readonly meta) {
static REQUEST_CONTEXTS: any[] = [];

constructor(
@Inject('META') private readonly meta,
@Inject(CONTEXT) private readonly context: any,
) {
UsersService.COUNTER++;
UsersService.REQUEST_CONTEXTS.push(this.context);
}

findById(id: string) {
Expand Down
23 changes: 14 additions & 9 deletions packages/graphql/lib/services/resolvers-explorer.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export class ResolversExplorerService extends BaseExplorerService {
private readonly logger = new Logger(ResolversExplorerService.name);
private readonly gqlParamsFactory = new GqlParamsFactory();
private readonly injector = new Injector();
private coreModuleRef: Module | null | undefined;

constructor(
private readonly modulesContainer: ModulesContainer,
Expand Down Expand Up @@ -298,18 +299,22 @@ export class ResolversExplorerService extends BaseExplorerService {
}

private registerContextProvider<T = any>(request: T, contextId: ContextId) {
const coreModuleArray = [...this.modulesContainer.entries()]
.filter(
([key, { metatype }]) =>
metatype && metatype.name === InternalCoreModule.name,
)
.map(([key, value]) => value);
if (this.coreModuleRef === undefined) {
const coreModuleArray = [...this.modulesContainer.entries()]
.filter(
([key, { metatype }]) =>
metatype && metatype.name === InternalCoreModule.name,
)
.map(([key, value]) => value);

this.coreModuleRef = head(coreModuleArray) ?? null;
}

const coreModuleRef = head(coreModuleArray);
if (!coreModuleRef) {
if (!this.coreModuleRef) {
return;
}
const wrapper = coreModuleRef.getProviderByKey(REQUEST);

const wrapper = this.coreModuleRef.getProviderByKey(REQUEST);
wrapper.setInstanceByContextId(contextId, {
instance: contextId.getParent ? contextId.payload : request,
isResolved: true,
Expand Down