Skip to content

Commit 14f97bc

Browse files
committed
refactor: use fed2 implementation due to changes
1 parent d9f7446 commit 14f97bc

File tree

2 files changed

+21
-14
lines changed

2 files changed

+21
-14
lines changed

packages/graphql/lib/federation/graphql-federation.factory.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,13 @@ import { gql } from 'graphql-tag';
2626
import { forEach, isEmpty } from 'lodash';
2727
import { GraphQLSchemaBuilder } from '../graphql-schema.builder';
2828
import { GraphQLSchemaHost } from '../graphql-schema.host';
29-
import { GqlModuleOptions, BuildFederatedSchemaOptions } from '../interfaces';
29+
import {
30+
GqlModuleOptions,
31+
BuildFederatedSchemaOptions,
32+
AutoSchemaFileValue,
33+
} from '../interfaces';
3034
import { ResolversExplorerService, ScalarsExplorerService } from '../services';
31-
import { extend, stringifyWithoutQuotes } from '../utils';
35+
import { extend, getFederation2Info, stringifyWithoutQuotes } from '../utils';
3236
import { transformSchema } from '../utils/transform-schema.util';
3337

3438
@Injectable()
@@ -113,7 +117,10 @@ export class GraphQLFederationFactory {
113117
let typeDefs = isApolloSubgraph2
114118
? printSchemaWithDirectives(autoGeneratedSchema)
115119
: printSubgraphSchema(autoGeneratedSchema);
116-
if (options.useFed2 && isApolloSubgraph2) {
120+
121+
const useFed2 = getFederation2Info(options.autoSchemaFile);
122+
123+
if (useFed2 && isApolloSubgraph2) {
117124
const {
118125
directives = [
119126
'@key',
@@ -123,7 +130,7 @@ export class GraphQLFederationFactory {
123130
'@requires',
124131
],
125132
importUrl = 'https://specs.apollo.dev/federation/v2.0',
126-
} = typeof options.useFed2 === 'boolean' ? {} : options.useFed2;
133+
} = typeof useFed2 === 'boolean' ? {} : useFed2;
127134
const mappedDirectives = directives
128135
.map((directive) => {
129136
if (!isString(directive)) {
@@ -141,7 +148,7 @@ export class GraphQLFederationFactory {
141148
extend schema @link(url: "${importUrl}", import: [${mappedDirectives}])
142149
${typeDefs}
143150
`;
144-
} else if (options.useFed2 && !isApolloSubgraph2) {
151+
} else if (useFed2 && !isApolloSubgraph2) {
145152
Logger.error(
146153
'You are trying to use Apollo Federation 2 but you are not using @apollo/subgraph@^2.0.0, please upgrade',
147154
'GraphQLFederationFactory',
@@ -327,21 +334,22 @@ export class GraphQLFederationFactory {
327334
}
328335

329336
async buildFederatedSchema<T extends GqlModuleOptions>(
330-
autoSchemaFile: string | boolean,
337+
autoSchemaFile: AutoSchemaFileValue,
331338
options: T,
332339
resolvers: Function[],
333340
) {
334341
const scalarsMap = this.scalarsExplorerService.getScalarsMap();
335342
try {
336343
const buildSchemaOptions = options.buildSchemaOptions || {};
344+
const useFed2 = getFederation2Info(options.autoSchemaFile);
337345
return await this.gqlSchemaBuilder.generateSchema(
338346
resolvers,
339347
autoSchemaFile,
340348
{
341349
...buildSchemaOptions,
342350
directives: [
343351
...specifiedDirectives,
344-
...(options.useFed2 ? [] : this.loadFederationDirectives()),
352+
...(useFed2 ? [] : this.loadFederationDirectives()),
345353
...((buildSchemaOptions && buildSchemaOptions.directives) || []),
346354
],
347355
scalarsMap,

packages/graphql/lib/graphql-schema.builder.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ import { isString } from '@nestjs/common/utils/shared.utils';
33
import { GraphQLSchema, lexicographicSortSchema, printSchema } from 'graphql';
44
import { resolve } from 'path';
55
import { GRAPHQL_SDL_FILE_HEADER } from './graphql.constants';
6-
import { GqlModuleOptions } from './interfaces';
6+
import { AutoSchemaFileValue, GqlModuleOptions } from './interfaces';
77
import { BuildSchemaOptions } from './interfaces/build-schema-options.interface';
88
import { GraphQLSchemaFactory } from './schema-builder/graphql-schema.factory';
99
import { FileSystemHelper } from './schema-builder/helpers/file-system.helper';
1010
import { ScalarsExplorerService } from './services';
11+
import { getPathForAutoSchemaFile } from './utils';
1112

1213
@Injectable()
1314
export class GraphQLSchemaBuilder {
@@ -18,7 +19,7 @@ export class GraphQLSchemaBuilder {
1819
) {}
1920

2021
public async build(
21-
autoSchemaFile: string | boolean,
22+
autoSchemaFile: AutoSchemaFileValue,
2223
options: GqlModuleOptions,
2324
resolvers: Function[],
2425
): Promise<any> {
@@ -45,19 +46,17 @@ export class GraphQLSchemaBuilder {
4546

4647
public async generateSchema(
4748
resolvers: Function[],
48-
autoSchemaFile: boolean | string,
49+
autoSchemaFile: AutoSchemaFileValue,
4950
options: BuildSchemaOptions = {},
5051
sortSchema?: boolean,
5152
transformSchema?: (
5253
schema: GraphQLSchema,
5354
) => GraphQLSchema | Promise<GraphQLSchema>,
5455
): Promise<GraphQLSchema> {
5556
const schema = await this.gqlSchemaFactory.create(resolvers, options);
56-
if (typeof autoSchemaFile !== 'boolean') {
57-
const filename = isString(autoSchemaFile)
58-
? autoSchemaFile
59-
: resolve(process.cwd(), 'schema.gql');
57+
const filename = getPathForAutoSchemaFile(autoSchemaFile);
6058

59+
if (filename) {
6160
const transformedSchema = transformSchema
6261
? await transformSchema(schema)
6362
: schema;

0 commit comments

Comments
 (0)