Skip to content

Commit 6828e67

Browse files
committed
feat(mercurius): add plugins tests
1 parent 34ed4c4 commit 6828e67

File tree

15 files changed

+361
-0
lines changed

15 files changed

+361
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { INestApplication } from '@nestjs/common';
2+
import { FastifyAdapter } from '@nestjs/platform-fastify';
3+
import { Test } from '@nestjs/testing';
4+
import { FastifyInstance } from 'fastify';
5+
import { ApplicationModule } from '../plugins/code-first-plugin/app.module';
6+
import { NEW_PLUGIN_URL } from '../plugins/mocks/utils/constants';
7+
8+
describe('Code-first with plugins', () => {
9+
let app: INestApplication;
10+
11+
beforeEach(async () => {
12+
const module = await Test.createTestingModule({
13+
imports: [ApplicationModule],
14+
}).compile();
15+
16+
app = module.createNestApplication(new FastifyAdapter());
17+
await app.init();
18+
});
19+
20+
it('should get the plugin', async () => {
21+
const fastifyInstance: FastifyInstance = app.getHttpAdapter().getInstance();
22+
await fastifyInstance.ready();
23+
24+
const response = await fastifyInstance.inject({
25+
method: 'GET',
26+
url: NEW_PLUGIN_URL,
27+
});
28+
const data = JSON.parse(response.body);
29+
expect(fastifyInstance.printPlugins().includes('mockPlugin')).toBe(true);
30+
expect(response.statusCode).toBe(200);
31+
expect(data.from).toBe(NEW_PLUGIN_URL);
32+
});
33+
34+
it('it should query dog', async () => {
35+
const fastifyInstance: FastifyInstance = app.getHttpAdapter().getInstance();
36+
await fastifyInstance.ready();
37+
38+
const response = await fastifyInstance.graphql(`
39+
{
40+
getAnimalName
41+
}
42+
`);
43+
expect(response.data).toEqual({
44+
getAnimalName: 'dog',
45+
});
46+
});
47+
48+
afterEach(async () => {
49+
await app.close();
50+
});
51+
});
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import { INestApplication } from '@nestjs/common';
2+
import { FastifyAdapter } from '@nestjs/platform-fastify';
3+
import { Test } from '@nestjs/testing';
4+
import { FastifyInstance } from 'fastify';
5+
import { AppModule as PostsModule } from '../plugins/graphql-federation-plugin/posts-service/federation-posts.module';
6+
import { AppModule as UsersModule } from '../plugins/graphql-federation-plugin/users-service/federation-users.module';
7+
import {
8+
BASE_PLUGIN_URL,
9+
NEW_PLUGIN_URL,
10+
} from '../plugins/mocks/utils/constants';
11+
12+
describe('GraphQL Federation with plugins', () => {
13+
let app: INestApplication;
14+
15+
describe('UsersService', () => {
16+
beforeEach(async () => {
17+
const module = await Test.createTestingModule({
18+
imports: [UsersModule],
19+
}).compile();
20+
21+
app = module.createNestApplication(new FastifyAdapter());
22+
await app.init();
23+
});
24+
25+
it('should get the plugin for users', async () => {
26+
const fastifyInstance: FastifyInstance = app
27+
.getHttpAdapter()
28+
.getInstance();
29+
await fastifyInstance.ready();
30+
31+
const response = await fastifyInstance.inject({
32+
method: 'GET',
33+
url: NEW_PLUGIN_URL,
34+
});
35+
const data = JSON.parse(response.body);
36+
expect(fastifyInstance.printPlugins().includes('mockPlugin')).toBe(true);
37+
expect(response.statusCode).toBe(200);
38+
expect(data.from).toBe(NEW_PLUGIN_URL);
39+
});
40+
});
41+
42+
describe('PostsService', () => {
43+
beforeEach(async () => {
44+
const module = await Test.createTestingModule({
45+
imports: [PostsModule],
46+
}).compile();
47+
48+
app = module.createNestApplication(new FastifyAdapter());
49+
await app.init();
50+
});
51+
52+
it('should get the plugin for posts', async () => {
53+
const fastifyInstance: FastifyInstance = app
54+
.getHttpAdapter()
55+
.getInstance();
56+
await fastifyInstance.ready();
57+
58+
const response = await fastifyInstance.inject({
59+
method: 'GET',
60+
url: BASE_PLUGIN_URL,
61+
});
62+
const data = JSON.parse(response.body);
63+
expect(fastifyInstance.printPlugins().includes('mockPlugin')).toBe(true);
64+
expect(response.statusCode).toBe(200);
65+
expect(data.from).toBe(BASE_PLUGIN_URL);
66+
});
67+
});
68+
69+
afterEach(async () => {
70+
await app.close();
71+
});
72+
});
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { INestApplication } from '@nestjs/common';
2+
import { FastifyAdapter } from '@nestjs/platform-fastify';
3+
import { Test } from '@nestjs/testing';
4+
import * as request from 'supertest';
5+
import { AppModule as PostsModule } from '../graphql-federation/posts-service/federation-posts.module';
6+
import { AppModule as UsersModule } from '../graphql-federation/users-service/federation-users.module';
7+
import { AppModule as GatewayModule } from '../plugins/graphql-federation-plugin/gateway/gateway.module';
8+
import { BASE_PLUGIN_URL } from '../plugins/mocks/utils/constants';
9+
10+
describe('GraphQL Gateway', () => {
11+
let postsApp: INestApplication;
12+
let usersApp: INestApplication;
13+
let gatewayApp: INestApplication;
14+
15+
beforeEach(async () => {
16+
const usersModule = await Test.createTestingModule({
17+
imports: [UsersModule],
18+
}).compile();
19+
20+
usersApp = usersModule.createNestApplication(new FastifyAdapter());
21+
await usersApp.listen(3011);
22+
23+
const postsModule = await Test.createTestingModule({
24+
imports: [PostsModule],
25+
}).compile();
26+
27+
postsApp = postsModule.createNestApplication(new FastifyAdapter());
28+
await postsApp.listen(3012);
29+
30+
const gatewayModule = await Test.createTestingModule({
31+
imports: [GatewayModule],
32+
}).compile();
33+
34+
gatewayApp = gatewayModule.createNestApplication(new FastifyAdapter());
35+
await gatewayApp.init();
36+
37+
await gatewayApp.getHttpAdapter().getInstance().ready();
38+
});
39+
40+
it('should get the plugin url', () => {
41+
return request(gatewayApp.getHttpServer())
42+
.get(BASE_PLUGIN_URL)
43+
.expect(200)
44+
.expect('Content-Type', /json/)
45+
.expect({
46+
from: BASE_PLUGIN_URL,
47+
});
48+
});
49+
50+
afterEach(async () => {
51+
await postsApp.close();
52+
await usersApp.close();
53+
await gatewayApp.close();
54+
});
55+
});
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { Module } from '@nestjs/common';
2+
import { GraphQLModule } from '@nestjs/graphql';
3+
import { MercuriusDriverConfig } from '../../../lib';
4+
import { MercuriusDriver } from '../../../lib/drivers';
5+
import { mockPlugin } from '../mocks/mock.plugin';
6+
import { NEW_PLUGIN_URL } from '../mocks/utils/constants';
7+
import { DogsModule } from './dogs/dogs.module';
8+
9+
@Module({
10+
imports: [
11+
DogsModule,
12+
GraphQLModule.forRoot<MercuriusDriverConfig>({
13+
driver: MercuriusDriver,
14+
autoSchemaFile: true,
15+
plugins: [
16+
{
17+
plugin: mockPlugin,
18+
options: {
19+
url: NEW_PLUGIN_URL,
20+
},
21+
},
22+
],
23+
}),
24+
],
25+
})
26+
export class ApplicationModule {}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { Module } from '@nestjs/common';
2+
import { DogsResolver } from './dogs.resolver';
3+
4+
@Module({
5+
providers: [DogsResolver],
6+
})
7+
export class DogsModule {}
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 DogsResolver {
5+
@Query((returns) => String)
6+
getAnimalName(): string {
7+
return 'dog';
8+
}
9+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { ValidationPipe } from '@nestjs/common';
2+
import { NestFactory } from '@nestjs/core';
3+
import { FastifyAdapter } from '@nestjs/platform-fastify';
4+
import mercurius from 'mercurius';
5+
import { ApplicationModule } from './app.module';
6+
7+
async function bootstrap() {
8+
const app = await NestFactory.create(ApplicationModule, new FastifyAdapter());
9+
app.useGlobalPipes(
10+
new ValidationPipe({
11+
exceptionFactory: (errors) =>
12+
new mercurius.ErrorWithProps('Validation error', { errors }, 200),
13+
}),
14+
);
15+
await app.listen(3010);
16+
}
17+
bootstrap();
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { Module } from '@nestjs/common';
2+
import { GraphQLModule } from '@nestjs/graphql';
3+
import { MercuriusGatewayDriver } from '../../../../lib/drivers';
4+
import { mockPlugin } from '../../mocks/mock.plugin';
5+
6+
@Module({
7+
imports: [
8+
GraphQLModule.forRoot({
9+
driver: MercuriusGatewayDriver,
10+
gateway: {
11+
services: [
12+
{ name: 'users', url: 'http://localhost:3011/graphql' },
13+
{ name: 'posts', url: 'http://localhost:3012/graphql' },
14+
],
15+
},
16+
plugins: [
17+
{
18+
plugin: mockPlugin,
19+
},
20+
],
21+
}),
22+
],
23+
})
24+
export class AppModule {}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { Module } from '@nestjs/common';
2+
import { GraphQLModule } from '@nestjs/graphql';
3+
import { join } from 'path';
4+
import {
5+
MercuriusDriverConfig,
6+
MercuriusFederationDriver,
7+
} from '../../../../lib';
8+
import { PostsModule } from '../../../graphql-federation/posts-service/posts/posts.module';
9+
import { upperDirectiveTransformer } from '../../../graphql-federation/posts-service/posts/upper.directive';
10+
import { mockPlugin } from '../../mocks/mock.plugin';
11+
12+
@Module({
13+
imports: [
14+
GraphQLModule.forRoot<MercuriusDriverConfig>({
15+
driver: MercuriusFederationDriver,
16+
typePaths: [
17+
join(
18+
__dirname,
19+
'../../../graphql-federation/posts-service',
20+
'**/*.graphql',
21+
),
22+
],
23+
transformSchema: (schema) => upperDirectiveTransformer(schema, 'upper'),
24+
federationMetadata: true,
25+
plugins: [
26+
{
27+
plugin: mockPlugin,
28+
},
29+
],
30+
}),
31+
PostsModule,
32+
],
33+
})
34+
export class AppModule {}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { Module } from '@nestjs/common';
2+
import { GraphQLModule } from '@nestjs/graphql';
3+
import { join } from 'path';
4+
import {
5+
MercuriusDriverConfig,
6+
MercuriusFederationDriver,
7+
} from '../../../../lib';
8+
import { UsersModule } from '../../../graphql-federation/users-service/users/users.module';
9+
import { mockPlugin } from '../../mocks/mock.plugin';
10+
import { NEW_PLUGIN_URL } from '../../mocks/utils/constants';
11+
12+
@Module({
13+
imports: [
14+
GraphQLModule.forRoot<MercuriusDriverConfig>({
15+
driver: MercuriusFederationDriver,
16+
typePaths: [
17+
join(
18+
__dirname,
19+
'../../../graphql-federation/users-service',
20+
'**/*.graphql',
21+
),
22+
],
23+
federationMetadata: true,
24+
plugins: [
25+
{
26+
plugin: mockPlugin,
27+
options: {
28+
url: NEW_PLUGIN_URL,
29+
},
30+
},
31+
],
32+
}),
33+
UsersModule,
34+
],
35+
})
36+
export class AppModule {}

0 commit comments

Comments
 (0)