-
Notifications
You must be signed in to change notification settings - Fork 398
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add test for injecting custom buildservice into gateway
- Loading branch information
1 parent
5481ef0
commit 0f56138
Showing
3 changed files
with
122 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import { INestApplication } from '@nestjs/common'; | ||
import { Test } from '@nestjs/testing'; | ||
import * as request from 'supertest'; | ||
import { AppModule as PostsModule } from '../graphql-federation/posts-service/federation-posts.module'; | ||
import { AppModule as UsersModule } from '../graphql-federation/users-service/federation-users.module'; | ||
import { AppModule as GatewayModule } from '../graphql-federation/gateway/gateway-buildservice.module'; | ||
import { RemoteGraphQLDataSource } from '@apollo/gateway'; | ||
import { GRAPHQL_GATEWAY_BUILD_SERVICE } from '../../lib/graphql.constants'; | ||
|
||
describe('GraphQL Gateway buildservice', () => { | ||
let postsApp: INestApplication; | ||
let usersApp: INestApplication; | ||
let gatewayApp: INestApplication; | ||
let buildServiceHook: Function; | ||
|
||
beforeEach(async () => { | ||
buildServiceHook = jest.fn(); | ||
const usersModule = await Test.createTestingModule({ | ||
imports: [UsersModule], | ||
}).compile(); | ||
|
||
usersApp = usersModule.createNestApplication(); | ||
await usersApp.listenAsync(3001); | ||
|
||
const postsModule = await Test.createTestingModule({ | ||
imports: [PostsModule], | ||
}).compile(); | ||
|
||
postsApp = postsModule.createNestApplication(); | ||
await postsApp.listenAsync(3002); | ||
|
||
const gatewayModule = await Test.createTestingModule({ | ||
imports: [GatewayModule], | ||
}) | ||
.overrideProvider(GRAPHQL_GATEWAY_BUILD_SERVICE) | ||
.useValue(({ url }) => { | ||
return new RemoteGraphQLDataSource({ url, willSendRequest: buildServiceHook as any }); | ||
}) | ||
.compile(); | ||
|
||
gatewayApp = gatewayModule.createNestApplication(); | ||
await gatewayApp.init(); | ||
}); | ||
|
||
it(`should run query through build service`, async () => { | ||
await request(gatewayApp.getHttpServer()) | ||
.post('/graphql') | ||
.send({ | ||
operationName: null, | ||
variables: {}, | ||
query: ` | ||
{ | ||
getPosts { | ||
id, | ||
title, | ||
body, | ||
user { | ||
id, | ||
name, | ||
} | ||
} | ||
}`, | ||
}) | ||
.expect(200, { | ||
data: { | ||
getPosts: [ | ||
{ | ||
id: '1', | ||
title: 'Hello world', | ||
body: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.', | ||
user: { | ||
id: '5', | ||
name: 'GraphQL', | ||
}, | ||
}, | ||
], | ||
}, | ||
}); | ||
expect(buildServiceHook).toHaveBeenCalled(); | ||
}); | ||
|
||
afterEach(async () => { | ||
await postsApp.close(); | ||
await usersApp.close(); | ||
await gatewayApp.close(); | ||
}); | ||
}); |
34 changes: 34 additions & 0 deletions
34
tests/graphql-federation/gateway/gateway-buildservice.module.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { GraphQLGatewayModule } from '../../../lib/graphql-gateway.module'; | ||
import { GRAPHQL_GATEWAY_BUILD_SERVICE } from '../../../lib/graphql.constants'; | ||
import { RemoteGraphQLDataSource } from '@apollo/gateway'; | ||
|
||
@Module({ | ||
providers: [ | ||
{ | ||
provide: GRAPHQL_GATEWAY_BUILD_SERVICE, | ||
useValue: ({ name, url }) => { | ||
console.log('BuildService: %s', name); | ||
return new RemoteGraphQLDataSource({ url }); | ||
}, | ||
}, | ||
], | ||
exports: [GRAPHQL_GATEWAY_BUILD_SERVICE], | ||
}) | ||
class BuildServiceModule {} | ||
|
||
@Module({ | ||
imports: [ | ||
GraphQLGatewayModule.forRootAsync({ | ||
useFactory: async () => ({ | ||
serviceList: [ | ||
{ name: 'users', url: 'http://localhost:3001/graphql' }, | ||
{ name: 'posts', url: 'http://localhost:3002/graphql' }, | ||
], | ||
}), | ||
imports: [BuildServiceModule], | ||
inject: [GRAPHQL_GATEWAY_BUILD_SERVICE], | ||
}), | ||
], | ||
}) | ||
export class AppModule {} |