UNPKG

@nestjs/graphql

Version:

Nest - modern, fast, powerful node.js web framework (@graphql)

54 lines (53 loc) 2.5 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.EnumDefinitionFactory = void 0; const tslib_1 = require("tslib"); const common_1 = require("@nestjs/common"); const graphql_1 = require("graphql"); const ast_definition_node_factory_1 = require("./ast-definition-node.factory"); let EnumDefinitionFactory = class EnumDefinitionFactory { constructor(astDefinitionNodeFactory) { this.astDefinitionNodeFactory = astDefinitionNodeFactory; } create(metadata) { const enumValues = this.getEnumValues(metadata.ref); return { enumRef: metadata.ref, type: new graphql_1.GraphQLEnumType({ name: metadata.name, description: metadata.description, values: Object.keys(enumValues).reduce((prevValue, key) => { const valueMap = metadata.valuesMap[key]; prevValue[key] = { value: enumValues[key], description: valueMap?.description, deprecationReason: valueMap?.deprecationReason, /** * AST node has to be manually created in order to define directives * (more on this topic here: https://github.com/graphql/graphql-js/issues/1343) */ astNode: this.astDefinitionNodeFactory.createEnumValueNode(key, valueMap?.directives), }; return prevValue; }, {}), /** * AST node has to be manually created in order to define directives * (more on this topic here: https://github.com/graphql/graphql-js/issues/1343) */ astNode: this.astDefinitionNodeFactory.createEnumTypeNode(metadata.name, metadata.directives), }), }; } getEnumValues(enumObject) { const enumKeys = Object.keys(enumObject).filter((key) => isNaN(parseInt(key, 10))); return enumKeys.reduce((prev, nextKey) => { prev[nextKey] = enumObject[nextKey]; return prev; }, {}); } }; exports.EnumDefinitionFactory = EnumDefinitionFactory; exports.EnumDefinitionFactory = EnumDefinitionFactory = tslib_1.__decorate([ (0, common_1.Injectable)(), tslib_1.__metadata("design:paramtypes", [ast_definition_node_factory_1.AstDefinitionNodeFactory]) ], EnumDefinitionFactory);