Skip to content

Commit 94d8b3c

Browse files
committed
refactor: import DataLoader correctly
Replace 'import * as DataLoader' with 'import DataLoader', this causes inconsistencies between the type used locally, and the DataLoader type imported by the client.
1 parent 03e7e85 commit 94d8b3c

File tree

1 file changed

+42
-44
lines changed

1 file changed

+42
-44
lines changed

index.ts

Lines changed: 42 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,25 @@
1-
import * as DataLoader from 'dataloader';
2-
import { NestInterceptor, ExecutionContext, CallHandler, InternalServerErrorException, createParamDecorator } from '@nestjs/common';
3-
import { Injectable } from '@nestjs/common';
4-
import { ModuleRef, APP_INTERCEPTOR } from '@nestjs/core';
1+
import {
2+
CallHandler,
3+
createParamDecorator,
4+
ExecutionContext,
5+
Injectable,
6+
InternalServerErrorException,
7+
NestInterceptor,
8+
} from '@nestjs/common';
9+
import { APP_INTERCEPTOR, ModuleRef } from '@nestjs/core';
510
import { GqlExecutionContext } from '@nestjs/graphql';
11+
import DataLoader from 'dataloader';
612
import { Observable } from 'rxjs';
713

814
/**
915
* This interface will be used to generate the initial data loader.
1016
* The concrete implementation should be added as a provider to your module.
1117
*/
1218
export interface NestDataLoader<ID, Type> {
13-
/**
14-
* Should return a new instance of dataloader each time
15-
*/
16-
generateDataLoader(): DataLoader<ID, Type>;
19+
/**
20+
* Should return a new instance of dataloader each time
21+
*/
22+
generateDataLoader(): DataLoader<ID, Type>;
1723
}
1824

1925
/**
@@ -28,51 +34,43 @@ const NEST_LOADER_CONTEXT_KEY: string = 'NEST_LOADER_CONTEXT_KEY';
2834

2935
@Injectable()
3036
export class DataLoaderInterceptor implements NestInterceptor {
37+
constructor(private readonly moduleRef: ModuleRef) {}
3138

32-
constructor(
33-
private readonly moduleRef: ModuleRef,
34-
) { }
39+
/**
40+
* @inheritdoc
41+
*/
42+
intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
43+
const graphqlExecutionContext = GqlExecutionContext.create(context);
44+
const ctx: any = graphqlExecutionContext.getContext();
3545

36-
/**
37-
* @inheritdoc
38-
*/
39-
intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
40-
const graphqlExecutionContext = GqlExecutionContext.create(context);
41-
const ctx: any = graphqlExecutionContext.getContext();
42-
43-
if (ctx[NEST_LOADER_CONTEXT_KEY] === undefined) {
44-
45-
ctx[NEST_LOADER_CONTEXT_KEY] = (type: string): NestDataLoader<any, any> => {
46-
47-
if (ctx[type] === undefined) {
48-
try {
49-
ctx[type] = this.moduleRef
50-
.get<NestDataLoader<any, any>>(type, { strict: false })
51-
.generateDataLoader();
52-
} catch (e) {
53-
throw new InternalServerErrorException(`The loader ${type} is not provided`);
54-
}
55-
}
56-
57-
return ctx[type];
58-
};
46+
if (ctx[NEST_LOADER_CONTEXT_KEY] === undefined) {
47+
ctx[NEST_LOADER_CONTEXT_KEY] = (type: string): NestDataLoader<any, any> => {
48+
if (ctx[type] === undefined) {
49+
try {
50+
ctx[type] = this.moduleRef
51+
.get<NestDataLoader<any, any>>(type, { strict: false })
52+
.generateDataLoader();
53+
} catch (e) {
54+
throw new InternalServerErrorException(`The loader ${type} is not provided`);
55+
}
5956
}
60-
return next.handle();
57+
58+
return ctx[type];
59+
};
6160
}
61+
return next.handle();
62+
}
6263
}
6364

6465
/**
6566
* The decorator to be used within your graphql method.
6667
*/
67-
export const Loader = createParamDecorator(
68-
(data: string, [_, __, ctx]) => {
69-
70-
if (ctx[NEST_LOADER_CONTEXT_KEY] === undefined) {
71-
throw new InternalServerErrorException(`
68+
export const Loader = createParamDecorator((data: string, [_, __, ctx]) => {
69+
if (ctx[NEST_LOADER_CONTEXT_KEY] === undefined) {
70+
throw new InternalServerErrorException(`
7271
You should provide interceptor ${DataLoaderInterceptor.name} globaly with ${APP_INTERCEPTOR}
7372
`);
74-
}
73+
}
7574

76-
return ctx[NEST_LOADER_CONTEXT_KEY](data);
77-
},
78-
);
75+
return ctx[NEST_LOADER_CONTEXT_KEY](data);
76+
});

0 commit comments

Comments
 (0)