diff --git a/packages/graphql/lib/federation/graphql-federation.factory.ts b/packages/graphql/lib/federation/graphql-federation.factory.ts index 012084713..da3e9dbdb 100644 --- a/packages/graphql/lib/federation/graphql-federation.factory.ts +++ b/packages/graphql/lib/federation/graphql-federation.factory.ts @@ -280,6 +280,12 @@ export class GraphQLFederationFactory { options: T, resolvers: Function[], ) { + const { printSubgraphSchema } = loadPackage( + '@apollo/subgraph', + 'ApolloFederation', + () => require('@apollo/subgraph'), + ); + const scalarsMap = this.scalarsExplorerService.getScalarsMap(); try { const buildSchemaOptions = options.buildSchemaOptions || {}; @@ -298,6 +304,7 @@ export class GraphQLFederationFactory { }, options.sortSchema, options.transformAutoSchemaFile && options.transformSchema, + printSubgraphSchema, ); } catch (err) { if (err && err.details) { diff --git a/packages/graphql/lib/graphql-schema.builder.ts b/packages/graphql/lib/graphql-schema.builder.ts index f1ba0d70f..1a89aed9c 100644 --- a/packages/graphql/lib/graphql-schema.builder.ts +++ b/packages/graphql/lib/graphql-schema.builder.ts @@ -1,6 +1,10 @@ import { Injectable } from '@nestjs/common'; import { isString } from '@nestjs/common/utils/shared.utils'; -import { GraphQLSchema, lexicographicSortSchema, printSchema } from 'graphql'; +import { + GraphQLSchema, + lexicographicSortSchema, + printSchema as gqlPrintSchema, +} from 'graphql'; import { resolve } from 'path'; import { GRAPHQL_SDL_FILE_HEADER } from './graphql.constants'; import { GqlModuleOptions } from './interfaces'; @@ -51,6 +55,7 @@ export class GraphQLSchemaBuilder { transformSchema?: ( schema: GraphQLSchema, ) => GraphQLSchema | Promise, + printSchema?: (schema: GraphQLSchema) => string, ): Promise { const schema = await this.gqlSchemaFactory.create(resolvers, options); if (typeof autoSchemaFile !== 'boolean') { @@ -61,13 +66,14 @@ export class GraphQLSchemaBuilder { const transformedSchema = transformSchema ? await transformSchema(schema) : schema; - const fileContent = - GRAPHQL_SDL_FILE_HEADER + - printSchema( - sortSchema - ? lexicographicSortSchema(transformedSchema) - : transformedSchema, - ); + + const base = sortSchema + ? lexicographicSortSchema(transformedSchema) + : transformedSchema; + + const gql = printSchema ? printSchema(base) : gqlPrintSchema(base); + + const fileContent = GRAPHQL_SDL_FILE_HEADER + gql; await this.fileSystemHelper.writeFile(filename, fileContent); } return schema;