UNPKG

4.4 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.GraphQLSchemaBuilder = void 0;
4const tslib_1 = require("tslib");
5const common_1 = require("@nestjs/common");
6const load_package_util_1 = require("@nestjs/common/utils/load-package.util");
7const shared_utils_1 = require("@nestjs/common/utils/shared.utils");
8const graphql_1 = require("graphql");
9const path_1 = require("path");
10const graphql_constants_1 = require("./graphql.constants");
11const graphql_schema_factory_1 = require("./schema-builder/graphql-schema.factory");
12const file_system_helper_1 = require("./schema-builder/helpers/file-system.helper");
13const services_1 = require("./services");
14let GraphQLSchemaBuilder = class GraphQLSchemaBuilder {
15 constructor(scalarsExplorerService, gqlSchemaFactory, fileSystemHelper) {
16 this.scalarsExplorerService = scalarsExplorerService;
17 this.gqlSchemaFactory = gqlSchemaFactory;
18 this.fileSystemHelper = fileSystemHelper;
19 }
20 build(autoSchemaFile, options, resolvers) {
21 return tslib_1.__awaiter(this, void 0, void 0, function* () {
22 const scalarsMap = this.scalarsExplorerService.getScalarsMap();
23 try {
24 const buildSchemaOptions = options.buildSchemaOptions || {};
25 return yield this.buildSchema(resolvers, autoSchemaFile, Object.assign(Object.assign({}, buildSchemaOptions), { scalarsMap, schemaDirectives: options.schemaDirectives }), options.sortSchema, options.transformAutoSchemaFile && options.transformSchema);
26 }
27 catch (err) {
28 if (err && err.details) {
29 console.error(err.details);
30 }
31 throw err;
32 }
33 });
34 }
35 buildFederatedSchema(autoSchemaFile, options, resolvers) {
36 return tslib_1.__awaiter(this, void 0, void 0, function* () {
37 const scalarsMap = this.scalarsExplorerService.getScalarsMap();
38 try {
39 const buildSchemaOptions = options.buildSchemaOptions || {};
40 return yield this.buildSchema(resolvers, autoSchemaFile, Object.assign(Object.assign({}, buildSchemaOptions), { directives: [
41 ...graphql_1.specifiedDirectives,
42 ...this.loadFederationDirectives(),
43 ...((buildSchemaOptions && buildSchemaOptions.directives) || []),
44 ], scalarsMap, schemaDirectives: options.schemaDirectives, skipCheck: true }), options.sortSchema, options.transformAutoSchemaFile && options.transformSchema);
45 }
46 catch (err) {
47 if (err && err.details) {
48 console.error(err.details);
49 }
50 throw err;
51 }
52 });
53 }
54 buildSchema(resolvers, autoSchemaFile, options = {}, sortSchema, transformSchema) {
55 return tslib_1.__awaiter(this, void 0, void 0, function* () {
56 const schema = yield this.gqlSchemaFactory.create(resolvers, options);
57 if (typeof autoSchemaFile !== 'boolean') {
58 const filename = shared_utils_1.isString(autoSchemaFile)
59 ? autoSchemaFile
60 : path_1.resolve(process.cwd(), 'schema.gql');
61 const transformedSchema = transformSchema
62 ? yield transformSchema(schema)
63 : schema;
64 const fileContent = graphql_constants_1.GRAPHQL_SDL_FILE_HEADER +
65 graphql_1.printSchema(sortSchema
66 ? graphql_1.lexicographicSortSchema(transformedSchema)
67 : transformedSchema);
68 yield this.fileSystemHelper.writeFile(filename, fileContent);
69 }
70 return schema;
71 });
72 }
73 loadFederationDirectives() {
74 const { federationDirectives, } = load_package_util_1.loadPackage('@apollo/federation/dist/directives', 'SchemaBuilder', () => require('@apollo/federation/dist/directives'));
75 return federationDirectives;
76 }
77};
78GraphQLSchemaBuilder = tslib_1.__decorate([
79 common_1.Injectable(),
80 tslib_1.__metadata("design:paramtypes", [services_1.ScalarsExplorerService,
81 graphql_schema_factory_1.GraphQLSchemaFactory,
82 file_system_helper_1.FileSystemHelper])
83], GraphQLSchemaBuilder);
84exports.GraphQLSchemaBuilder = GraphQLSchemaBuilder;