Skip to content

Commit

Permalink
test: add test for injecting custom buildservice into gateway
Browse files Browse the repository at this point in the history
  • Loading branch information
rickdgeerling committed Oct 30, 2019
1 parent 5481ef0 commit 0f56138
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/interfaces/gql-gateway-module-options.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export interface GatewayOptionsFactory {
export interface GatewayModuleAsyncOptions extends Pick<ModuleMetadata, 'imports'> {
useExisting?: Type<GatewayOptionsFactory>;
useClass?: Type<GatewayOptionsFactory>;
useFactory?: (...args: any[]) => Promise<GqlModuleOptions> | GqlModuleOptions;
useFactory?: (...args: any[]) => Promise<GatewayModuleOptions> | GatewayModuleOptions;
inject?: any[];
}

Expand Down
87 changes: 87 additions & 0 deletions tests/e2e/graphql-gateway-buildservice.spec.ts
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 tests/graphql-federation/gateway/gateway-buildservice.module.ts
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 {}

0 comments on commit 0f56138

Please sign in to comment.