Skip to content

Commit

Permalink
Update Loader to use loader class types instead name strings
Browse files Browse the repository at this point in the history
This is a requirement from Nest 8 change where classes are registered
by class instance instead of class name.
nestjs/nest#6141
nestjs/nest#5591
  • Loading branch information
CarsonF committed Sep 18, 2021
1 parent 9f2d7db commit 2c7a098
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 35 deletions.
50 changes: 30 additions & 20 deletions src/core/data-loader/data-loader.interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ import {
ExecutionContext,
Injectable,
NestInterceptor,
Type,
} from '@nestjs/common';
import { ContextIdFactory, ModuleRef } from '@nestjs/core';
import { GqlContextType, GqlExecutionContext } from '@nestjs/graphql';
import { Observable } from 'rxjs';
import { ServerException } from '../../common';
import { NEST_LOADER_CONTEXT_KEY } from './constants';
import { NestDataLoader } from './loader.decorator';
import { DataLoader, NestDataLoader } from './loader.decorator';

@Injectable()
export class DataLoaderInterceptor implements NestInterceptor {
Expand All @@ -25,26 +26,35 @@ export class DataLoaderInterceptor implements NestInterceptor {
if (ctx[NEST_LOADER_CONTEXT_KEY] === undefined) {
ctx[NEST_LOADER_CONTEXT_KEY] = {
contextId: ContextIdFactory.create(),
getLoader: (type: string): Promise<NestDataLoader<any, any>> => {
if (ctx[type] === undefined) {
ctx[type] = (async () => {
try {
return (
await this.moduleRef.resolve<NestDataLoader<any, any>>(
type,
ctx[NEST_LOADER_CONTEXT_KEY].contextId,
{ strict: false }
)
).generateDataLoader(ctx);
} catch (e) {
throw new ServerException(
`The loader ${type} is not provided`,
e
);
}
})();
loaders: new Map<
Type<NestDataLoader<any, any>>,
DataLoader<any, any>
>(),
getLoader: (
type: Type<NestDataLoader<any, any>>
): Promise<NestDataLoader<any, any>> => {
if (!ctx[NEST_LOADER_CONTEXT_KEY].loaders.has(type)) {
ctx[NEST_LOADER_CONTEXT_KEY].loaders.set(
type,
(async () => {
try {
return (
await this.moduleRef.resolve<NestDataLoader<any, any>>(
type,
ctx[NEST_LOADER_CONTEXT_KEY].contextId,
{ strict: false }
)
).generateDataLoader(ctx);
} catch (e) {
throw new ServerException(
`The loader ${type.name} is not provided`,
e
);
}
})()
);
}
return ctx[type];
return ctx[NEST_LOADER_CONTEXT_KEY].loaders.get(type);
},
};
}
Expand Down
19 changes: 4 additions & 15 deletions src/core/data-loader/loader.decorator.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
import { createParamDecorator, ExecutionContext } from '@nestjs/common';
import { createParamDecorator, ExecutionContext, Type } from '@nestjs/common';
import { APP_INTERCEPTOR } from '@nestjs/core';
import {
GqlExecutionContext,
GqlContextType as GqlRequestType,
} from '@nestjs/graphql';
// eslint-disable-next-line no-restricted-imports -- the one spot we do want to import it
import * as DataLoaderLib from 'dataloader';
import {
AbstractClassType,
GqlContextType,
ID,
ServerException,
} from '../../common';
import { GqlContextType, ID, ServerException } from '../../common';
import { NEST_LOADER_CONTEXT_KEY } from './constants';
import { DataLoaderInterceptor } from './data-loader.interceptor';

Expand Down Expand Up @@ -59,13 +54,7 @@ export interface NestDataLoader<T, Key = ID, CachedKey = Key> {
* The decorator to be used within your graphql method.
*/
export const Loader = createParamDecorator(
(data: AbstractClassType<any>, context: ExecutionContext) => {
let name = data?.name;
if (!name) {
throw new ServerException(`Invalid name provider to @Loader ('${name}')`);
}
name += 'Loader';

(type: Type<NestDataLoader<any, any>>, context: ExecutionContext) => {
if (context.getType<GqlRequestType>() !== 'graphql') {
throw new ServerException(
'@Loader should only be used within the GraphQL context'
Expand All @@ -79,6 +68,6 @@ export const Loader = createParamDecorator(
);
}

return ctx[NEST_LOADER_CONTEXT_KEY].getLoader(name);
return ctx[NEST_LOADER_CONTEXT_KEY].getLoader(type);
}
);

0 comments on commit 2c7a098

Please sign in to comment.