Skip to content

Commit 6512e01

Browse files
Merge branch 'carlevans719-fix-resolver-provider'
2 parents 8c75d13 + 390ca3a commit 6512e01

File tree

7 files changed

+304
-1
lines changed

7 files changed

+304
-1
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { Module } from '@nestjs/common';
2+
import { CatsResolver } from './cats.resolver';
3+
4+
@Module({})
5+
export class CatsModule {
6+
static register(
7+
resolverRegistrationMethod: 'useClass' | 'useFactory' | 'useValue',
8+
) {
9+
switch (resolverRegistrationMethod) {
10+
case 'useClass':
11+
return {
12+
module: CatsModule,
13+
providers: [
14+
{
15+
provide: CatsResolver,
16+
useClass: CatsResolver,
17+
},
18+
],
19+
};
20+
21+
case 'useValue':
22+
return {
23+
module: CatsModule,
24+
providers: [
25+
{
26+
provide: CatsResolver,
27+
useValue: new CatsResolver(),
28+
},
29+
],
30+
};
31+
32+
case 'useFactory':
33+
default:
34+
return {
35+
module: CatsModule,
36+
providers: [
37+
{
38+
provide: CatsResolver,
39+
useFactory() {
40+
return new CatsResolver();
41+
},
42+
},
43+
],
44+
};
45+
}
46+
}
47+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { Query, Resolver } from '@nestjs/graphql';
2+
3+
@Resolver()
4+
export class CatsResolver {
5+
@Query((returns) => String)
6+
getAnimalName(): string {
7+
return 'cat';
8+
}
9+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
import { INestApplication } from '@nestjs/common';
2+
import { Test } from '@nestjs/testing';
3+
import * as request from 'supertest';
4+
import { ApplicationModule } from '../code-first/app.module';
5+
import { CatsModule } from '../code-first/cats/cats.module';
6+
7+
describe('GraphQL - Resolver registration methods', () => {
8+
let app: INestApplication;
9+
10+
describe('useClass', () => {
11+
beforeEach(async () => {
12+
const module = await Test.createTestingModule({
13+
imports: [ApplicationModule, CatsModule.register('useClass')],
14+
}).compile();
15+
16+
app = module.createNestApplication();
17+
await app.init();
18+
});
19+
20+
it('should return the cats result', async () => {
21+
return request(app.getHttpServer())
22+
.post('/graphql')
23+
.send({
24+
operationName: null,
25+
variables: {},
26+
query: 'query {\n getAnimalName \n}\n',
27+
})
28+
.expect(200, {
29+
data: {
30+
getAnimalName: 'cat',
31+
},
32+
});
33+
});
34+
35+
afterEach(async () => {
36+
await app.close();
37+
});
38+
});
39+
40+
describe('useValue', () => {
41+
beforeEach(async () => {
42+
const module = await Test.createTestingModule({
43+
imports: [ApplicationModule, CatsModule.register('useValue')],
44+
}).compile();
45+
46+
app = module.createNestApplication();
47+
await app.init();
48+
});
49+
50+
it('should return the cats result', async () => {
51+
return request(app.getHttpServer())
52+
.post('/graphql')
53+
.send({
54+
operationName: null,
55+
variables: {},
56+
query: 'query {\n getAnimalName \n}\n',
57+
})
58+
.expect(200, {
59+
data: {
60+
getAnimalName: 'cat',
61+
},
62+
});
63+
});
64+
65+
afterEach(async () => {
66+
await app.close();
67+
});
68+
});
69+
70+
describe('useFactory', () => {
71+
beforeEach(async () => {
72+
const module = await Test.createTestingModule({
73+
imports: [ApplicationModule, CatsModule.register('useFactory')],
74+
}).compile();
75+
76+
app = module.createNestApplication();
77+
await app.init();
78+
});
79+
80+
it('should return the cats result', async () => {
81+
return request(app.getHttpServer())
82+
.post('/graphql')
83+
.send({
84+
operationName: null,
85+
variables: {},
86+
query: 'query {\n getAnimalName \n}\n',
87+
})
88+
.expect(200, {
89+
data: {
90+
getAnimalName: 'cat',
91+
},
92+
});
93+
});
94+
95+
afterEach(async () => {
96+
await app.close();
97+
});
98+
});
99+
});

packages/graphql/lib/services/resolvers-explorer.service.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,10 +284,18 @@ export class ResolversExplorerService extends BaseExplorerService {
284284
this.modulesContainer,
285285
this.gqlOptions.include || [],
286286
);
287-
const resolvers = this.flatMap(modules, (instance) => instance.metatype);
287+
const resolvers = this.flatMap(modules, this.mapToCtor).filter(Boolean);
288288
return resolvers;
289289
}
290290

291+
private mapToCtor(wrapper: InstanceWrapper): Function {
292+
const { instance } = wrapper;
293+
if (!instance) {
294+
return undefined;
295+
}
296+
return instance.constructor;
297+
}
298+
291299
private registerContextProvider<T = any>(request: T, contextId: ContextId) {
292300
const coreModuleArray = [...this.modulesContainer.entries()]
293301
.filter(
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { Module } from '@nestjs/common';
2+
import { CatsResolver } from './cats.resolver';
3+
4+
@Module({})
5+
export class CatsModule {
6+
static register(
7+
resolverRegistrationMethod: 'useClass' | 'useFactory' | 'useValue',
8+
) {
9+
switch (resolverRegistrationMethod) {
10+
case 'useClass':
11+
return {
12+
module: CatsModule,
13+
providers: [
14+
{
15+
provide: CatsResolver,
16+
useClass: CatsResolver,
17+
},
18+
],
19+
};
20+
21+
case 'useValue':
22+
return {
23+
module: CatsModule,
24+
providers: [
25+
{
26+
provide: CatsResolver,
27+
useValue: new CatsResolver(),
28+
},
29+
],
30+
};
31+
32+
case 'useFactory':
33+
default:
34+
return {
35+
module: CatsModule,
36+
providers: [
37+
{
38+
provide: CatsResolver,
39+
useFactory() {
40+
return new CatsResolver();
41+
},
42+
},
43+
],
44+
};
45+
}
46+
}
47+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { Query, Resolver } from '@nestjs/graphql';
2+
3+
@Resolver()
4+
export class CatsResolver {
5+
@Query((returns) => String)
6+
getAnimalName(): string {
7+
return 'cat';
8+
}
9+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import { INestApplication } from '@nestjs/common';
2+
import { FastifyAdapter } from '@nestjs/platform-fastify';
3+
import { Test } from '@nestjs/testing';
4+
import { ApplicationModule } from '../code-first/app.module';
5+
import { CatsModule } from '../code-first/cats/cats.module';
6+
7+
describe('GraphQL - Resolver registration methods', () => {
8+
let app: INestApplication;
9+
10+
describe('useClass', () => {
11+
beforeEach(async () => {
12+
const module = await Test.createTestingModule({
13+
imports: [ApplicationModule, CatsModule.register('useClass')],
14+
}).compile();
15+
16+
app = module.createNestApplication(new FastifyAdapter());
17+
await app.init();
18+
await app.getHttpAdapter().getInstance().ready();
19+
});
20+
21+
it('should return the cats result', async () => {
22+
const fastifyInstance = app.getHttpAdapter().getInstance();
23+
const response = await fastifyInstance.graphql(
24+
'query {\n getAnimalName \n}\n',
25+
);
26+
27+
expect(response.data).toEqual({ getAnimalName: 'cat' });
28+
});
29+
30+
afterEach(async () => {
31+
await app.close();
32+
});
33+
});
34+
35+
describe('useValue', () => {
36+
beforeEach(async () => {
37+
const module = await Test.createTestingModule({
38+
imports: [ApplicationModule, CatsModule.register('useValue')],
39+
}).compile();
40+
41+
app = module.createNestApplication(new FastifyAdapter());
42+
await app.init();
43+
await app.getHttpAdapter().getInstance().ready();
44+
});
45+
46+
it('should return the cats result', async () => {
47+
const fastifyInstance = app.getHttpAdapter().getInstance();
48+
const response = await fastifyInstance.graphql(
49+
'query {\n getAnimalName \n}\n',
50+
);
51+
52+
expect(response.data).toEqual({ getAnimalName: 'cat' });
53+
});
54+
55+
afterEach(async () => {
56+
await app.close();
57+
});
58+
});
59+
60+
describe('useFactory', () => {
61+
beforeEach(async () => {
62+
const module = await Test.createTestingModule({
63+
imports: [ApplicationModule, CatsModule.register('useFactory')],
64+
}).compile();
65+
66+
app = module.createNestApplication(new FastifyAdapter());
67+
await app.init();
68+
await app.getHttpAdapter().getInstance().ready();
69+
});
70+
71+
it('should return the cats result', async () => {
72+
const fastifyInstance = app.getHttpAdapter().getInstance();
73+
const response = await fastifyInstance.graphql(
74+
'query {\n getAnimalName \n}\n',
75+
);
76+
77+
expect(response.data).toEqual({ getAnimalName: 'cat' });
78+
});
79+
80+
afterEach(async () => {
81+
await app.close();
82+
});
83+
});
84+
});

0 commit comments

Comments
 (0)