Skip to content

Commit

Permalink
chore(): resolve merge conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilmysliwiec committed Nov 9, 2020
2 parents de276f3 + 91b5034 commit d4493af
Show file tree
Hide file tree
Showing 10 changed files with 208 additions and 158 deletions.
17 changes: 13 additions & 4 deletions lib/federation/graphql-federation.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
DynamicModule,
Inject,
Module,
OnModuleDestroy,
OnModuleInit,
Optional,
Provider,
Expand Down Expand Up @@ -54,8 +55,12 @@ import { GraphQLFederationFactory } from './graphql-federation.factory';
],
exports: [GraphQLSchemaHost, GraphQLTypesLoader, GraphQLAstExplorer],
})
export class GraphQLFederationModule implements OnModuleInit {
private apolloServer: ApolloServerBase;
export class GraphQLFederationModule implements OnModuleInit, OnModuleDestroy {
private _apolloServer: ApolloServerBase;

get apolloServer(): ApolloServerBase {
return this._apolloServer;
}

constructor(
@Optional()
Expand Down Expand Up @@ -173,6 +178,10 @@ export class GraphQLFederationModule implements OnModuleInit {
}
}

async onModuleDestroy() {
await this._apolloServer?.stop();
}

private registerGqlServer(apolloOptions: GqlModuleOptions) {
const httpAdapter = this.httpAdapterHost.httpAdapter;
const platformName = httpAdapter.getType();
Expand Down Expand Up @@ -220,7 +229,7 @@ export class GraphQLFederationModule implements OnModuleInit {
cors,
bodyParserConfig,
});
this.apolloServer = apolloServer;
this._apolloServer = apolloServer;
}

private registerFastify(apolloOptions: GqlModuleOptions) {
Expand Down Expand Up @@ -261,7 +270,7 @@ export class GraphQLFederationModule implements OnModuleInit {
}),
);

this.apolloServer = apolloServer;
this._apolloServer = apolloServer;
}

private getNormalizedPath(apolloOptions: GqlModuleOptions): string {
Expand Down
17 changes: 11 additions & 6 deletions lib/graphql.module.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Inject, Module } from '@nestjs/common';
import {
DynamicModule,
OnModuleInit,
OnModuleDestroy,
OnModuleInit,
Provider,
} from '@nestjs/common/interfaces';
import { loadPackage } from '@nestjs/common/utils/load-package.util';
Expand Down Expand Up @@ -50,7 +50,12 @@ import {
exports: [GraphQLTypesLoader, GraphQLAstExplorer, GraphQLSchemaHost],
})
export class GraphQLModule implements OnModuleInit, OnModuleDestroy {
protected apolloServer: ApolloServerBase;
private _apolloServer: ApolloServerBase;

get apolloServer(): ApolloServerBase {
return this._apolloServer;
}

constructor(
private readonly httpAdapterHost: HttpAdapterHost,
@Inject(GRAPHQL_MODULE_OPTIONS) private readonly options: GqlModuleOptions,
Expand Down Expand Up @@ -149,14 +154,14 @@ export class GraphQLModule implements OnModuleInit, OnModuleDestroy {

this.registerGqlServer(apolloOptions);
if (this.options.installSubscriptionHandlers) {
this.apolloServer.installSubscriptionHandlers(
this._apolloServer.installSubscriptionHandlers(
httpAdapter.getHttpServer(),
);
}
}

async onModuleDestroy() {
await this.apolloServer?.stop();
await this._apolloServer?.stop();
}

private registerGqlServer(apolloOptions: GqlModuleOptions) {
Expand Down Expand Up @@ -199,7 +204,7 @@ export class GraphQLModule implements OnModuleInit, OnModuleDestroy {
bodyParserConfig,
});

this.apolloServer = apolloServer;
this._apolloServer = apolloServer;
}

private registerFastify(apolloOptions: GqlModuleOptions) {
Expand Down Expand Up @@ -230,7 +235,7 @@ export class GraphQLModule implements OnModuleInit, OnModuleDestroy {
}),
);

this.apolloServer = apolloServer;
this._apolloServer = apolloServer;
}

