@nestjs/graphql
Version:
Nest - modern, fast, powerful node.js web framework (@graphql)
38 lines (37 loc) • 1.75 kB
JavaScript
import { mergeTypeDefs } from '@graphql-tools/merge';
import { loadPackage } from '@nestjs/common/utils/load-package.util.js';
import { gql } from 'graphql-tag';
import { GraphQLDefinitionsFactory } from '../graphql-definitions.factory.js';
import { extend } from '../utils/index.js';
/**
* @publicApi
*/
export class GraphQLFederationDefinitionsFactory extends GraphQLDefinitionsFactory {
async exploreAndEmit(typePaths, path, outputAs, isDebugEnabled, definitionsGeneratorOptions, typeDefs) {
const typePathDefs = await this.gqlTypesLoader.mergeTypesByPaths(typePaths);
const mergedTypeDefs = extend(typePathDefs, typeDefs);
const { buildSubgraphSchema, printSubgraphSchema, } = await loadPackage('@apollo/subgraph', 'ApolloFederation', () => import('@apollo/subgraph'));
const schema = buildSubgraphSchema([
{
typeDefs: gql `
${mergedTypeDefs}
`,
resolvers: {},
},
]);
// "buildSubgraphSchema" generates an empty Query definition if there is the `extend type Query` statement
// This leads to duplicated IQuery interfaces
// see: https://github.com//issues/2344
const mergedDefinition = mergeTypeDefs([printSubgraphSchema(schema)], {
useSchemaDefinition: false,
throwOnConflict: true,
commentDescriptions: true,
reverseDirectives: true,
});
const tsFile = await this.gqlAstExplorer.explore(gql `
${mergedDefinition}
`, path, outputAs, definitionsGeneratorOptions);
await tsFile.save();
this.printMessage(`[${new Date().toLocaleTimeString()}] The definitions have been updated.`, isDebugEnabled);
}
}