UNPKG

@graphql-codegen/flutter-freezed

Version:

GraphQL Code Generator plugin to generate Freezed models from your GraphQL schema

51 lines (50 loc) 2.24 kB
import { indent } from '@graphql-codegen/visitor-plugin-common'; import { Config } from '../config/config-value.js'; import { FieldName, TypeName } from '../config/pattern.js'; import { APPLIES_ON_ENUM, APPLIES_ON_ENUM_VALUE, } from '../config/plugin-config.js'; import { atJsonKeyDecorator, stringIsNotEmpty } from '../utils.js'; import { Block } from './index.js'; export class EnumBlock { // TODO: @next-version: Implement enhanced enums static build(config, node) { const typeName = TypeName.fromString(node.name.value); let block = ''; block += Block.buildComment(node); block += this.buildDecorators(); block += this.buildHeader(config, typeName); block += this.buildBody(config, node); block += this.buildFooter(); return block; } } EnumBlock.buildDecorators = () => { // TODO: @next-version: @JsonEnum(valueField: 'code', fieldRename: 'new-name') return ''; }; EnumBlock.buildHeader = (config, typeName) => { const enumTypeName = Block.buildBlockName(config, APPLIES_ON_ENUM, typeName.value, typeName, undefined, 'PascalCase'); return `enum ${enumTypeName} {\n`; }; EnumBlock.buildBody = (config, node) => { var _a, _b; const typeName = TypeName.fromString(node.name.value); return (_b = ((_a = node.values) !== null && _a !== void 0 ? _a : [])) === null || _b === void 0 ? void 0 : _b.map((enumValue) => { const fieldName = FieldName.fromString(enumValue.name.value); const enumValueName = Block.buildBlockName(config, APPLIES_ON_ENUM_VALUE, fieldName.value, typeName, fieldName, Config.camelCasedEnums(config)); const comment = Block.buildComment(enumValue); const jsonKey = atJsonKeyDecorator({ name: fieldName.value !== enumValueName ? fieldName.value : undefined, }); //TODO: @next-version: const jsonValue = @JsonValue(String|int) const decorators = [ jsonKey, // jsonValue ].join(''); return [comment, decorators, `${enumValueName},\n`] .map(block => (stringIsNotEmpty(block) ? indent(block) : block)) .join(''); }).join(''); }; EnumBlock.buildFooter = () => { return '}\n\n'; };