graphql-codegen-flutter-freezed
Version:
A stand-alone package to generate Freezed models from GraphQL schema based on the flutter-freezed plugin for GraphQL Code Generator
122 lines (121 loc) • 4.84 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.FreezedFactoryBlock = void 0;
const visitor_plugin_common_1 = require("@graphql-codegen/visitor-plugin-common");
const graphql_1 = require("graphql");
const change_case_all_1 = require("change-case-all");
const parameter_block_1 = require("./parameter-block");
const utils_1 = require("../utils");
class FreezedFactoryBlock {
constructor(_config, _node) {
this._config = _config;
this._node = _node;
/** document the constructor */
this._comment = '';
/** a list of decorators to copy paste to the generator */
this._decorators = [];
/** a list of interfaces to implements */
// _implements: string[] = [];
/** a list of class to mixin with */
// _mixins: string[] = [];
/** the parameters of this factory constructor */
// TODO: handle other parameter types like positional parameters later.
// TODO: sticking to named parameters because GraphQL is a typed language
this._parameters = [];
this._config = _config;
this._node = _node;
this._freezedConfigValue = new utils_1.FreezedConfigValue(_config, _node.name.value);
}
init() {
/*
setDecorators(), setName() and setType() will be called
when the factory is retrieved from the repository
*/
this.setComment().setParameters().setShape().setBlock();
return this;
}
setComment() {
var _a;
const comment = (_a = this._node.description) === null || _a === void 0 ? void 0 : _a.value;
if (comment && comment !== null && comment !== '') {
this._comment = (0, visitor_plugin_common_1.indent)(`/// ${comment} \n`);
}
return this;
}
setDecorators(appliesOn, nodeName) {
this._decorators = [
...(0, utils_1.transformCustomDecorators)((0, utils_1.getCustomDecorators)(this._config, appliesOn.split(','), nodeName), this._node),
];
return this;
}
setKey(key) {
this._key = (0, change_case_all_1.pascalCase)(key);
return this;
}
setName(name) {
this._name = (0, change_case_all_1.pascalCase)(name);
return this;
}
setNamedConstructor(namedConstructor) {
if (namedConstructor) {
this._namedConstructor = (0, change_case_all_1.camelCase)(namedConstructor);
}
return this;
}
setParameters() {
var _a, _b, _c;
const appliesOn = this._namedConstructor
? ['union_factory_parameter']
: ['class_factory_parameter'];
if (this._node.kind !== graphql_1.Kind.UNION_TYPE_DEFINITION && this._node.kind !== graphql_1.Kind.ENUM_TYPE_DEFINITION) {
this._parameters =
(_c = (_b = (_a = this._node) === null || _a === void 0 ? void 0 : _a.fields) === null || _b === void 0 ? void 0 : _b.map((field) => new parameter_block_1.FreezedParameterBlock(this._config, appliesOn, this._node, field).init())) !== null && _c !== void 0 ? _c : [];
}
return this;
}
setShape() {
this._shape = this._parameters.map(p => p.toString()).join('');
return this;
}
setBlock() {
let block = '';
//append comment
block += this._comment;
// append the decorators
block += this._decorators.map(d => (0, visitor_plugin_common_1.indent)(`${d}\n`)).join('');
block += (0, visitor_plugin_common_1.indent)('');
// decide if to use const or not
if (this._freezedConfigValue.get('immutable')) {
block += 'const ';
}
// append the factory keyword and the name
block += `factory ${this._name}`;
// append .namedConstructor is not null
if (this._namedConstructor && this._namedConstructor !== '') {
block += `.${this._namedConstructor}`;
}
// append the parenthesis for the constructor and braces for the named parameters
block += '({\n';
//append the shape
block += this._shape;
// close the constructor and assign the key
block += (0, visitor_plugin_common_1.indent)(`}) = `);
// but first decide whether prefix the key with an underscore
if (!this._namedConstructor) {
block += '_';
}
// finally, append the key
block += `${this._key};\n`;
// store it in the shape
this._block = block;
return this;
}
/** returns the block */
toString() {
if (!this._block) {
throw new Error('FreezedFactoryBlock: setShape must be called before calling toString()');
}
return this._block;
}
}
exports.FreezedFactoryBlock = FreezedFactoryBlock;