@@ -26,9 +26,13 @@ import { gql } from 'graphql-tag';
26
26
import { forEach , isEmpty } from 'lodash' ;
27
27
import { GraphQLSchemaBuilder } from '../graphql-schema.builder' ;
28
28
import { GraphQLSchemaHost } from '../graphql-schema.host' ;
29
- import { GqlModuleOptions , BuildFederatedSchemaOptions } from '../interfaces' ;
29
+ import {
30
+ GqlModuleOptions ,
31
+ BuildFederatedSchemaOptions ,
32
+ AutoSchemaFileValue ,
33
+ } from '../interfaces' ;
30
34
import { ResolversExplorerService , ScalarsExplorerService } from '../services' ;
31
- import { extend , stringifyWithoutQuotes } from '../utils' ;
35
+ import { extend , getFederation2Info , stringifyWithoutQuotes } from '../utils' ;
32
36
import { transformSchema } from '../utils/transform-schema.util' ;
33
37
34
38
@Injectable ( )
@@ -113,7 +117,10 @@ export class GraphQLFederationFactory {
113
117
let typeDefs = isApolloSubgraph2
114
118
? printSchemaWithDirectives ( autoGeneratedSchema )
115
119
: printSubgraphSchema ( autoGeneratedSchema ) ;
116
- if ( options . useFed2 && isApolloSubgraph2 ) {
120
+
121
+ const useFed2 = getFederation2Info ( options . autoSchemaFile ) ;
122
+
123
+ if ( useFed2 && isApolloSubgraph2 ) {
117
124
const {
118
125
directives = [
119
126
'@key' ,
@@ -123,7 +130,7 @@ export class GraphQLFederationFactory {
123
130
'@requires' ,
124
131
] ,
125
132
importUrl = 'https://specs.apollo.dev/federation/v2.0' ,
126
- } = typeof options . useFed2 === 'boolean' ? { } : options . useFed2 ;
133
+ } = typeof useFed2 === 'boolean' ? { } : useFed2 ;
127
134
const mappedDirectives = directives
128
135
. map ( ( directive ) => {
129
136
if ( ! isString ( directive ) ) {
@@ -141,7 +148,7 @@ export class GraphQLFederationFactory {
141
148
extend schema @link(url: "${ importUrl } ", import: [${ mappedDirectives } ])
142
149
${ typeDefs }
143
150
` ;
144
- } else if ( options . useFed2 && ! isApolloSubgraph2 ) {
151
+ } else if ( useFed2 && ! isApolloSubgraph2 ) {
145
152
Logger . error (
146
153
'You are trying to use Apollo Federation 2 but you are not using @apollo/subgraph@^2.0.0, please upgrade' ,
147
154
'GraphQLFederationFactory' ,
@@ -327,21 +334,22 @@ export class GraphQLFederationFactory {
327
334
}
328
335
329
336
async buildFederatedSchema < T extends GqlModuleOptions > (
330
- autoSchemaFile : string | boolean ,
337
+ autoSchemaFile : AutoSchemaFileValue ,
331
338
options : T ,
332
339
resolvers : Function [ ] ,
333
340
) {
334
341
const scalarsMap = this . scalarsExplorerService . getScalarsMap ( ) ;
335
342
try {
336
343
const buildSchemaOptions = options . buildSchemaOptions || { } ;
344
+ const useFed2 = getFederation2Info ( options . autoSchemaFile ) ;
337
345
return await this . gqlSchemaBuilder . generateSchema (
338
346
resolvers ,
339
347
autoSchemaFile ,
340
348
{
341
349
...buildSchemaOptions ,
342
350
directives : [
343
351
...specifiedDirectives ,
344
- ...( options . useFed2 ? [ ] : this . loadFederationDirectives ( ) ) ,
352
+ ...( useFed2 ? [ ] : this . loadFederationDirectives ( ) ) ,
345
353
...( ( buildSchemaOptions && buildSchemaOptions . directives ) || [ ] ) ,
346
354
] ,
347
355
scalarsMap,
0 commit comments