From 191ff3ac6e620258bf2a7e635e717c27fdba8f06 Mon Sep 17 00:00:00 2001 From: Jackson Kearl Date: Mon, 24 Jun 2019 13:24:17 -0700 Subject: [PATCH 1/8] Add createGateway helper function --- packages/apollo-gateway/src/index.ts | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/packages/apollo-gateway/src/index.ts b/packages/apollo-gateway/src/index.ts index 0e43515ed46..03e36fbdb63 100644 --- a/packages/apollo-gateway/src/index.ts +++ b/packages/apollo-gateway/src/index.ts @@ -134,8 +134,8 @@ export class ApolloGateway implements GraphQLService { this.serviceMap[serviceDef.name] = this.config.buildService ? this.config.buildService(serviceDef) : new RemoteGraphQLDataSource({ - url: serviceDef.url, - }); + url: serviceDef.url, + }); } } @@ -260,6 +260,14 @@ function wrapSchemaWithAliasResolver(schema: GraphQLSchema): GraphQLSchema { return schema; } +export async function createGateway( + config: GatewayConfig, +): Promise { + const gateway = new ApolloGateway(config); + await gateway.load(); + return gateway; +} + export { buildQueryPlan, executeQueryPlan, From 6df6f001a582c2f9ab2b58cd1ad8da2e5859a7e1 Mon Sep 17 00:00:00 2001 From: Jackson Kearl Date: Mon, 24 Jun 2019 13:40:15 -0700 Subject: [PATCH 2/8] Lint --- packages/apollo-gateway/src/index.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/apollo-gateway/src/index.ts b/packages/apollo-gateway/src/index.ts index 03e36fbdb63..46fd7bfed7b 100644 --- a/packages/apollo-gateway/src/index.ts +++ b/packages/apollo-gateway/src/index.ts @@ -134,8 +134,8 @@ export class ApolloGateway implements GraphQLService { this.serviceMap[serviceDef.name] = this.config.buildService ? this.config.buildService(serviceDef) : new RemoteGraphQLDataSource({ - url: serviceDef.url, - }); + url: serviceDef.url, + }); } } From 3c9bdc5344ed21f7cf8f0ebe18166e00c6d3c029 Mon Sep 17 00:00:00 2001 From: Jackson Kearl Date: Tue, 25 Jun 2019 16:09:03 -0700 Subject: [PATCH 3/8] Move to {gateway: createGateway} API --- packages/apollo-gateway/src/index.ts | 15 ++++----------- packages/apollo-server-core/src/ApolloServer.ts | 4 ++++ packages/apollo-server-core/src/types.ts | 7 +++++++ 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/packages/apollo-gateway/src/index.ts b/packages/apollo-gateway/src/index.ts index 46fd7bfed7b..a87b3f28257 100644 --- a/packages/apollo-gateway/src/index.ts +++ b/packages/apollo-gateway/src/index.ts @@ -1,7 +1,7 @@ import { - GraphQLExecutor, GraphQLExecutionResult, GraphQLRequestContext, + GraphQLService, } from 'apollo-server-core'; import { InMemoryLRUCache } from 'apollo-server-caching'; import { isObjectType, isIntrospectionType, GraphQLSchema } from 'graphql'; @@ -24,12 +24,6 @@ import { serializeQueryPlan, QueryPlan } from './QueryPlan'; import { GraphQLDataSource } from './datasources/types'; import { RemoteGraphQLDataSource } from './datasources/RemoteGraphQLDatasource'; -export interface GraphQLService { - schema?: GraphQLSchema; - executor: GraphQLExecutor; - isReady: boolean; -} - export type ServiceEndpointDefinition = Pick; export interface GatewayConfigBase { @@ -52,7 +46,7 @@ function isLocalConfig(config: GatewayConfig): config is LocalGatewayConfig { return 'localServiceList' in config; } -export class ApolloGateway implements GraphQLService { +export class ApolloGateway { public schema?: GraphQLSchema; public isReady: boolean = false; protected serviceMap: ServiceMap = Object.create(null); @@ -95,7 +89,7 @@ export class ApolloGateway implements GraphQLService { this.createSchema(services); } - return { schema: this.schema, executor: this.executor }; + return { schema: this.schema!, executor: this.executor }; } protected createSchema(services: ServiceDefinition[]) { @@ -264,8 +258,7 @@ export async function createGateway( config: GatewayConfig, ): Promise { const gateway = new ApolloGateway(config); - await gateway.load(); - return gateway; + return await gateway.load(); } export { diff --git a/packages/apollo-server-core/src/ApolloServer.ts b/packages/apollo-server-core/src/ApolloServer.ts index e1da33a2539..1bfdfddf7cf 100644 --- a/packages/apollo-server-core/src/ApolloServer.ts +++ b/packages/apollo-server-core/src/ApolloServer.ts @@ -153,6 +153,7 @@ export class ApolloServerBase { uploads, playground, plugins, + gateway, ...requestOptions } = config; @@ -261,6 +262,9 @@ export class ApolloServerBase { throw new Error(errors.map(error => error.message).join('\n\n')); } this.schema = schema!; + } else if (gateway) { + this.schema = gateway.schema; + this.requestOptions.executor = gateway.executor; } else { if (!typeDefs) { throw Error( diff --git a/packages/apollo-server-core/src/types.ts b/packages/apollo-server-core/src/types.ts index d0c391437da..67081f5c3d7 100644 --- a/packages/apollo-server-core/src/types.ts +++ b/packages/apollo-server-core/src/types.ts @@ -26,6 +26,7 @@ import { CacheControlExtensionOptions } from 'apollo-cache-control'; import { ApolloServerPlugin } from 'apollo-server-plugin-base'; import { GraphQLSchemaModule } from '@apollographql/apollo-tools'; +import { GraphQLExecutor } from 'apollo-server-core/dist/requestPipelineAPI'; export { GraphQLSchemaModule }; export { KeyValueCache } from 'apollo-server-caching'; @@ -64,6 +65,11 @@ type BaseConfig = Pick< | 'cache' >; +export interface GraphQLService { + schema: GraphQLSchema; + executor: GraphQLExecutor; +} + // This configuration is shared between all integrations and should include // fields that are not specific to a single integration export interface Config extends BaseConfig { @@ -86,6 +92,7 @@ export interface Config extends BaseConfig { //https://github.com/jaydenseric/graphql-upload#type-uploadoptions uploads?: boolean | FileUploadOptions; playground?: PlaygroundConfig; + gateway?: GraphQLService; } export interface FileUploadOptions { From 90da87e09b87afdb9bb676ac64b444011ff79d40 Mon Sep 17 00:00:00 2001 From: Jackson Kearl Date: Tue, 25 Jun 2019 16:57:06 -0700 Subject: [PATCH 4/8] Declare return type of ApolloGateway#load() --- packages/apollo-gateway/src/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/apollo-gateway/src/index.ts b/packages/apollo-gateway/src/index.ts index a87b3f28257..c74b0084cd7 100644 --- a/packages/apollo-gateway/src/index.ts +++ b/packages/apollo-gateway/src/index.ts @@ -81,7 +81,7 @@ export class ApolloGateway { this.initializeQueryPlanStore(); } - public async load() { + public async load(): Promise { if (!this.isReady) { this.logger.debug('Loading configuration for Gateway'); const [services] = await this.loadServiceDefinitions(this.config); From 51191e26845b9ab0492aa966e9e3b799998c073c Mon Sep 17 00:00:00 2001 From: Jackson Kearl Date: Wed, 26 Jun 2019 14:57:12 -0700 Subject: [PATCH 5/8] Add apiKey threading logic --- packages/apollo-gateway/src/index.ts | 5 ++--- packages/apollo-server-core/src/ApolloServer.ts | 11 +++++++++++ packages/apollo-server-core/src/types.ts | 3 ++- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/packages/apollo-gateway/src/index.ts b/packages/apollo-gateway/src/index.ts index c74b0084cd7..01fe7f2794c 100644 --- a/packages/apollo-gateway/src/index.ts +++ b/packages/apollo-gateway/src/index.ts @@ -89,7 +89,7 @@ export class ApolloGateway { this.createSchema(services); } - return { schema: this.schema!, executor: this.executor }; + return { schema: this.schema!, executor: this.executor }; // TODO: pass apiKey. (dependant on #2915) } protected createSchema(services: ServiceDefinition[]) { @@ -257,8 +257,7 @@ function wrapSchemaWithAliasResolver(schema: GraphQLSchema): GraphQLSchema { export async function createGateway( config: GatewayConfig, ): Promise { - const gateway = new ApolloGateway(config); - return await gateway.load(); + return await new ApolloGateway(config).load(); } export { diff --git a/packages/apollo-server-core/src/ApolloServer.ts b/packages/apollo-server-core/src/ApolloServer.ts index 1bfdfddf7cf..eecfa84cf98 100644 --- a/packages/apollo-server-core/src/ApolloServer.ts +++ b/packages/apollo-server-core/src/ApolloServer.ts @@ -61,6 +61,7 @@ import { import { Headers } from 'apollo-server-env'; import { buildServiceDefinition } from '@apollographql/apollo-tools'; +import { EngineReportingOptions } from 'apollo-engine-reporting'; const NoIntrospection = (context: ValidationContext) => ({ Field(node: FieldDefinitionNode) { @@ -265,6 +266,16 @@ export class ApolloServerBase { } else if (gateway) { this.schema = gateway.schema; this.requestOptions.executor = gateway.executor; + if (gateway.apiKey) { + if (engine === undefined) { + ((engine as unknown) as EngineReportingOptions) = { + apiKey: gateway.apiKey, + }; + } + if (engine) { + engine.apiKey = engine.apiKey || gateway.apiKey; + } + } } else { if (!typeDefs) { throw Error( diff --git a/packages/apollo-server-core/src/types.ts b/packages/apollo-server-core/src/types.ts index 67081f5c3d7..0b79eb6642c 100644 --- a/packages/apollo-server-core/src/types.ts +++ b/packages/apollo-server-core/src/types.ts @@ -68,6 +68,7 @@ type BaseConfig = Pick< export interface GraphQLService { schema: GraphQLSchema; executor: GraphQLExecutor; + apiKey?: string; } // This configuration is shared between all integrations and should include @@ -83,7 +84,7 @@ export interface Config extends BaseConfig { introspection?: boolean; mocks?: boolean | IMocks; mockEntireSchema?: boolean; - engine?: boolean | EngineReportingOptions; + engine?: false | EngineReportingOptions; extensions?: Array<() => GraphQLExtension>; cacheControl?: CacheControlExtensionOptions | boolean; plugins?: PluginDefinition[]; From f4ae5b54f5d57118f60c26f683a62650e52b7262 Mon Sep 17 00:00:00 2001 From: Jackson Kearl Date: Thu, 27 Jun 2019 15:02:11 -0700 Subject: [PATCH 6/8] Remove some TODO's, change typings --- packages/apollo-gateway/src/index.ts | 2 +- packages/apollo-server-core/src/types.ts | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/packages/apollo-gateway/src/index.ts b/packages/apollo-gateway/src/index.ts index 01fe7f2794c..e94b7acee8b 100644 --- a/packages/apollo-gateway/src/index.ts +++ b/packages/apollo-gateway/src/index.ts @@ -89,7 +89,7 @@ export class ApolloGateway { this.createSchema(services); } - return { schema: this.schema!, executor: this.executor }; // TODO: pass apiKey. (dependant on #2915) + return { schema: this.schema!, executor: this.executor }; } protected createSchema(services: ServiceDefinition[]) { diff --git a/packages/apollo-server-core/src/types.ts b/packages/apollo-server-core/src/types.ts index 0b79eb6642c..67081f5c3d7 100644 --- a/packages/apollo-server-core/src/types.ts +++ b/packages/apollo-server-core/src/types.ts @@ -68,7 +68,6 @@ type BaseConfig = Pick< export interface GraphQLService { schema: GraphQLSchema; executor: GraphQLExecutor; - apiKey?: string; } // This configuration is shared between all integrations and should include @@ -84,7 +83,7 @@ export interface Config extends BaseConfig { introspection?: boolean; mocks?: boolean | IMocks; mockEntireSchema?: boolean; - engine?: false | EngineReportingOptions; + engine?: boolean | EngineReportingOptions; extensions?: Array<() => GraphQLExtension>; cacheControl?: CacheControlExtensionOptions | boolean; plugins?: PluginDefinition[]; From 931330a684937982383f87abff495803fd16ccb1 Mon Sep 17 00:00:00 2001 From: Jackson Kearl Date: Thu, 27 Jun 2019 15:02:58 -0700 Subject: [PATCH 7/8] Remove createGateway helper function --- packages/apollo-gateway/src/index.ts | 6 ------ 1 file changed, 6 deletions(-) diff --git a/packages/apollo-gateway/src/index.ts b/packages/apollo-gateway/src/index.ts index e94b7acee8b..ad7832a69ba 100644 --- a/packages/apollo-gateway/src/index.ts +++ b/packages/apollo-gateway/src/index.ts @@ -254,12 +254,6 @@ function wrapSchemaWithAliasResolver(schema: GraphQLSchema): GraphQLSchema { return schema; } -export async function createGateway( - config: GatewayConfig, -): Promise { - return await new ApolloGateway(config).load(); -} - export { buildQueryPlan, executeQueryPlan, From 45f4e68e342bc6c7052722a0befbb68d3a44fe15 Mon Sep 17 00:00:00 2001 From: Jackson Kearl Date: Thu, 27 Jun 2019 15:06:27 -0700 Subject: [PATCH 8/8] Pass gatgeway directly --- packages/apollo-gateway/src/index.ts | 4 ++-- packages/apollo-server-core/src/types.ts | 6 ++++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/packages/apollo-gateway/src/index.ts b/packages/apollo-gateway/src/index.ts index ad7832a69ba..0e91adce5d4 100644 --- a/packages/apollo-gateway/src/index.ts +++ b/packages/apollo-gateway/src/index.ts @@ -46,7 +46,7 @@ function isLocalConfig(config: GatewayConfig): config is LocalGatewayConfig { return 'localServiceList' in config; } -export class ApolloGateway { +export class ApolloGateway implements GraphQLService { public schema?: GraphQLSchema; public isReady: boolean = false; protected serviceMap: ServiceMap = Object.create(null); @@ -81,7 +81,7 @@ export class ApolloGateway { this.initializeQueryPlanStore(); } - public async load(): Promise { + public async load() { if (!this.isReady) { this.logger.debug('Loading configuration for Gateway'); const [services] = await this.loadServiceDefinitions(this.config); diff --git a/packages/apollo-server-core/src/types.ts b/packages/apollo-server-core/src/types.ts index 67081f5c3d7..bfa51bbe2ea 100644 --- a/packages/apollo-server-core/src/types.ts +++ b/packages/apollo-server-core/src/types.ts @@ -66,8 +66,10 @@ type BaseConfig = Pick< >; export interface GraphQLService { - schema: GraphQLSchema; - executor: GraphQLExecutor; + load(): Promise<{ + schema: GraphQLSchema; + executor: GraphQLExecutor; + }>; } // This configuration is shared between all integrations and should include