8
8
GraphQLExecutionResult ,
9
9
Logger ,
10
10
GraphQLRequestContextExecutionDidStart ,
11
- ApolloConfig ,
12
11
} from 'apollo-server-types' ;
13
12
import { InMemoryLRUCache } from 'apollo-server-caching' ;
14
13
import {
@@ -126,7 +125,7 @@ export const SERVICE_DEFINITION_QUERY =
126
125
127
126
type GatewayState =
128
127
| { phase : 'initialized' }
129
- | { phase : 'failed to load' }
128
+ | { phase : 'failed to load' }
130
129
| { phase : 'loaded' }
131
130
| { phase : 'stopping' ; stoppingDonePromise : Promise < void > }
132
131
| { phase : 'stopped' }
@@ -136,13 +135,28 @@ type GatewayState =
136
135
doneWaiting : ( ) => void ;
137
136
}
138
137
| { phase : 'polling' ; pollingDonePromise : Promise < void > } ;
138
+
139
+ interface ApolloConfigFromAS3 {
140
+ key ?: string ;
141
+ keyHash ?: string ;
142
+ graphRef ?: string ;
143
+ }
144
+
145
+ interface ApolloConfigFromAS2Or3 {
146
+ key ?: string ;
147
+ keyHash ?: string ;
148
+ graphRef ?: string ;
149
+ graphId ?: string ;
150
+ graphVariant ?: string ;
151
+ }
152
+
139
153
export class ApolloGateway implements GraphQLService {
140
154
public schema ?: GraphQLSchema ;
141
155
private serviceMap : DataSourceMap = Object . create ( null ) ;
142
156
private config : GatewayConfig ;
143
157
private logger : Logger ;
144
158
private queryPlanStore : InMemoryLRUCache < QueryPlan > ;
145
- private apolloConfig ?: ApolloConfig ;
159
+ private apolloConfig ?: ApolloConfigFromAS3 ;
146
160
private onSchemaChangeListeners = new Set < SchemaChangeCallback > ( ) ;
147
161
private serviceDefinitions : ServiceDefinition [ ] = [ ] ;
148
162
private compositionMetadata ?: CompositionMetadata ;
@@ -306,7 +320,7 @@ export class ApolloGateway implements GraphQLService {
306
320
}
307
321
308
322
public async load ( options ?: {
309
- apollo ?: ApolloConfig ;
323
+ apollo ?: ApolloConfigFromAS2Or3 ;
310
324
engine ?: GraphQLServiceEngineConfig ;
311
325
} ) {
312
326
if ( this . state . phase !== 'initialized' ) {
@@ -315,13 +329,22 @@ export class ApolloGateway implements GraphQLService {
315
329
) ;
316
330
}
317
331
if ( options ?. apollo ) {
318
- this . apolloConfig = options . apollo ;
332
+ const { key, keyHash, graphRef, graphId, graphVariant } = options . apollo ;
333
+ this . apolloConfig = {
334
+ key,
335
+ keyHash,
336
+ graphRef :
337
+ graphRef ??
338
+ ( graphId ? `${ graphId } @${ graphVariant ?? 'current' } ` : undefined ) ,
339
+ } ;
319
340
} else if ( options ?. engine ) {
320
341
// Older version of apollo-server-core that isn't passing 'apollo' yet.
342
+ const { apiKeyHash, graphId, graphVariant } = options . engine ;
321
343
this . apolloConfig = {
322
- keyHash : options . engine . apiKeyHash ,
323
- graphId : options . engine . graphId ,
324
- graphVariant : options . engine . graphVariant || 'current' ,
344
+ keyHash : apiKeyHash ,
345
+ graphRef : graphId
346
+ ? `${ graphId } @${ graphVariant ?? 'current' } `
347
+ : undefined ,
325
348
} ;
326
349
}
327
350
@@ -363,8 +386,8 @@ export class ApolloGateway implements GraphQLService {
363
386
const mode = isManagedConfig ( this . config ) ? 'managed' : 'unmanaged' ;
364
387
this . logger . info (
365
388
`Gateway successfully loaded schema.\n\t* Mode: ${ mode } ${
366
- this . apolloConfig && this . apolloConfig . graphId
367
- ? `\n\t* Service: ${ this . apolloConfig . graphId } @ ${ this . apolloConfig . graphVariant } `
389
+ this . apolloConfig && this . apolloConfig . graphRef
390
+ ? `\n\t* Service: ${ this . apolloConfig . graphRef } `
368
391
: ''
369
392
} `,
370
393
) ;
@@ -861,7 +884,7 @@ export class ApolloGateway implements GraphQLService {
861
884
}
862
885
863
886
const canUseManagedConfig =
864
- this . apolloConfig ?. graphId && this . apolloConfig ?. keyHash ;
887
+ this . apolloConfig ?. graphRef && this . apolloConfig ?. keyHash ;
865
888
if ( ! canUseManagedConfig ) {
866
889
throw new Error (
867
890
'When a manual configuration is not provided, gateway requires an Apollo ' +
@@ -877,26 +900,24 @@ export class ApolloGateway implements GraphQLService {
877
900
! isPrecomposedManagedConfig ( config )
878
901
) {
879
902
return getServiceDefinitionsFromStorage ( {
880
- graphId : this . apolloConfig ! . graphId ! ,
903
+ graphRef : this . apolloConfig ! . graphRef ! ,
881
904
apiKeyHash : this . apolloConfig ! . keyHash ! ,
882
- graphVariant : this . apolloConfig ! . graphVariant ,
883
905
federationVersion : config . federationVersion || 1 ,
884
906
fetcher : this . fetcher ,
885
907
} ) ;
886
908
}
887
909
888
910
return loadSupergraphSdlFromStorage ( {
889
- graphId : this . apolloConfig ! . graphId ! ,
911
+ graphRef : this . apolloConfig ! . graphRef ! ,
890
912
apiKey : this . apolloConfig ! . key ! ,
891
- graphVariant : this . apolloConfig ! . graphVariant ,
892
913
endpoint : this . experimental_schemaConfigDeliveryEndpoint ! ,
893
914
fetcher : this . fetcher ,
894
915
} ) ;
895
916
}
896
917
897
918
private maybeWarnOnConflictingConfig ( ) {
898
919
const canUseManagedConfig =
899
- this . apolloConfig ?. graphId && this . apolloConfig ?. keyHash ;
920
+ this . apolloConfig ?. graphRef && this . apolloConfig ?. keyHash ;
900
921
901
922
// This might be a bit confusing just by reading, but `!isManagedConfig` just
902
923
// means it's any of the other types of config. If it's any other config _and_
0 commit comments