From 033ea873c5ab0e7bd3c447da82c720500b6cab3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20My=C5=9Bliwiec?= Date: Tue, 23 Nov 2021 12:38:03 +0100 Subject: [PATCH] feat(): minor tweaks, normalize path by default --- packages/apollo/lib/drivers/apollo-base.driver.ts | 5 ++--- .../graphql/lib/drivers/abstract-graphql.driver.ts | 3 ++- packages/graphql/lib/graphql.module.ts | 11 +++++++---- .../lib/drivers/mercurius-federation.driver.ts | 14 +++----------- .../lib/drivers/mercurius-gateway.driver.ts | 12 +++--------- packages/mercurius/lib/drivers/mercurius.driver.ts | 10 +--------- 6 files changed, 18 insertions(+), 37 deletions(-) diff --git a/packages/apollo/lib/drivers/apollo-base.driver.ts b/packages/apollo/lib/drivers/apollo-base.driver.ts index 8ca70c2b8..2f77b0cba 100644 --- a/packages/apollo/lib/drivers/apollo-base.driver.ts +++ b/packages/apollo/lib/drivers/apollo-base.driver.ts @@ -26,7 +26,7 @@ const apolloPredefinedExceptions: Partial< export abstract class ApolloBaseDriver< T extends Record = ApolloDriverConfig, -> extends AbstractGraphQLDriver { +> extends AbstractGraphQLDriver { protected _apolloServer: ApolloServerBase; get instance(): ApolloServerBase { @@ -106,8 +106,7 @@ export abstract class ApolloBaseDriver< 'GraphQLModule', () => require('apollo-server-express'), ); - const path = this.getNormalizedPath(apolloOptions); - const { disableHealthCheck, onHealthCheck, cors, bodyParserConfig } = + const { disableHealthCheck, path, onHealthCheck, cors, bodyParserConfig } = apolloOptions; const httpAdapter = this.httpAdapterHost.httpAdapter; diff --git a/packages/graphql/lib/drivers/abstract-graphql.driver.ts b/packages/graphql/lib/drivers/abstract-graphql.driver.ts index bd8a70024..67c513108 100644 --- a/packages/graphql/lib/drivers/abstract-graphql.driver.ts +++ b/packages/graphql/lib/drivers/abstract-graphql.driver.ts @@ -5,7 +5,6 @@ import { GqlModuleOptions, GraphQLDriver } from '../interfaces'; import { normalizeRoutePath } from '../utils'; export abstract class AbstractGraphQLDriver< - TDriver = unknown, TOptions extends Record = GqlModuleOptions, > implements GraphQLDriver { @@ -32,6 +31,8 @@ export abstract class AbstractGraphQLDriver< ...defaults, ...options, }; + (clonedOptions as Record).path = + this.getNormalizedPath(clonedOptions); return clonedOptions; } diff --git a/packages/graphql/lib/graphql.module.ts b/packages/graphql/lib/graphql.module.ts index dd46abaa6..7eae3223f 100644 --- a/packages/graphql/lib/graphql.module.ts +++ b/packages/graphql/lib/graphql.module.ts @@ -44,15 +44,18 @@ import { extend, generateString } from './utils'; GraphQLFederationFactory, ], }) -export class GraphQLModule implements OnModuleInit, OnModuleDestroy { - get graphQlAdapter(): AbstractGraphQLDriver { - return this._graphQlAdapter; +export class GraphQLModule< + TAdapter extends AbstractGraphQLDriver = AbstractGraphQLDriver, +> implements OnModuleInit, OnModuleDestroy +{ + get graphQlAdapter(): TAdapter { + return this._graphQlAdapter as TAdapter; } constructor( private readonly httpAdapterHost: HttpAdapterHost, @Inject(GRAPHQL_MODULE_OPTIONS) private readonly options: GqlModuleOptions, - private readonly _graphQlAdapter: AbstractGraphQLDriver, + private readonly _graphQlAdapter: AbstractGraphQLDriver, private readonly graphQlTypesLoader: GraphQLTypesLoader, ) {} diff --git a/packages/mercurius/lib/drivers/mercurius-federation.driver.ts b/packages/mercurius/lib/drivers/mercurius-federation.driver.ts index ca2af229d..7af537624 100644 --- a/packages/mercurius/lib/drivers/mercurius-federation.driver.ts +++ b/packages/mercurius/lib/drivers/mercurius-federation.driver.ts @@ -8,10 +8,7 @@ import mercurius from 'mercurius'; import { MercuriusDriverConfig } from '../interfaces/mercurius-driver-config.interface'; @Injectable() -export class MercuriusFederationDriver extends AbstractGraphQLDriver< - FastifyInstance, - MercuriusDriverConfig -> { +export class MercuriusFederationDriver extends AbstractGraphQLDriver { get instance(): FastifyInstance< Server, IncomingMessage, @@ -27,9 +24,9 @@ export class MercuriusFederationDriver extends AbstractGraphQLDriver< super(); } - public async start(mercuriusOptions: MercuriusDriverConfig) { + public async start(options: MercuriusDriverConfig) { const adapterOptions = await this.graphqlFederationFactory.mergeWithSchema( - mercuriusOptions, + options, ); if (adapterOptions.definitions && adapterOptions.definitions.path) { @@ -45,14 +42,9 @@ export class MercuriusFederationDriver extends AbstractGraphQLDriver< if (platformName !== 'fastify') { throw new Error(`No support for current HttpAdapter: ${platformName}`); } - - const path = this.getNormalizedPath(adapterOptions); const app = httpAdapter.getInstance(); - await app.register(mercurius, { ...adapterOptions, - path, - schema: adapterOptions.schema, }); } diff --git a/packages/mercurius/lib/drivers/mercurius-gateway.driver.ts b/packages/mercurius/lib/drivers/mercurius-gateway.driver.ts index 6ed91190e..a34c2abc4 100644 --- a/packages/mercurius/lib/drivers/mercurius-gateway.driver.ts +++ b/packages/mercurius/lib/drivers/mercurius-gateway.driver.ts @@ -4,10 +4,7 @@ import { IncomingMessage, Server, ServerResponse } from 'http'; import mercurius from 'mercurius'; import { MercuriusDriverConfig } from '../interfaces/mercurius-driver-config.interface'; -export class MercuriusGatewayDriver extends AbstractGraphQLDriver< - FastifyInstance, - MercuriusDriverConfig -> { +export class MercuriusGatewayDriver extends AbstractGraphQLDriver { get instance(): FastifyInstance< Server, IncomingMessage, @@ -17,7 +14,7 @@ export class MercuriusGatewayDriver extends AbstractGraphQLDriver< return this.httpAdapterHost?.httpAdapter?.getInstance?.(); } - public async start(mercuriusOptions: MercuriusDriverConfig) { + public async start(options: MercuriusDriverConfig) { const httpAdapter = this.httpAdapterHost.httpAdapter; const platformName = httpAdapter.getType(); @@ -25,12 +22,9 @@ export class MercuriusGatewayDriver extends AbstractGraphQLDriver< throw new Error(`No support for current HttpAdapter: ${platformName}`); } - const path = this.getNormalizedPath(mercuriusOptions); const app = httpAdapter.getInstance(); - await app.register(mercurius, { - ...mercuriusOptions, - path, + ...options, }); } diff --git a/packages/mercurius/lib/drivers/mercurius.driver.ts b/packages/mercurius/lib/drivers/mercurius.driver.ts index 3e906a35c..76b0214d0 100644 --- a/packages/mercurius/lib/drivers/mercurius.driver.ts +++ b/packages/mercurius/lib/drivers/mercurius.driver.ts @@ -6,10 +6,7 @@ import { IncomingMessage, Server, ServerResponse } from 'http'; import mercurius from 'mercurius'; import { MercuriusDriverConfig } from '../interfaces/mercurius-driver-config.interface'; -export class MercuriusDriver extends AbstractGraphQLDriver< - FastifyInstance, - MercuriusDriverConfig -> { +export class MercuriusDriver extends AbstractGraphQLDriver { get instance(): FastifyInstance< Server, IncomingMessage, @@ -38,14 +35,9 @@ export class MercuriusDriver extends AbstractGraphQLDriver< if (platformName !== 'fastify') { throw new Error(`No support for current HttpAdapter: ${platformName}`); } - - const path = this.getNormalizedPath(options); const app = httpAdapter.getInstance(); - await app.register(mercurius, { ...options, - path, - schema: options.schema, }); }