UNPKG

2.61 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 graphql_1 = require("graphql");
7const graphql_constants_1 = require("./graphql.constants");
8const graphql_schema_factory_1 = require("./schema-builder/graphql-schema.factory");
9const file_system_helper_1 = require("./schema-builder/helpers/file-system.helper");
10const services_1 = require("./services");
11const utils_1 = require("./utils");
12let GraphQLSchemaBuilder = class GraphQLSchemaBuilder {
13 constructor(scalarsExplorerService, gqlSchemaFactory, fileSystemHelper) {
14 this.scalarsExplorerService = scalarsExplorerService;
15 this.gqlSchemaFactory = gqlSchemaFactory;
16 this.fileSystemHelper = fileSystemHelper;
17 }
18 async build(autoSchemaFile, options, resolvers) {
19 const scalarsMap = this.scalarsExplorerService.getScalarsMap();
20 try {
21 const buildSchemaOptions = options.buildSchemaOptions || {};
22 return await this.generateSchema(resolvers, autoSchemaFile, {
23 ...buildSchemaOptions,
24 scalarsMap,
25 }, 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 async generateSchema(resolvers, autoSchemaFile, options = {}, sortSchema, transformSchema) {
35 const schema = await this.gqlSchemaFactory.create(resolvers, options);
36 const filename = (0, utils_1.getPathForAutoSchemaFile)(autoSchemaFile);
37 if (filename) {
38 const transformedSchema = transformSchema
39 ? await transformSchema(schema)
40 : schema;
41 const fileContent = graphql_constants_1.GRAPHQL_SDL_FILE_HEADER +
42 (0, graphql_1.printSchema)(sortSchema
43 ? (0, graphql_1.lexicographicSortSchema)(transformedSchema)
44 : transformedSchema);
45 await this.fileSystemHelper.writeFile(filename, fileContent);
46 }
47 return schema;
48 }
49};
50exports.GraphQLSchemaBuilder = GraphQLSchemaBuilder;
51exports.GraphQLSchemaBuilder = GraphQLSchemaBuilder = tslib_1.__decorate([
52 (0, common_1.Injectable)(),
53 tslib_1.__metadata("design:paramtypes", [services_1.ScalarsExplorerService,
54 graphql_schema_factory_1.GraphQLSchemaFactory,
55 file_system_helper_1.FileSystemHelper])
56], GraphQLSchemaBuilder);