Skip to content

Commit

Permalink
fix(federation): respect use global prefix option for gateway #1158
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilmysliwiec committed Nov 10, 2020
1 parent e9c14e6 commit 2465370
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions lib/federation/graphql-gateway.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
Provider,
} from '@nestjs/common';
import { loadPackage } from '@nestjs/common/utils/load-package.util';
import { HttpAdapterHost } from '@nestjs/core';
import { ApplicationConfig, HttpAdapterHost } from '@nestjs/core';
import { ApolloServerBase } from 'apollo-server-core';
import { GATEWAY_BUILD_SERVICE } from '.';
import { GRAPHQL_MODULE_ID } from '../graphql.constants';
Expand All @@ -18,7 +18,7 @@ import {
GatewayOptionsFactory,
GqlModuleOptions,
} from '../interfaces';
import { generateString } from '../utils';
import { generateString, normalizeRoutePath } from '../utils';
import { GRAPHQL_GATEWAY_MODULE_OPTIONS } from './federation.constants';

@Module({})
Expand All @@ -33,6 +33,7 @@ export class GraphQLGatewayModule implements OnModuleInit {
private readonly buildService: GatewayBuildService,
@Inject(GRAPHQL_GATEWAY_MODULE_OPTIONS)
private readonly options: GatewayModuleOptions,
private readonly applicationConfig: ApplicationConfig,
) {}

static forRoot(options: GatewayModuleOptions): DynamicModule {
Expand Down Expand Up @@ -158,9 +159,9 @@ export class GraphQLGatewayModule implements OnModuleInit {
onHealthCheck,
cors,
bodyParserConfig,
path,
} = apolloOptions;
const app = this.httpAdapterHost.httpAdapter.getInstance();
const path = this.getNormalizedPath(apolloOptions);

const apolloServer = new ApolloServer(apolloOptions);
apolloServer.applyMiddleware({
Expand All @@ -183,15 +184,16 @@ export class GraphQLGatewayModule implements OnModuleInit {

const httpAdapter = this.httpAdapterHost.httpAdapter;
const app = httpAdapter.getInstance();
const path = this.getNormalizedPath(apolloOptions);

const apolloServer = new ApolloServer(apolloOptions);
const {
disableHealthCheck,
onHealthCheck,
cors,
bodyParserConfig,
path,
} = apolloOptions;

await app.register(
apolloServer.createHandler({
disableHealthCheck,
Expand All @@ -204,4 +206,13 @@ export class GraphQLGatewayModule implements OnModuleInit {

this.apolloServer = apolloServer;
}

private getNormalizedPath(apolloOptions: GqlModuleOptions): string {
const prefix = this.applicationConfig.getGlobalPrefix();
const useGlobalPrefix = prefix && this.options.server?.useGlobalPrefix;
const gqlOptionsPath = normalizeRoutePath(apolloOptions.path);
return useGlobalPrefix
? normalizeRoutePath(prefix) + gqlOptionsPath
: gqlOptionsPath;
}
}

0 comments on commit 2465370

Please sign in to comment.