Skip to content

Commit

Permalink
feat(): adapters to drivers
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilmysliwiec committed Nov 9, 2021
1 parent 7aa8761 commit 2012ade
Show file tree
Hide file tree
Showing 47 changed files with 157 additions and 203 deletions.
3 changes: 0 additions & 3 deletions packages/apollo/lib/adapters/index.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { HttpStatus } from '@nestjs/common';
import { loadPackage } from '@nestjs/common/utils/load-package.util';
import { AbstractGraphQLAdapter } from '@nestjs/graphql-experimental/adapters/abstract-graphql.adapter';
import { AbstractGraphQLDriver } from '@nestjs/graphql-experimental/drivers/abstract-graphql.driver';
import { normalizeRoutePath } from '@nestjs/graphql-experimental/utils';
import {
ApolloError,
Expand All @@ -14,7 +14,7 @@ import {
} from 'apollo-server-core';
import { GraphQLError, GraphQLFormattedError } from 'graphql';
import { omit } from 'lodash';
import { ApolloAdapterOptions } from '../interfaces';
import { ApolloDriverConfig } from '../interfaces';

const apolloPredefinedExceptions: Partial<
Record<HttpStatus, typeof ApolloError | typeof UserInputError>
Expand All @@ -24,9 +24,9 @@ const apolloPredefinedExceptions: Partial<
[HttpStatus.FORBIDDEN]: ForbiddenError,
};