private getNormalizedPath(apolloOptions: GqlModuleOptions): string {
Expand Down
1 change: 1 addition & 0 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ export * from './services/gql-execution-context';
export * from './tokens';
export * from './type-factories';
export * from './type-helpers';
export { getApolloServer } from './utils';
25 changes: 25 additions & 0 deletions lib/utils/get-apollo-server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { INestApplicationContext } from '@nestjs/common';
import { ApolloServerBase } from 'apollo-server-core';
import { GraphQLFederationModule, GraphQLModule } from '..';

/**
* Returns the underlying ApolloServer instance for a given application.
* Supports both Apollo federation and regular GraphQL applications.
* @param app Nest application reference
* @returns Apollo Server instance used by the host application
*/
export function getApolloServer(
app: INestApplicationContext,
): ApolloServerBase {
try {
const graphqlFederationModule = app.get(GraphQLFederationModule);
return graphqlFederationModule.apolloServer;
} catch (error) {}
try {
const graphqlModule = app.get(GraphQLModule);
return graphqlModule.apolloServer;
} catch (error) {}
throw new Error(
'Nest could not find either the GraphQLFederationModule or GraphQLModule. Neither module is registered in the given application.',
);
}
1 change: 1 addition & 0 deletions lib/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export * from './generate-token.util';
export * from './merge-defaults.util';
export * from './normalize-route-path.util';
export * from './remove-temp.util';
export * from './get-apollo-server';
9 changes: 9 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"@typescript-eslint/parser": "4.6.1",
"apollo-server-express": "2.16.1",
"apollo-server-fastify": "2.16.1",
"apollo-server-testing": "2.16.1",
"class-transformer": "0.3.1",
"class-validator": "0.12.2",
"eslint": "7.13.0",
Expand Down
78 changes: 37 additions & 41 deletions tests/e2e/code-first-federation.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { INestApplication } from '@nestjs/common';
import { Test } from '@nestjs/testing';
import * as request from 'supertest';
import { gql } from 'apollo-server-express';
import { ApolloServerTestClient } from 'apollo-server-testing';
import { ApplicationModule } from '../code-first-federation/app.module';
import { createTestClient } from '../utils/create-test-client';

describe('Code-first - Federation', () => {
let app: INestApplication;
let apolloClient: ApolloServerTestClient;

beforeEach(async () => {
const module = await Test.createTestingModule({
Expand All @@ -13,36 +16,30 @@ describe('Code-first - Federation', () => {

app = module.createNestApplication();
await app.init();
apolloClient = createTestClient(module);
});

it(`should return query result`, () => {
return request(app.getHttpServer())
.post('/graphql')
.send({
operationName: null,
variables: {},
query: `
it(`should return query result`, async () => {
const response = await apolloClient.query({
query: gql`
{
_service { sdl }
}`,
})
.expect(200, {
data: {
_service: {
sdl:
'"Search result description"\nunion FederationSearchResultUnion = Post | User\n\ntype Post @key(fields: "id") {\n id: ID!\n title: String!\n authorId: Int!\n}\n\ntype Query {\n findPost(id: Float!): Post!\n getPosts: [Post!]!\n search: [FederationSearchResultUnion!]! @deprecated(reason: "test")\n}\n\ntype User @extends @key(fields: "id") {\n id: ID! @external\n posts: [Post!]!\n}\n',
},
},
});
_service {
sdl
}
}
`,
});
expect(response.data).toEqual({
_service: {
sdl:
'"Search result description"\nunion FederationSearchResultUnion = Post | User\n\ntype Post @key(fields: "id") {\n id: ID!\n title: String!\n authorId: Int!\n}\n\ntype Query {\n findPost(id: Float!): Post!\n getPosts: [Post!]!\n search: [FederationSearchResultUnion!]! @deprecated(reason: "test")\n}\n\ntype User @extends @key(fields: "id") {\n id: ID! @external\n posts: [Post!]!\n}\n',
},
});
});

it('should return the search result', () => {
return request(app.getHttpServer())
.post('/graphql')
.send({
operationName: null,
variables: {},
query: `
it('should return the search result', async () => {
const response = await apolloClient.query({
query: gql`
{
search {
... on Post {
Expand All @@ -53,22 +50,21 @@ describe('Code-first - Federation', () => {
}
__typename
}
}`,
})
.expect(200, {
data: {
search: [
{
id: '1',
__typename: 'User',
},
{
title: 'lorem ipsum',
__typename: 'Post',
},
],
}
`,
});
expect(response.data).toEqual({
search: [
{
id: '1',
__typename: 'User',
},
{
title: 'lorem ipsum',
__typename: 'Post',
},
});
],
});
});

afterEach(async () => {
Expand Down

0 comments on commit d4493af

Please sign in to comment.