@nestjs/graphql
Version:
Nest - modern, fast, powerful node.js web framework (@graphql)
64 lines (63 loc) • 3.04 kB
JavaScript
import { __decorate, __metadata } from "tslib";
import { Injectable } from '@nestjs/common';
import { lexicographicSortSchema, printSchema } from 'graphql';
import { GRAPHQL_SDL_FILE_HEADER, GRAPHQL_SDL_FILE_END, } from './graphql.constants.js';
import { GraphQLSchemaFactory } from './schema-builder/graphql-schema.factory.js';
import { FileSystemHelper } from './schema-builder/helpers/file-system.helper.js';
import { ScalarsExplorerService } from './services/index.js';
import { getPathForAutoSchemaFile } from './utils/index.js';
let GraphQLSchemaBuilder = class GraphQLSchemaBuilder {
constructor(scalarsExplorerService, gqlSchemaFactory, fileSystemHelper) {
this.scalarsExplorerService = scalarsExplorerService;
this.gqlSchemaFactory = gqlSchemaFactory;
this.fileSystemHelper = fileSystemHelper;
}
async build(autoSchemaFile, options, resolvers) {
const scalarsMap = this.scalarsExplorerService.getScalarsMap();
try {
const buildSchemaOptions = options.buildSchemaOptions || {};
return await this.generateSchema(resolvers, autoSchemaFile, {
...buildSchemaOptions,
scalarsMap: [
...(buildSchemaOptions.scalarsMap ?? []),
...scalarsMap.filter(({ type }) => buildSchemaOptions.scalarsMap?.every((item) => item.type !== type) ?? true),
],
// Pass include modules for type filtering in Code First approach
// This property is handled internally by GraphQLSchemaFactory
includeModules: options.include,
}, options.sortSchema, options.transformAutoSchemaFile && options.transformSchema);
}
catch (err) {
if (err && err.details) {
console.error(err.details);
}
throw err;
}
}
async generateSchema(resolvers, autoSchemaFile, options = {}, sortSchema, transformSchema, printSchemaFn) {
const schema = await this.gqlSchemaFactory.create(resolvers, options);
const filename = getPathForAutoSchemaFile(autoSchemaFile);
if (filename) {
const transformedSchema = transformSchema
? await transformSchema(schema)
: schema;
const finalSchema = sortSchema
? lexicographicSortSchema(transformedSchema)
: transformedSchema;
const print = printSchemaFn ?? printSchema;
let fileContent = GRAPHQL_SDL_FILE_HEADER + print(finalSchema);
if (options.addNewlineAtEnd) {
fileContent = fileContent.concat(GRAPHQL_SDL_FILE_END);
}
await this.fileSystemHelper.writeFile(filename, fileContent);
}
return schema;
}
};
GraphQLSchemaBuilder = __decorate([
Injectable(),
__metadata("design:paramtypes", [ScalarsExplorerService,
GraphQLSchemaFactory,
FileSystemHelper])
], GraphQLSchemaBuilder);
export { GraphQLSchemaBuilder };