export abstract class ApolloGraphQLBaseAdapter<
T extends Record<string, any> = ApolloAdapterOptions,
> extends AbstractGraphQLAdapter<ApolloServerBase, T> {
export abstract class ApolloBaseDriver<
T extends Record<string, any> = ApolloDriverConfig,
> extends AbstractGraphQLDriver<ApolloServerBase, T> {
protected _apolloServer: ApolloServerBase;

get instance(): ApolloServerBase {
Expand All @@ -51,7 +51,7 @@ export abstract class ApolloGraphQLBaseAdapter<
}

public async mergeDefaultOptions(options: T): Promise<T> {
let defaults: ApolloAdapterOptions = {
let defaults: ApolloDriverConfig = {
path: '/graphql',
fieldResolverEnhancers: [],
stopOnTerminationSignals: false,
Expand Down Expand Up @@ -88,7 +88,7 @@ export abstract class ApolloGraphQLBaseAdapter<
omit(defaults, 'plugins'),
);

(options as ApolloAdapterOptions).plugins = (options.plugins || []).concat(
(options as ApolloDriverConfig).plugins = (options.plugins || []).concat(
defaults.plugins || [],
);

Expand Down Expand Up @@ -179,12 +179,12 @@ export abstract class ApolloGraphQLBaseAdapter<
if (options.formatError) {
const origFormatError = options.formatError;
const transformHttpErrorFn = this.createTransformHttpErrorFn();
(options as ApolloAdapterOptions).formatError = (err) => {
(options as ApolloDriverConfig).formatError = (err) => {
err = transformHttpErrorFn(err) as GraphQLError;
return origFormatError(err);
};
} else {
(options as ApolloAdapterOptions).formatError =
(options as ApolloDriverConfig).formatError =
this.createTransformHttpErrorFn();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ import {
ScalarsExplorerService,
} from '@nestjs/graphql-experimental/services';
import { extend } from '@nestjs/graphql-experimental/utils/extend.util';
import { ApolloAdapterOptions } from '..';
import { ApolloDriverConfig } from '..';
import { GraphQLFederationFactory } from '../factories/graphql-federation.factory';
import { PluginsExplorerService } from '../services/plugins-explorer.service';
import { ApolloGraphQLBaseAdapter } from './apollo-graphql-base.adapter';
import { ApolloBaseDriver } from './apollo-base.driver';

@Injectable()
export class ApolloFederationGraphQLAdapter extends ApolloGraphQLBaseAdapter {
export class ApolloFederationDriver extends ApolloBaseDriver {
private readonly graphqlFederationFactory: GraphQLFederationFactory;

constructor(
Expand All @@ -36,7 +36,7 @@ export class ApolloFederationGraphQLAdapter extends ApolloGraphQLBaseAdapter {
);
}

public async start(options: ApolloAdapterOptions): Promise<void> {
public async start(options: ApolloDriverConfig): Promise<void> {
const { printSubgraphSchema } = loadPackage(
'@apollo/subgraph',
'ApolloFederation',
Expand Down Expand Up @@ -72,11 +72,11 @@ export class ApolloFederationGraphQLAdapter extends ApolloGraphQLBaseAdapter {
}
}

public async runPreOptionsHooks(apolloOptions: ApolloAdapterOptions) {
public async runPreOptionsHooks(apolloOptions: ApolloDriverConfig) {
await this.runExecutorFactoryIfPresent(apolloOptions);
}

protected async registerExpress(apolloOptions: ApolloAdapterOptions) {
protected async registerExpress(apolloOptions: ApolloDriverConfig) {
return super.registerExpress(apolloOptions, {
preStartHook: () => {
// If custom directives are provided merge them into schema per Apollo
Expand All @@ -91,7 +91,7 @@ export class ApolloFederationGraphQLAdapter extends ApolloGraphQLBaseAdapter {
});
}

protected async registerFastify(apolloOptions: ApolloAdapterOptions) {
protected async registerFastify(apolloOptions: ApolloDriverConfig) {
return super.registerFastify(apolloOptions, {
preStartHook: () => {
// If custom directives are provided merge them into schema per Apollo
Expand All @@ -106,9 +106,7 @@ export class ApolloFederationGraphQLAdapter extends ApolloGraphQLBaseAdapter {
});
}

private async runExecutorFactoryIfPresent(
apolloOptions: ApolloAdapterOptions,
) {
private async runExecutorFactoryIfPresent(apolloOptions: ApolloDriverConfig) {
if (!apolloOptions.executorFactory) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Injectable } from '@nestjs/common';
import { loadPackage } from '@nestjs/common/utils/load-package.util';
import { ApolloGatewayAdapterOptions } from '..';
import { ApolloGraphQLBaseAdapter } from './apollo-graphql-base.adapter';
import { ApolloGatewayDriverConfig } from '..';
import { ApolloBaseDriver } from './apollo-base.driver';

@Injectable()
export class ApolloGatewayGraphQLAdapter extends ApolloGraphQLBaseAdapter<ApolloGatewayAdapterOptions> {
public async start(options: ApolloGatewayAdapterOptions): Promise<void> {
export class ApolloGatewayDriver extends ApolloBaseDriver<ApolloGatewayDriverConfig> {
public async start(options: ApolloGatewayDriverConfig): Promise<void> {
const { ApolloGateway } = loadPackage(
'@apollo/gateway',
'ApolloGateway',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import {
} from '@nestjs/graphql-experimental';
import { extend } from '@nestjs/graphql-experimental/utils';
import { printSchema } from 'graphql';
import { ApolloAdapterOptions } from '../interfaces';
import { ApolloDriverConfig } from '../interfaces';
import { PluginsExplorerService } from '../services/plugins-explorer.service';
import { ApolloGraphQLBaseAdapter } from './apollo-graphql-base.adapter';
import { ApolloBaseDriver } from './apollo-base.driver';

@Injectable()
export class ApolloGraphQLAdapter extends ApolloGraphQLBaseAdapter {
export class ApolloDriver extends ApolloBaseDriver {
private _subscriptionService?: GqlSubscriptionService;
private readonly pluginsExplorerService: PluginsExplorerService;

Expand All @@ -20,7 +20,7 @@ export class ApolloGraphQLAdapter extends ApolloGraphQLBaseAdapter {
this.pluginsExplorerService = new PluginsExplorerService(modulesContainer);
}

public async start(apolloOptions: ApolloAdapterOptions) {
public async start(apolloOptions: ApolloDriverConfig) {
const options = await this.mergeDefaultOptions(apolloOptions);
const typeDefs =
(await this.graphQlTypesLoader.mergeTypesByPaths(options.typePaths)) ||
Expand All @@ -34,7 +34,7 @@ export class ApolloGraphQLAdapter extends ApolloGraphQLBaseAdapter {
);

const adapterOptions =
await this.graphQlFactory.mergeOptions<ApolloAdapterOptions>({
await this.graphQlFactory.mergeOptions<ApolloDriverConfig>({
...options,
typeDefs: mergedTypeDefs,
});
Expand Down Expand Up @@ -64,7 +64,7 @@ export class ApolloGraphQLAdapter extends ApolloGraphQLBaseAdapter {
}
}

public async registerServer(apolloOptions: ApolloAdapterOptions) {
public async registerServer(apolloOptions: ApolloDriverConfig) {
const httpAdapter = this.httpAdapterHost.httpAdapter;
const platformName = httpAdapter.getType();

Expand Down
3 changes: 3 additions & 0 deletions packages/apollo/lib/drivers/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './apollo-federation.driver';
export * from './apollo-gateway.driver';
export * from './apollo.driver';
12 changes: 6 additions & 6 deletions packages/apollo/lib/factories/graphql-federation.factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import {
} from 'graphql';
import { gql } from 'graphql-tag';
import { forEach, isEmpty } from 'lodash';
import { ApolloAdapterOptions } from '../interfaces';
import { ApolloDriverConfig } from '../interfaces';
import { PluginsExplorerService } from '../services/plugins-explorer.service';
import { transformSchema } from '../utils/transform-schema.util';

Expand All @@ -43,8 +43,8 @@ export class GraphQLFederationFactory {
) {}

async mergeOptions(
options: ApolloAdapterOptions = {},
): Promise<ApolloAdapterOptions> {
options: ApolloDriverConfig = {},
): Promise<ApolloDriverConfig> {
const transformSchema = async (schema: GraphQLSchema) =>
options.transformSchema ? options.transformSchema(schema) : schema;

Expand All @@ -71,7 +71,7 @@ export class GraphQLFederationFactory {
};
}

private buildSchemaFromTypeDefs(options: ApolloAdapterOptions) {
private buildSchemaFromTypeDefs(options: ApolloDriverConfig) {
const { buildSubgraphSchema }: typeof import('@apollo/subgraph') =
loadPackage('@apollo/subgraph', 'ApolloFederation', () =>
require('@apollo/subgraph'),
Expand All @@ -88,7 +88,7 @@ export class GraphQLFederationFactory {
}

private async generateSchema(
options: ApolloAdapterOptions,
options: ApolloDriverConfig,
): Promise<GraphQLSchema> {
const { buildSubgraphSchema, printSubgraphSchema } = loadPackage(
'@apollo/subgraph',
Expand Down Expand Up @@ -272,7 +272,7 @@ export class GraphQLFederationFactory {

async buildFederatedSchema(
autoSchemaFile: string | boolean,
options: ApolloAdapterOptions,
options: ApolloDriverConfig,
resolvers: Function[],
) {
const scalarsMap = this.scalarsExplorerService.getScalarsMap();
Expand Down
2 changes: 1 addition & 1 deletion packages/apollo/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export * from './adapters';
export * from './decorators';
export * from './drivers';
export * from './factories';
export * from './interfaces';
export * from './utils';
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export interface ServerRegistration {
disableHealthCheck?: boolean;
}

export interface ApolloAdapterOptions
export interface ApolloDriverConfig
extends Omit<Config, 'typeDefs'>,
ServerRegistration,
Omit<GqlModuleOptions, 'context'> {
Expand Down Expand Up @@ -77,7 +77,5 @@ export interface ApolloAdapterOptions
autoTransformHttpErrors?: boolean;
}

export type ApolloAdapterOptionsFactory =
GqlOptionsFactory<ApolloAdapterOptions>;
export type ApolloAdapterAsyncOptions =
GqlModuleAsyncOptions<ApolloAdapterOptions>;
export type ApolloDriverConfigFactory = GqlOptionsFactory<ApolloDriverConfig>;
export type ApolloDriverAsyncConfig = GqlModuleAsyncOptions<ApolloDriverConfig>;
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
GqlOptionsFactory,
} from '@nestjs/graphql-experimental';

export interface ApolloGatewayAdapterOptions {
export interface ApolloGatewayDriverConfig {
gateway?: GatewayConfig;
server?: Omit<
GqlModuleOptions,
Expand All @@ -23,11 +23,11 @@ export interface ApolloGatewayAdapterOptions {
| 'schemaDirectives'
| 'buildSchemaOptions'
| 'fieldResolverEnhancers'
| 'adapter'
| 'driver'
>;
}

export type GatewayOptionsFactory =
GqlOptionsFactory<ApolloGatewayAdapterOptions>;
export type GatewayAsyncOptions =
GqlModuleAsyncOptions<ApolloGatewayAdapterOptions>;
export type ApolloGatewayDriverConfigFactory =
GqlOptionsFactory<ApolloGatewayDriverConfig>;
export type ApolloGatewayDriverAsyncConfig =
GqlModuleAsyncOptions<ApolloGatewayDriverConfig>;
4 changes: 2 additions & 2 deletions packages/apollo/lib/interfaces/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from './adapter-options.interface';
export * from './gateway-adapter-options.interface';
export * from './apollo-driver-config.interface';
export * from './apollo-gateway-driver-config.interface';
8 changes: 4 additions & 4 deletions packages/apollo/tests/code-first-federation/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Module } from '@nestjs/common';
import { GraphQLModule } from '@nestjs/graphql-experimental';
import { ApolloServerPluginInlineTraceDisabled } from 'apollo-server-core';
import { ApolloAdapterOptions } from '../../lib';
import { ApolloFederationGraphQLAdapter } from '../../lib/adapters';
import { ApolloDriverConfig } from '../../lib';
import { ApolloFederationDriver } from '../../lib/drivers';
import { PostModule } from './post/post.module';
import { RecipeModule } from './recipe/recipe.module';
import { User } from './user/user.entity';
Expand All @@ -13,8 +13,8 @@ import { UserModule } from './user/user.module';
UserModule,
PostModule,
RecipeModule,
GraphQLModule.forRoot<ApolloAdapterOptions>({
adapter: ApolloFederationGraphQLAdapter,
GraphQLModule.forRoot<ApolloDriverConfig>({
driver: ApolloFederationDriver,
debug: false,
autoSchemaFile: true,
buildSchemaOptions: {
Expand Down
8 changes: 4 additions & 4 deletions packages/apollo/tests/code-first/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { Module } from '@nestjs/common';
import { GraphQLModule } from '@nestjs/graphql-experimental';
import { ApolloAdapterOptions } from '../../lib';
import { ApolloGraphQLAdapter } from '../../lib/adapters';
import { ApolloDriverConfig } from '../../lib';
import { ApolloDriver } from '../../lib/drivers';
import { DirectionsModule } from './directions/directions.module';
import { RecipesModule } from './recipes/recipes.module';

@Module({
imports: [
RecipesModule,
DirectionsModule,
GraphQLModule.forRoot<ApolloAdapterOptions>({
adapter: ApolloGraphQLAdapter,
GraphQLModule.forRoot<ApolloDriverConfig>({
driver: ApolloDriver,
debug: false,
installSubscriptionHandlers: true,
autoSchemaFile: true,
Expand Down
4 changes: 2 additions & 2 deletions packages/apollo/tests/e2e/graphql-request-scoped.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { GraphQLModule } from '@nestjs/graphql-experimental';
import { Test } from '@nestjs/testing';
import { join } from 'path';
import * as request from 'supertest';
import { ApolloGraphQLAdapter } from '../../lib/adapters';
import { ApolloDriver } from '../../lib/drivers';
import { CatsRequestScopedService } from '../graphql/cats/cats-request-scoped.service';
import { CatsModule } from '../graphql/cats/cats.module';

Expand All @@ -15,7 +15,7 @@ describe('GraphQL request scoped', () => {
imports: [
CatsModule.enableRequestScope(),
GraphQLModule.forRoot({
adapter: ApolloGraphQLAdapter,
driver: ApolloDriver,
typePaths: [join(__dirname, '..', 'graphql', '**', '*.graphql')],
}),
],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Injectable } from '@nestjs/common';
import {
ApolloGatewayAdapterOptions,
GatewayOptionsFactory,
ApolloGatewayDriverConfig,
ApolloGatewayDriverConfigFactory,
} from '../../../../lib';

@Injectable()
export class ConfigService implements GatewayOptionsFactory {
public createGqlOptions(): Partial<ApolloGatewayAdapterOptions> {
export class ConfigService implements ApolloGatewayDriverConfigFactory {
public createGqlOptions(): Partial<ApolloGatewayDriverConfig> {
return {
gateway: {
serviceList: [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { Module } from '@nestjs/common';
import { GraphQLModule } from '@nestjs/graphql-experimental';
import { ApolloGatewayAdapterOptions } from '../../../lib';
import { ApolloGatewayGraphQLAdapter } from '../../../lib/adapters';
import { ApolloGatewayDriverConfig } from '../../../lib';
import { ApolloGatewayDriver } from '../../../lib/drivers';
import { ConfigModule } from './config/config.module';
import { ConfigService } from './config/config.service';

@Module({
imports: [
GraphQLModule.forRootAsync<ApolloGatewayAdapterOptions>({
adapter: ApolloGatewayGraphQLAdapter,
GraphQLModule.forRootAsync<ApolloGatewayDriverConfig>({
driver: ApolloGatewayDriver,
useClass: ConfigService,
imports: [ConfigModule],
inject: [ConfigService],
Expand Down
Loading

0 comments on commit 2012ade

Please sign in to comment.