1 | "use strict";
|
2 | var GraphQLSchemaFactory_1;
|
3 | Object.defineProperty(exports, "__esModule", { value: true });
|
4 | exports.GraphQLSchemaFactory = void 0;
|
5 | const tslib_1 = require("tslib");
|
6 | const common_1 = require("@nestjs/common");
|
7 | const shared_utils_1 = require("@nestjs/common/utils/shared.utils");
|
8 | const graphql_1 = require("graphql");
|
9 | const graphql_constants_1 = require("../graphql.constants");
|
10 | const scalar_types_utils_1 = require("../utils/scalar-types.utils");
|
11 | const schema_generation_error_1 = require("./errors/schema-generation.error");
|
12 | const mutation_type_factory_1 = require("./factories/mutation-type.factory");
|
13 | const orphaned_types_factory_1 = require("./factories/orphaned-types.factory");
|
14 | const query_type_factory_1 = require("./factories/query-type.factory");
|
15 | const subscription_type_factory_1 = require("./factories/subscription-type.factory");
|
16 | const lazy_metadata_storage_1 = require("./storages/lazy-metadata.storage");
|
17 | const type_metadata_storage_1 = require("./storages/type-metadata.storage");
|
18 | const type_definitions_generator_1 = require("./type-definitions.generator");
|
19 | let GraphQLSchemaFactory = GraphQLSchemaFactory_1 = class GraphQLSchemaFactory {
|
20 | constructor(queryTypeFactory, mutationTypeFactory, subscriptionTypeFactory, orphanedTypesFactory, typeDefinitionsGenerator) {
|
21 | this.queryTypeFactory = queryTypeFactory;
|
22 | this.mutationTypeFactory = mutationTypeFactory;
|
23 | this.subscriptionTypeFactory = subscriptionTypeFactory;
|
24 | this.orphanedTypesFactory = orphanedTypesFactory;
|
25 | this.typeDefinitionsGenerator = typeDefinitionsGenerator;
|
26 | this.logger = new common_1.Logger(GraphQLSchemaFactory_1.name);
|
27 | }
|
28 | async create(resolvers, scalarsOrOptions = [], options = {}) {
|
29 | if (Array.isArray(scalarsOrOptions)) {
|
30 | this.assignScalarObjects(scalarsOrOptions, options);
|
31 | }
|
32 | else {
|
33 | options = scalarsOrOptions;
|
34 | }
|
35 | lazy_metadata_storage_1.LazyMetadataStorage.load(resolvers);
|
36 | type_metadata_storage_1.TypeMetadataStorage.compile(options.orphanedTypes);
|
37 | this.typeDefinitionsGenerator.generate(options);
|
38 | const schema = new graphql_1.GraphQLSchema({
|
39 | mutation: this.mutationTypeFactory.create(resolvers, options),
|
40 | query: this.queryTypeFactory.create(resolvers, options),
|
41 | subscription: this.subscriptionTypeFactory.create(resolvers, options),
|
42 | types: [
|
43 | ...this.orphanedTypesFactory.create(options.orphanedTypes),
|
44 | ...(options.scalarsMap ?? []).map(({ scalar }) => scalar),
|
45 | ],
|
46 | directives: [...graphql_1.specifiedDirectives, ...(options.directives ?? [])],
|
47 | });
|
48 | if (!options.skipCheck) {
|
49 | const introspectionQuery = (0, graphql_1.getIntrospectionQuery)();
|
50 | const executionResult = await (0, graphql_1.graphql)({
|
51 | schema,
|
52 | source: introspectionQuery,
|
53 | });
|
54 | const errors = executionResult.errors;
|
55 | if (errors) {
|
56 | throw new schema_generation_error_1.SchemaGenerationError(errors);
|
57 | }
|
58 | }
|
59 | return schema;
|
60 | }
|
61 | assignScalarObjects(scalars, options) {
|
62 | if ((0, shared_utils_1.isEmpty)(scalars)) {
|
63 | return;
|
64 | }
|
65 | const scalarsMap = options.scalarsMap || [];
|
66 | scalars
|
67 | .filter((classRef) => classRef)
|
68 | .forEach((classRef) => this.addScalarTypeByClassRef(classRef, scalarsMap));
|
69 | options.scalarsMap = scalarsMap;
|
70 | }
|
71 | addScalarTypeByClassRef(classRef, scalarsMap) {
|
72 | try {
|
73 | const scalarNameMetadata = Reflect.getMetadata(graphql_constants_1.SCALAR_NAME_METADATA, classRef);
|
74 | const scalarTypeMetadata = Reflect.getMetadata(graphql_constants_1.SCALAR_TYPE_METADATA, classRef);
|
75 | if (!scalarNameMetadata) {
|
76 | return;
|
77 | }
|
78 | const instance = new classRef();
|
79 | const type = ((0, shared_utils_1.isFunction)(scalarTypeMetadata) && scalarTypeMetadata()) || classRef;
|
80 | scalarsMap.push({
|
81 | type,
|
82 | scalar: (0, scalar_types_utils_1.createScalarType)(scalarNameMetadata, instance),
|
83 | });
|
84 | }
|
85 | catch {
|
86 | this.logger.error(`Cannot generate a GraphQLScalarType for "${classRef.name}" scalar. Make sure to put any initialization logic in the lifecycle hooks instead of a constructor.`);
|
87 | }
|
88 | }
|
89 | };
|
90 | exports.GraphQLSchemaFactory = GraphQLSchemaFactory;
|
91 | exports.GraphQLSchemaFactory = GraphQLSchemaFactory = GraphQLSchemaFactory_1 = tslib_1.__decorate([
|
92 | (0, common_1.Injectable)(),
|
93 | tslib_1.__metadata("design:paramtypes", [query_type_factory_1.QueryTypeFactory,
|
94 | mutation_type_factory_1.MutationTypeFactory,
|
95 | subscription_type_factory_1.SubscriptionTypeFactory,
|
96 | orphaned_types_factory_1.OrphanedTypesFactory,
|
97 | type_definitions_generator_1.TypeDefinitionsGenerator])
|
98 | ], GraphQLSchemaFactory);
|