@nestjs/graphql
Version:
Nest - modern, fast, powerful node.js web framework (@graphql)
51 lines (50 loc) • 2.28 kB
JavaScript
import { __decorate, __metadata } from "tslib";
import { Injectable } from '@nestjs/common';
import { GraphQLEnumType } from 'graphql';
import { AstDefinitionNodeFactory } from './ast-definition-node.factory.js';
let EnumDefinitionFactory = class EnumDefinitionFactory {
constructor(astDefinitionNodeFactory) {
this.astDefinitionNodeFactory = astDefinitionNodeFactory;
}
create(metadata) {
const enumValues = this.getEnumValues(metadata.ref);
return {
enumRef: metadata.ref,
type: new 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;
}, {});
}
};
EnumDefinitionFactory = __decorate([
Injectable(),
__metadata("design:paramtypes", [AstDefinitionNodeFactory])
], EnumDefinitionFactory);
export { EnumDefinitionFactory };