UNPKG

@graphql-codegen/flutter-freezed

Version:

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

98 lines (97 loc) 5.44 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.FactoryBlock = void 0; const visitor_plugin_common_1 = require("@graphql-codegen/visitor-plugin-common"); const config_value_js_1 = require("../config/config-value.js"); const pattern_js_1 = require("../config/pattern.js"); const plugin_config_js_1 = require("../config/plugin-config.js"); const utils_js_1 = require("../utils.js"); const index_js_1 = require("./index.js"); const parameter_block_js_1 = require("./parameter-block.js"); class FactoryBlock { static build(config, node, blockAppliesOn, className, factoryName) { let block = ''; block += index_js_1.Block.buildComment(node); block += this.buildDecorators(config, blockAppliesOn, className, factoryName); block += this.buildHeader(config, blockAppliesOn, className, factoryName); block += this.buildBody(config, node, blockAppliesOn); factoryName = blockAppliesOn.includes('default_factory') ? className : factoryName; block += this.buildFooter(config, blockAppliesOn, factoryName); return block; } } exports.FactoryBlock = FactoryBlock; FactoryBlock.buildDecorators = (config, blockAppliesOn, className, factoryName) => { // TODO: @Assert const typeName = factoryName ? pattern_js_1.TypeName.fromUnionOfTypeNames(className, factoryName) : className; const deprecatedDecorator = config_value_js_1.Config.deprecated(config, blockAppliesOn, typeName); const decorators = [deprecatedDecorator].join(''); return (0, utils_js_1.stringIsNotEmpty)(decorators) ? (0, visitor_plugin_common_1.indent)(decorators) : decorators; }; FactoryBlock.buildHeader = (config, blockAppliesOn, className, factoryName) => { const typeName = factoryName ? pattern_js_1.TypeName.fromUnionOfTypeNames(className, factoryName) : className; const immutable = config_value_js_1.Config.immutable(config, typeName); // const mutableInputs = Config.mutableInputs(config, factoryName); // const mutable = immutable !== true || (node.kind === Kind.INPUT_OBJECT_TYPE_DEFINITION && mutableInputs); const constFactory = immutable ? (0, visitor_plugin_common_1.indent)('const factory') : (0, visitor_plugin_common_1.indent)('factory'); const _className = index_js_1.Block.buildBlockName(config, blockAppliesOn, className.value, className, undefined, 'PascalCase'); if (factoryName) { const _factoryName = index_js_1.Block.buildBlockName(config, blockAppliesOn, factoryName.value, factoryName, undefined, 'camelCase'); return `${constFactory} ${_className}.${_factoryName}({\n`; } return `${constFactory} ${_className}({\n`; }; FactoryBlock.buildBody = (config, node, appliesOn) => { var _a, _b; let appliesOnParameters = []; if (appliesOn.includes('default_factory')) { appliesOnParameters = plugin_config_js_1.APPLIES_ON_DEFAULT_FACTORY_PARAMETERS; } else if (appliesOn.includes('union_factory')) { appliesOnParameters = plugin_config_js_1.APPLIES_ON_UNION_FACTORY_PARAMETERS; } else if (appliesOn.includes('merged_factory')) { appliesOnParameters = plugin_config_js_1.APPLIES_ON_MERGED_FACTORY_PARAMETERS; } return ((_b = (_a = node.fields) === null || _a === void 0 ? void 0 : _a.map((field) => { return parameter_block_js_1.ParameterBlock.build(config, node, field, appliesOnParameters); }).join('')) !== null && _b !== void 0 ? _b : ''); }; FactoryBlock.buildFooter = (config, blockAppliesOn, factoryName) => { const _ = blockAppliesOn.includes('default_factory') ? '_' : ''; const _factoryName = index_js_1.Block.buildBlockName(config, blockAppliesOn, factoryName.value, factoryName, undefined, 'PascalCase'); return (0, visitor_plugin_common_1.indent)(`}) = ${_}${_factoryName};\n\n`); }; FactoryBlock.serializeDefaultFactory = (className) => { return `${index_js_1.Block.tokens.defaultFactory}${className.value}==>${plugin_config_js_1.APPLIES_ON_DEFAULT_FACTORY.join(',')}\n`; }; FactoryBlock.serializeUnionFactory = (className, factoryName) => { return `${index_js_1.Block.tokens.unionFactory}${className.value}==>${factoryName.value}==>${plugin_config_js_1.APPLIES_ON_UNION_FACTORY.join(',')}\n`; }; FactoryBlock.serializeMergedFactory = (className, factoryName) => { return `${index_js_1.Block.tokens.mergedFactory}${className.value}==>${factoryName.value}==>${plugin_config_js_1.APPLIES_ON_MERGED_FACTORY.join(',')}\n`; }; FactoryBlock.deserializeFactory = (config, nodeRepository, blockAppliesOn, className) => { const node = nodeRepository.get(className.value); if (node) { return FactoryBlock.buildFromFactory(config, node, blockAppliesOn, className); } return ''; }; FactoryBlock.deserializeNamedFactory = (config, nodeRepository, blockAppliesOn, className, factoryName) => { const node = nodeRepository.get(factoryName.value); if (node) { return FactoryBlock.buildFromNamedFactory(config, node, blockAppliesOn, className, factoryName); } return ''; }; FactoryBlock.buildFromFactory = (config, node, blockAppliesOn, className) => { return FactoryBlock.build(config, node, blockAppliesOn, className); }; FactoryBlock.buildFromNamedFactory = (config, node, blockAppliesOn, className, factoryName) => { return FactoryBlock.build(config, node, blockAppliesOn, className, factoryName); };