UNPKG

2.19 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.GraphQLFederationDefinitionsFactory = void 0;
4const load_package_util_1 = require("@nestjs/common/utils/load-package.util");
5const graphql_tag_1 = require("graphql-tag");
6const graphql_definitions_factory_1 = require("../graphql-definitions.factory");
7const utils_1 = require("../utils");
8const merge_1 = require("@graphql-tools/merge");
9class GraphQLFederationDefinitionsFactory extends graphql_definitions_factory_1.GraphQLDefinitionsFactory {
10 async exploreAndEmit(typePaths, path, outputAs, isDebugEnabled, definitionsGeneratorOptions, typeDefs) {
11 const typePathDefs = await this.gqlTypesLoader.mergeTypesByPaths(typePaths);
12 const mergedTypeDefs = (0, utils_1.extend)(typePathDefs, typeDefs);
13 const { buildSubgraphSchema } = (0, load_package_util_1.loadPackage)('@apollo/subgraph', 'ApolloFederation', () => require('@apollo/subgraph'));
14 const { printSubgraphSchema } = (0, load_package_util_1.loadPackage)('@apollo/subgraph', 'ApolloFederation', () => require('@apollo/subgraph'));
15 const schema = buildSubgraphSchema([
16 {
17 typeDefs: (0, graphql_tag_1.gql) `
18 ${mergedTypeDefs}
19 `,
20 resolvers: {},
21 },
22 ]);
23 // "buildSubgraphSchema" generates an empty Query definition if there is the `extend type Query` statement
24 // This leads to duplicated IQuery interfaces
25 // see: https://github.com//issues/2344
26 const mergedDefinition = (0, merge_1.mergeTypeDefs)([printSubgraphSchema(schema)], {
27 useSchemaDefinition: false,
28 throwOnConflict: true,
29 commentDescriptions: true,
30 reverseDirectives: true,
31 });
32 const tsFile = await this.gqlAstExplorer.explore((0, graphql_tag_1.gql) `
33 ${mergedDefinition}
34 `, path, outputAs, definitionsGeneratorOptions);
35 await tsFile.save();
36 this.printMessage(`[${new Date().toLocaleTimeString()}] The definitions have been updated.`, isDebugEnabled);
37 }
38}
39exports.GraphQLFederationDefinitionsFactory = GraphQLFederationDefinitionsFactory;