1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.GraphQLSchemaBuilder = void 0;
|
4 | const tslib_1 = require("tslib");
|
5 | const common_1 = require("@nestjs/common");
|
6 | const graphql_1 = require("graphql");
|
7 | const graphql_constants_1 = require("./graphql.constants");
|
8 | const graphql_schema_factory_1 = require("./schema-builder/graphql-schema.factory");
|
9 | const file_system_helper_1 = require("./schema-builder/helpers/file-system.helper");
|
10 | const services_1 = require("./services");
|
11 | const utils_1 = require("./utils");
|
12 | let 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 | };
|
50 | exports.GraphQLSchemaBuilder = GraphQLSchemaBuilder;
|
51 | exports.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);
|