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
151 lines (150 loc) • 6.8 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.FreezedParameterBlock = exports.DART_SCALARS = void 0;
const visitor_plugin_common_1 = require("@graphql-codegen/visitor-plugin-common");
const change_case_all_1 = require("change-case-all");
const utils_1 = require("../utils");
/**
* maps GraphQL scalar types to Dart's scalar types
*/
exports.DART_SCALARS = {
ID: 'String',
String: 'String',
Boolean: 'bool',
Int: 'int',
Float: 'double',
DateTime: 'DateTime',
};
class FreezedParameterBlock {
constructor(_config, _appliesOn, _node, _field) {
this._config = _config;
this._appliesOn = _appliesOn;
this._node = _node;
this._field = _field;
/** document the property */
this._comment = '';
/** a list of decorators to copy paste to the generator */
this._decorators = [];
this.propertyType = (field, type, parentType) => {
if (this.isNonNullType(type)) {
return this.propertyType(field, type.type, type);
}
if (this.isListType(type)) {
const T = this.propertyType(field, type.type, type);
return `List<${T}>${this.isNonNullType(parentType) ? '' : '?'}`;
}
if (this.isNamedType(type)) {
return `${this.scalar(type.name.value)}${this.isNonNullType(parentType) ? '' : '?'}`;
}
return '';
};
this.isListType = (type) => (type === null || type === void 0 ? void 0 : type.kind) === 'ListType';
this.isNonNullType = (type) => (type === null || type === void 0 ? void 0 : type.kind) === 'NonNullType';
this.isNamedType = (type) => (type === null || type === void 0 ? void 0 : type.kind) === 'NamedType';
this._config = _config;
this._appliesOn = _appliesOn;
this._node = _node;
this._field = _field;
this._freezedConfigValue = new utils_1.FreezedConfigValue(_config, _node.name.value);
}
init() {
this.setComment().setDecorators().setRequired().setType().setName().setShape().setBlock();
return this;
}
setComment() {
var _a;
const comment = (_a = this._field.description) === null || _a === void 0 ? void 0 : _a.value;
if (comment && comment !== null && comment !== '') {
this._comment = (0, visitor_plugin_common_1.indent)(`/// ${comment}\n`, 2);
}
return this;
}
setDecorators() {
var _a, _b, _c, _d, _e, _f, _g, _h;
const nodeName = this._node.name.value;
const fieldName = this._field.name.value;
// determine if should mark as deprecated
const isDeprecated = (_d = (_c = (_b = (_a = this._config.typeSpecificFreezedConfig) === null || _a === void 0 ? void 0 : _a[nodeName]) === null || _b === void 0 ? void 0 : _b.fields) === null || _c === void 0 ? void 0 : _c[fieldName]) === null || _d === void 0 ? void 0 : _d.deprecated;
const defaultValue = (_h = (_g = (_f = (_e = this._config.typeSpecificFreezedConfig) === null || _e === void 0 ? void 0 : _e[nodeName]) === null || _f === void 0 ? void 0 : _f.fields) === null || _g === void 0 ? void 0 : _g[fieldName]) === null || _h === void 0 ? void 0 : _h.defaultValue;
if (this._freezedConfigValue.get('alwaysUseJsonKeyName') || fieldName !== (0, change_case_all_1.camelCase)(fieldName)) {
this._decorators = [...this._decorators, ``];
}
this._decorators = [
...this._decorators,
...(0, utils_1.transformCustomDecorators)((0, utils_1.getCustomDecorators)(this._config, this._appliesOn, this._node.name.value, fieldName), this._node, this._field),
];
// @deprecated
// if this._decorators doesn't include an @deprecated decorator but the field is marked as @deprecated...
if (!this._decorators.includes('@deprecated') && isDeprecated) {
this._decorators = [...this._decorators, '@deprecated'];
}
// @Default
if (defaultValue) {
//overwrite the customDecorator's defaultValue
this._decorators = this._decorators.filter(d => !d.startsWith('@Default'));
this._decorators = [...this._decorators, ``];
}
return this;
}
setRequired() {
this._required = this.isNonNullType(this._field.type);
return this;
}
setType() {
this._type = this.propertyType(this._field, this._field.type);
return this;
}
setName() {
this._name = (0, change_case_all_1.camelCase)(this._field.name.value);
return this;
}
/** compose the freezed constructor property */
setShape() {
var _a, _b, _c, _d;
let shape = '';
const nodeName = this._node.name.value;
const fieldName = this._field.name.value;
// determine if should mark as final
const isFinal = this._decorators.includes('final') ||
((_d = (_c = (_b = (_a = this._config.typeSpecificFreezedConfig) === null || _a === void 0 ? void 0 : _a[nodeName]) === null || _b === void 0 ? void 0 : _b.fields) === null || _c === void 0 ? void 0 : _c[fieldName]) === null || _d === void 0 ? void 0 : _d.final);
//append comment
shape += this._comment;
// append the decorators
shape += this._decorators
.filter(d => d !== 'final')
.map(d => (0, visitor_plugin_common_1.indent)(`${d}\n`, 2))
.join('');
// append required for non-nullable types
shape += (0, visitor_plugin_common_1.indent)(this._required ? 'required ' : '', 2);
// append isFinal
shape += isFinal ? 'final ' : '';
// append the Dart Type, name and trailing comma
shape += `${this._type} ${this._name},\n`;
// store it in the shape
this._shape = shape;
return this;
}
/** composes the full block */
setBlock() {
this._block = this._shape;
return this;
}
scalar(_scalar) {
var _a, _b;
if ((_b = (_a = this._config) === null || _a === void 0 ? void 0 : _a.customScalars) === null || _b === void 0 ? void 0 : _b[_scalar]) {
return this._config.customScalars[_scalar];
}
if (exports.DART_SCALARS[_scalar]) {
return exports.DART_SCALARS[_scalar];
}
return _scalar;
}
/** returns the block */
toString() {
if (!this._block) {
throw new Error('FreezedParameterBlock: setShape must be called before calling toString()');
}
return this._block;
}
}
exports.FreezedParameterBlock = FreezedParameterBlock;