@graphql-codegen/flutter-freezed 2.11.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cjs/config.js +2 -0
- package/cjs/freezed-declaration-blocks/class-block.js +221 -0
- package/cjs/freezed-declaration-blocks/factory-block.js +121 -0
- package/cjs/freezed-declaration-blocks/index.js +6 -0
- package/cjs/freezed-declaration-blocks/parameter-block.js +150 -0
- package/cjs/index.js +27 -0
- package/cjs/package.json +1 -0
- package/cjs/schema-visitor.js +15 -0
- package/cjs/utils.js +221 -0
- package/esm/config.js +1 -0
- package/esm/freezed-declaration-blocks/class-block.js +217 -0
- package/esm/freezed-declaration-blocks/factory-block.js +117 -0
- package/esm/freezed-declaration-blocks/index.js +3 -0
- package/esm/freezed-declaration-blocks/parameter-block.js +146 -0
- package/esm/index.js +23 -0
- package/esm/schema-visitor.js +11 -0
- package/esm/utils.js +209 -0
- package/package.json +46 -0
- package/typings/config.d.cts +453 -0
- package/typings/config.d.ts +453 -0
- package/typings/freezed-declaration-blocks/class-block.d.cts +36 -0
- package/typings/freezed-declaration-blocks/class-block.d.ts +36 -0
- package/typings/freezed-declaration-blocks/factory-block.d.cts +38 -0
- package/typings/freezed-declaration-blocks/factory-block.d.ts +38 -0
- package/typings/freezed-declaration-blocks/index.d.cts +3 -0
- package/typings/freezed-declaration-blocks/index.d.ts +3 -0
- package/typings/freezed-declaration-blocks/parameter-block.d.cts +45 -0
- package/typings/freezed-declaration-blocks/parameter-block.d.ts +45 -0
- package/typings/index.d.cts +3 -0
- package/typings/index.d.ts +3 -0
- package/typings/schema-visitor.d.cts +10 -0
- package/typings/schema-visitor.d.ts +10 -0
- package/typings/utils.d.cts +78 -0
- package/typings/utils.d.ts +78 -0
package/cjs/config.js
ADDED
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.FreezedDeclarationBlock = void 0;
|
|
4
|
+
const visitor_plugin_common_1 = require("@graphql-codegen/visitor-plugin-common");
|
|
5
|
+
const graphql_1 = require("graphql");
|
|
6
|
+
const change_case_all_1 = require("change-case-all");
|
|
7
|
+
const utils_js_1 = require("../utils.js");
|
|
8
|
+
const factory_block_js_1 = require("./factory-block.js");
|
|
9
|
+
class FreezedDeclarationBlock {
|
|
10
|
+
constructor(_config, _freezedFactoryBlockRepository, _node) {
|
|
11
|
+
this._config = _config;
|
|
12
|
+
this._freezedFactoryBlockRepository = _freezedFactoryBlockRepository;
|
|
13
|
+
this._node = _node;
|
|
14
|
+
/** document the class */
|
|
15
|
+
this._comment = '';
|
|
16
|
+
/** a list of decorators to copy paste to the generator */
|
|
17
|
+
this._decorators = [];
|
|
18
|
+
/** a list of default constructor and named Constructors used create a Freezed union/sealed class */
|
|
19
|
+
this._factoryBlocks = [];
|
|
20
|
+
this._config = _config;
|
|
21
|
+
this._freezedFactoryBlockRepository = _freezedFactoryBlockRepository;
|
|
22
|
+
this._node = _node;
|
|
23
|
+
this._freezedConfigValue = new utils_js_1.FreezedConfigValue(this._config, this._node.name.value);
|
|
24
|
+
}
|
|
25
|
+
init() {
|
|
26
|
+
if (this._node.kind === graphql_1.Kind.ENUM_TYPE_DEFINITION) {
|
|
27
|
+
this.setDecorators().setName().setShape().setBlock();
|
|
28
|
+
return this;
|
|
29
|
+
}
|
|
30
|
+
this.setComment().setDecorators().setName().setFactoryBlocks().setShape().setBlock();
|
|
31
|
+
return this;
|
|
32
|
+
}
|
|
33
|
+
setComment() {
|
|
34
|
+
var _a;
|
|
35
|
+
const comment = (_a = this._node.description) === null || _a === void 0 ? void 0 : _a.value;
|
|
36
|
+
if (comment && comment !== null && comment !== '') {
|
|
37
|
+
this._comment = `/// ${comment} \n`;
|
|
38
|
+
}
|
|
39
|
+
return this;
|
|
40
|
+
}
|
|
41
|
+
getEnumComment(value) {
|
|
42
|
+
var _a;
|
|
43
|
+
const comment = (_a = value.description) === null || _a === void 0 ? void 0 : _a.value;
|
|
44
|
+
if (comment && comment !== null && comment !== '') {
|
|
45
|
+
return `/// ${comment} \n`;
|
|
46
|
+
}
|
|
47
|
+
return '';
|
|
48
|
+
}
|
|
49
|
+
setDecorators() {
|
|
50
|
+
var _a, _b;
|
|
51
|
+
const name = this._node.name.value;
|
|
52
|
+
// determine if should mark as deprecated
|
|
53
|
+
const isDeprecated = (_b = (_a = this._config.typeSpecificFreezedConfig) === null || _a === void 0 ? void 0 : _a[name]) === null || _b === void 0 ? void 0 : _b.deprecated;
|
|
54
|
+
this._decorators =
|
|
55
|
+
this._node.kind === graphql_1.Kind.ENUM_TYPE_DEFINITION
|
|
56
|
+
? [...(0, utils_js_1.transformCustomDecorators)((0, utils_js_1.getCustomDecorators)(this._config, ['enum'], name), this._node)]
|
|
57
|
+
: [
|
|
58
|
+
this.getFreezedDecorator(),
|
|
59
|
+
...(0, utils_js_1.transformCustomDecorators)((0, utils_js_1.getCustomDecorators)(this._config, ['class'], name), this._node),
|
|
60
|
+
];
|
|
61
|
+
// @deprecated
|
|
62
|
+
// if this._decorators doesn't include an @deprecated decorator but the field is marked as @deprecated...
|
|
63
|
+
if (!this._decorators.includes('@deprecated') && isDeprecated) {
|
|
64
|
+
this._decorators = [...this._decorators, '@deprecated'];
|
|
65
|
+
}
|
|
66
|
+
return this;
|
|
67
|
+
}
|
|
68
|
+
getFreezedDecorator() {
|
|
69
|
+
const use_unfreezed = () => {
|
|
70
|
+
if (!this._freezedConfigValue.get('immutable') ||
|
|
71
|
+
(this._node.kind === graphql_1.Kind.INPUT_OBJECT_TYPE_DEFINITION && this._freezedConfigValue.get('mutableInputs'))) {
|
|
72
|
+
return '@unfreezed';
|
|
73
|
+
}
|
|
74
|
+
return use_Freezed_or_freezed();
|
|
75
|
+
};
|
|
76
|
+
const use_Freezed_or_freezed = () => {
|
|
77
|
+
// if any of these options is not null, use the @Freezed() decorator passing in that option
|
|
78
|
+
const [isCustomized, copyWith, equal, makeCollectionsUnmodifiable, unionKey, unionValueCase] = isCustomizedFreezed();
|
|
79
|
+
if (isCustomized) {
|
|
80
|
+
return `@Freezed(\n
|
|
81
|
+
${copyWith ? (0, visitor_plugin_common_1.indent)(`copyWith: ${copyWith},\n`) : ''}
|
|
82
|
+
${equal ? (0, visitor_plugin_common_1.indent)(`equal: ${equal},\n`) : ''}
|
|
83
|
+
${makeCollectionsUnmodifiable ? (0, visitor_plugin_common_1.indent)(`makeCollectionsUnmodifiable: ${makeCollectionsUnmodifiable},\n`) : ''}
|
|
84
|
+
${unionKey ? (0, visitor_plugin_common_1.indent)(`unionKey: ${unionKey},\n`) : ''}
|
|
85
|
+
${unionValueCase ? (0, visitor_plugin_common_1.indent)(`unionValueCase: ${unionValueCase},\n`) : ''}
|
|
86
|
+
)`;
|
|
87
|
+
}
|
|
88
|
+
// else fallback to the normal @freezed decorator
|
|
89
|
+
return '@freezed';
|
|
90
|
+
};
|
|
91
|
+
const isCustomizedFreezed = () => {
|
|
92
|
+
const copyWith = this._freezedConfigValue.get('copyWith');
|
|
93
|
+
const equal = this._freezedConfigValue.get('equal');
|
|
94
|
+
const makeCollectionsUnmodifiable = this._freezedConfigValue.get('makeCollectionsUnmodifiable');
|
|
95
|
+
const unionKey = this._freezedConfigValue.get('unionKey');
|
|
96
|
+
const unionValueCase = this._freezedConfigValue.get('unionValueCase');
|
|
97
|
+
const isCustomized = copyWith || equal || makeCollectionsUnmodifiable || unionKey || unionValueCase;
|
|
98
|
+
return [isCustomized, copyWith, equal, makeCollectionsUnmodifiable, unionKey, unionValueCase];
|
|
99
|
+
};
|
|
100
|
+
// this is the start of the pipeline of decisions to determine which Freezed decorator to use
|
|
101
|
+
return use_unfreezed();
|
|
102
|
+
}
|
|
103
|
+
setName() {
|
|
104
|
+
this._name = (0, change_case_all_1.pascalCase)(this._node.name.value);
|
|
105
|
+
return this;
|
|
106
|
+
}
|
|
107
|
+
setFactoryBlocks() {
|
|
108
|
+
var _a, _b, _c, _d;
|
|
109
|
+
if (this._node.kind === graphql_1.Kind.UNION_TYPE_DEFINITION) {
|
|
110
|
+
this._factoryBlocks =
|
|
111
|
+
(_b = (_a = this._node.types) === null || _a === void 0 ? void 0 : _a.map((_type) => new factory_block_js_1.FreezedFactoryBlock(this._config, this._node).init())) !== null && _b !== void 0 ? _b : [];
|
|
112
|
+
}
|
|
113
|
+
else if (this._node.kind !== graphql_1.Kind.ENUM_TYPE_DEFINITION) {
|
|
114
|
+
/*
|
|
115
|
+
for `ObjectTypeDefinitionNode` and `InputObjectTypeDefinitionNode` nodes,
|
|
116
|
+
we use the `ShapeRepository`
|
|
117
|
+
to register the `FreezedFactoryBlock` so that we can use it later
|
|
118
|
+
when we are merging inputs or generating freezed union/sealed classes
|
|
119
|
+
for GraphQL union types
|
|
120
|
+
*/
|
|
121
|
+
this._factoryBlocks =
|
|
122
|
+
(_d = (_c = this._node.fields) === null || _c === void 0 ? void 0 : _c.map((_field) => this._freezedFactoryBlockRepository.register(this._node.name.value, new factory_block_js_1.FreezedFactoryBlock(this._config, this._node).init()))) !== null && _d !== void 0 ? _d : [];
|
|
123
|
+
}
|
|
124
|
+
return this;
|
|
125
|
+
}
|
|
126
|
+
setShape() {
|
|
127
|
+
var _a, _b, _c;
|
|
128
|
+
let shape = '';
|
|
129
|
+
// some helper variables
|
|
130
|
+
const name = this._node.name.value;
|
|
131
|
+
let namedConstructor;
|
|
132
|
+
let factoryBlockKey;
|
|
133
|
+
// handle enums differently
|
|
134
|
+
if (this._node.kind === graphql_1.Kind.ENUM_TYPE_DEFINITION) {
|
|
135
|
+
this._shape = (_a = this._node.values) === null || _a === void 0 ? void 0 : _a.map((value) => {
|
|
136
|
+
var _a;
|
|
137
|
+
shape = (0, visitor_plugin_common_1.indent)(this.getEnumComment(value));
|
|
138
|
+
if ((_a = this._config.lowercaseEnums) !== null && _a !== void 0 ? _a : true) {
|
|
139
|
+
shape += `@JsonKey(name: ${value.name.value}) ${value.name.value.toLowerCase()}`;
|
|
140
|
+
}
|
|
141
|
+
else {
|
|
142
|
+
shape += value.name.value;
|
|
143
|
+
}
|
|
144
|
+
return `${shape}\n`;
|
|
145
|
+
}).join('');
|
|
146
|
+
return this;
|
|
147
|
+
}
|
|
148
|
+
// append private empty constructor
|
|
149
|
+
if (this._freezedConfigValue.get('privateEmptyConstructor')) {
|
|
150
|
+
shape += (0, visitor_plugin_common_1.indent)(`const ${this._name}._();\n\n`);
|
|
151
|
+
}
|
|
152
|
+
// decide whether to append an empty Union constructor
|
|
153
|
+
if (this._freezedConfigValue.get('defaultUnionConstructor') && this._node.kind === graphql_1.Kind.UNION_TYPE_DEFINITION) {
|
|
154
|
+
shape += (0, visitor_plugin_common_1.indent)(`const factory ${this._name}() = _${this._name};\n\n`);
|
|
155
|
+
}
|
|
156
|
+
// append tokens which will be used to retrieve the factory blocks
|
|
157
|
+
// from the FreezedFactoryBlockRepository
|
|
158
|
+
if (this._node.kind === graphql_1.Kind.UNION_TYPE_DEFINITION) {
|
|
159
|
+
(_c = (_b = this._node) === null || _b === void 0 ? void 0 : _b.types) === null || _c === void 0 ? void 0 : _c.forEach(type => {
|
|
160
|
+
namedConstructor = type.name.value;
|
|
161
|
+
factoryBlockKey = namedConstructor;
|
|
162
|
+
shape += `==>factory==>${factoryBlockKey}==>${'union_factory'}==>${name}==>${namedConstructor}\n`;
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
else {
|
|
166
|
+
factoryBlockKey = name;
|
|
167
|
+
// replace token for the ObjectType & InputType to be replaced with the default Freezed constructor
|
|
168
|
+
shape += `==>factory==>${factoryBlockKey}==>${'class_factory'}==>${name}\n`;
|
|
169
|
+
const mergeInputs = this._freezedConfigValue.get('mergeInputs');
|
|
170
|
+
if (this._node.kind === graphql_1.Kind.OBJECT_TYPE_DEFINITION && mergeInputs) {
|
|
171
|
+
// replace token for the InputTypes(a.k.a namedConstructors) as a union/sealed class
|
|
172
|
+
mergeInputs.forEach(input => {
|
|
173
|
+
namedConstructor = (0, change_case_all_1.camelCase)(input.split('$').join('_'));
|
|
174
|
+
factoryBlockKey = input.replace('$', name);
|
|
175
|
+
shape += `==>factory==>${factoryBlockKey}==>${'union_factory'}==>${name}==>${namedConstructor}\n`;
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
this._shape = shape;
|
|
180
|
+
return this;
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* returns the string output of the block
|
|
184
|
+
*/
|
|
185
|
+
setBlock() {
|
|
186
|
+
let block = '';
|
|
187
|
+
//append comment
|
|
188
|
+
block += this._comment;
|
|
189
|
+
// append the decorators
|
|
190
|
+
block += this._decorators.join('\n');
|
|
191
|
+
if (this._decorators !== []) {
|
|
192
|
+
block += '\n';
|
|
193
|
+
}
|
|
194
|
+
// handle enums differently
|
|
195
|
+
if (this._node.kind === graphql_1.Kind.ENUM_TYPE_DEFINITION) {
|
|
196
|
+
block += `enum ${this._name}{\n${this._shape}}\n\n`;
|
|
197
|
+
this._block = block;
|
|
198
|
+
return this;
|
|
199
|
+
}
|
|
200
|
+
// append start of class definition
|
|
201
|
+
block += `class ${this._name} with _$${this._name} {\n`;
|
|
202
|
+
// append the shape
|
|
203
|
+
block += this._shape;
|
|
204
|
+
// append fromJson
|
|
205
|
+
if (this._freezedConfigValue.get('fromJsonToJson')) {
|
|
206
|
+
block += (0, visitor_plugin_common_1.indent)(`factory ${this._name}.fromJson(Map<String, Object?> json) => _${this._name}FromJson(json);\n`);
|
|
207
|
+
}
|
|
208
|
+
//append end of class definition
|
|
209
|
+
block += '}\n\n';
|
|
210
|
+
this._block = block;
|
|
211
|
+
return this;
|
|
212
|
+
}
|
|
213
|
+
/** returns the block */
|
|
214
|
+
toString() {
|
|
215
|
+
if (!this._block) {
|
|
216
|
+
throw new Error('setShape must be called before calling toString()');
|
|
217
|
+
}
|
|
218
|
+
return this._block;
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
exports.FreezedDeclarationBlock = FreezedDeclarationBlock;
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.FreezedFactoryBlock = void 0;
|
|
4
|
+
const visitor_plugin_common_1 = require("@graphql-codegen/visitor-plugin-common");
|
|
5
|
+
const graphql_1 = require("graphql");
|
|
6
|
+
const change_case_all_1 = require("change-case-all");
|
|
7
|
+
const parameter_block_js_1 = require("./parameter-block.js");
|
|
8
|
+
const utils_js_1 = require("../utils.js");
|
|
9
|
+
class FreezedFactoryBlock {
|
|
10
|
+
constructor(_config, _node) {
|
|
11
|
+
this._config = _config;
|
|
12
|
+
this._node = _node;
|
|
13
|
+
/** document the constructor */
|
|
14
|
+
this._comment = '';
|
|
15
|
+
/** a list of decorators to copy paste to the generator */
|
|
16
|
+
this._decorators = [];
|
|
17
|
+
/** a list of interfaces to implements */
|
|
18
|
+
// _implements: string[] = [];
|
|
19
|
+
/** a list of class to mixin with */
|
|
20
|
+
// _mixins: string[] = [];
|
|
21
|
+
/** the parameters of this factory constructor */
|
|
22
|
+
// TODO: handle other parameter types like positional parameters later.
|
|
23
|
+
// TODO: sticking to named parameters because GraphQL is a typed language
|
|
24
|
+
this._parameters = [];
|
|
25
|
+
this._config = _config;
|
|
26
|
+
this._node = _node;
|
|
27
|
+
this._freezedConfigValue = new utils_js_1.FreezedConfigValue(_config, _node.name.value);
|
|
28
|
+
}
|
|
29
|
+
init() {
|
|
30
|
+
/*
|
|
31
|
+
setDecorators(), setName() and setType() will be called
|
|
32
|
+
when the factory is retrieved from the repository
|
|
33
|
+
*/
|
|
34
|
+
this.setComment().setParameters().setShape().setBlock();
|
|
35
|
+
return this;
|
|
36
|
+
}
|
|
37
|
+
setComment() {
|
|
38
|
+
var _a;
|
|
39
|
+
const comment = (_a = this._node.description) === null || _a === void 0 ? void 0 : _a.value;
|
|
40
|
+
if (comment && comment !== null && comment !== '') {
|
|
41
|
+
this._comment = (0, visitor_plugin_common_1.indent)(`/// ${comment} \n`);
|
|
42
|
+
}
|
|
43
|
+
return this;
|
|
44
|
+
}
|
|
45
|
+
setDecorators(appliesOn, nodeName) {
|
|
46
|
+
this._decorators = [
|
|
47
|
+
...(0, utils_js_1.transformCustomDecorators)((0, utils_js_1.getCustomDecorators)(this._config, appliesOn.split(','), nodeName), this._node),
|
|
48
|
+
];
|
|
49
|
+
return this;
|
|
50
|
+
}
|
|
51
|
+
setKey(key) {
|
|
52
|
+
this._key = (0, change_case_all_1.pascalCase)(key);
|
|
53
|
+
return this;
|
|
54
|
+
}
|
|
55
|
+
setName(name) {
|
|
56
|
+
this._name = (0, change_case_all_1.pascalCase)(name);
|
|
57
|
+
return this;
|
|
58
|
+
}
|
|
59
|
+
setNamedConstructor(namedConstructor) {
|
|
60
|
+
if (namedConstructor) {
|
|
61
|
+
this._namedConstructor = (0, change_case_all_1.camelCase)(namedConstructor);
|
|
62
|
+
}
|
|
63
|
+
return this;
|
|
64
|
+
}
|
|
65
|
+
setParameters() {
|
|
66
|
+
var _a, _b, _c;
|
|
67
|
+
const appliesOn = this._namedConstructor
|
|
68
|
+
? ['union_factory_parameter']
|
|
69
|
+
: ['class_factory_parameter'];
|
|
70
|
+
if (this._node.kind !== graphql_1.Kind.UNION_TYPE_DEFINITION && this._node.kind !== graphql_1.Kind.ENUM_TYPE_DEFINITION) {
|
|
71
|
+
this._parameters =
|
|
72
|
+
(_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_js_1.FreezedParameterBlock(this._config, appliesOn, this._node, field).init())) !== null && _c !== void 0 ? _c : [];
|
|
73
|
+
}
|
|
74
|
+
return this;
|
|
75
|
+
}
|
|
76
|
+
setShape() {
|
|
77
|
+
this._shape = this._parameters.map(p => p.toString()).join('');
|
|
78
|
+
return this;
|
|
79
|
+
}
|
|
80
|
+
setBlock() {
|
|
81
|
+
let block = '';
|
|
82
|
+
//append comment
|
|
83
|
+
block += this._comment;
|
|
84
|
+
// append the decorators
|
|
85
|
+
block += this._decorators.map(d => (0, visitor_plugin_common_1.indent)(`${d}\n`)).join('');
|
|
86
|
+
block += (0, visitor_plugin_common_1.indent)('');
|
|
87
|
+
// decide if to use const or not
|
|
88
|
+
if (this._freezedConfigValue.get('immutable')) {
|
|
89
|
+
block += 'const ';
|
|
90
|
+
}
|
|
91
|
+
// append the factory keyword and the name
|
|
92
|
+
block += `factory ${this._name}`;
|
|
93
|
+
// append .namedConstructor is not null
|
|
94
|
+
if (this._namedConstructor && this._namedConstructor !== '') {
|
|
95
|
+
block += `.${this._namedConstructor}`;
|
|
96
|
+
}
|
|
97
|
+
// append the parenthesis for the constructor and braces for the named parameters
|
|
98
|
+
block += '({\n';
|
|
99
|
+
//append the shape
|
|
100
|
+
block += this._shape;
|
|
101
|
+
// close the constructor and assign the key
|
|
102
|
+
block += (0, visitor_plugin_common_1.indent)(`}) = `);
|
|
103
|
+
// but first decide whether prefix the key with an underscore
|
|
104
|
+
if (!this._namedConstructor) {
|
|
105
|
+
block += '_';
|
|
106
|
+
}
|
|
107
|
+
// finally, append the key
|
|
108
|
+
block += `${this._key};\n`;
|
|
109
|
+
// store it in the shape
|
|
110
|
+
this._block = block;
|
|
111
|
+
return this;
|
|
112
|
+
}
|
|
113
|
+
/** returns the block */
|
|
114
|
+
toString() {
|
|
115
|
+
if (!this._block) {
|
|
116
|
+
throw new Error('FreezedFactoryBlock: setShape must be called before calling toString()');
|
|
117
|
+
}
|
|
118
|
+
return this._block;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
exports.FreezedFactoryBlock = FreezedFactoryBlock;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const tslib_1 = require("tslib");
|
|
4
|
+
tslib_1.__exportStar(require("./parameter-block.js"), exports);
|
|
5
|
+
tslib_1.__exportStar(require("./factory-block.js"), exports);
|
|
6
|
+
tslib_1.__exportStar(require("./class-block.js"), exports);
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.FreezedParameterBlock = exports.DART_SCALARS = void 0;
|
|
4
|
+
const visitor_plugin_common_1 = require("@graphql-codegen/visitor-plugin-common");
|
|
5
|
+
const change_case_all_1 = require("change-case-all");
|
|
6
|
+
const utils_js_1 = require("../utils.js");
|
|
7
|
+
/**
|
|
8
|
+
* maps GraphQL scalar types to Dart's scalar types
|
|
9
|
+
*/
|
|
10
|
+
exports.DART_SCALARS = {
|
|
11
|
+
ID: 'String',
|
|
12
|
+
String: 'String',
|
|
13
|
+
Boolean: 'bool',
|
|
14
|
+
Int: 'int',
|
|
15
|
+
Float: 'double',
|
|
16
|
+
DateTime: 'DateTime',
|
|
17
|
+
};
|
|
18
|
+
class FreezedParameterBlock {
|
|
19
|
+
constructor(_config, _appliesOn, _node, _field) {
|
|
20
|
+
this._config = _config;
|
|
21
|
+
this._appliesOn = _appliesOn;
|
|
22
|
+
this._node = _node;
|
|
23
|
+
this._field = _field;
|
|
24
|
+
/** document the property */
|
|
25
|
+
this._comment = '';
|
|
26
|
+
/** a list of decorators to copy paste to the generator */
|
|
27
|
+
this._decorators = [];
|
|
28
|
+
this.propertyType = (field, type, parentType) => {
|
|
29
|
+
if (this.isNonNullType(type)) {
|
|
30
|
+
return this.propertyType(field, type.type, type);
|
|
31
|
+
}
|
|
32
|
+
if (this.isListType(type)) {
|
|
33
|
+
const T = this.propertyType(field, type.type, type);
|
|
34
|
+
return `List<${T}>${this.isNonNullType(parentType) ? '' : '?'}`;
|
|
35
|
+
}
|
|
36
|
+
if (this.isNamedType(type)) {
|
|
37
|
+
return `${this.scalar(type.name.value)}${this.isNonNullType(parentType) ? '' : '?'}`;
|
|
38
|
+
}
|
|
39
|
+
return '';
|
|
40
|
+
};
|
|
41
|
+
this.isListType = (type) => (type === null || type === void 0 ? void 0 : type.kind) === 'ListType';
|
|
42
|
+
this.isNonNullType = (type) => (type === null || type === void 0 ? void 0 : type.kind) === 'NonNullType';
|
|
43
|
+
this.isNamedType = (type) => (type === null || type === void 0 ? void 0 : type.kind) === 'NamedType';
|
|
44
|
+
this._config = _config;
|
|
45
|
+
this._appliesOn = _appliesOn;
|
|
46
|
+
this._node = _node;
|
|
47
|
+
this._field = _field;
|
|
48
|
+
this._freezedConfigValue = new utils_js_1.FreezedConfigValue(_config, _node.name.value);
|
|
49
|
+
}
|
|
50
|
+
init() {
|
|
51
|
+
this.setComment().setDecorators().setRequired().setType().setName().setShape().setBlock();
|
|
52
|
+
return this;
|
|
53
|
+
}
|
|
54
|
+
setComment() {
|
|
55
|
+
var _a;
|
|
56
|
+
const comment = (_a = this._field.description) === null || _a === void 0 ? void 0 : _a.value;
|
|
57
|
+
if (comment && comment !== null && comment !== '') {
|
|
58
|
+
this._comment = (0, visitor_plugin_common_1.indent)(`/// ${comment}\n`, 2);
|
|
59
|
+
}
|
|
60
|
+
return this;
|
|
61
|
+
}
|
|
62
|
+
setDecorators() {
|
|
63
|
+
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
64
|
+
const nodeName = this._node.name.value;
|
|
65
|
+
const fieldName = this._field.name.value;
|
|
66
|
+
// determine if should mark as deprecated
|
|
67
|
+
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;
|
|
68
|
+
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;
|
|
69
|
+
if (this._freezedConfigValue.get('alwaysUseJsonKeyName') || fieldName !== (0, change_case_all_1.camelCase)(fieldName)) {
|
|
70
|
+
this._decorators = [...this._decorators, `@JsonKey(name: '${fieldName}')`];
|
|
71
|
+
}
|
|
72
|
+
this._decorators = [
|
|
73
|
+
...this._decorators,
|
|
74
|
+
...(0, utils_js_1.transformCustomDecorators)((0, utils_js_1.getCustomDecorators)(this._config, this._appliesOn, this._node.name.value, fieldName), this._node, this._field),
|
|
75
|
+
];
|
|
76
|
+
// @deprecated
|
|
77
|
+
// if this._decorators doesn't include an @deprecated decorator but the field is marked as @deprecated...
|
|
78
|
+
if (!this._decorators.includes('@deprecated') && isDeprecated) {
|
|
79
|
+
this._decorators = [...this._decorators, '@deprecated'];
|
|
80
|
+
}
|
|
81
|
+
// @Default
|
|
82
|
+
if (defaultValue) {
|
|
83
|
+
//overwrite the customDecorator's defaultValue
|
|
84
|
+
this._decorators = this._decorators.filter(d => !d.startsWith('@Default'));
|
|
85
|
+
this._decorators = [...this._decorators, `@Default(value: ${defaultValue})`];
|
|
86
|
+
}
|
|
87
|
+
return this;
|
|
88
|
+
}
|
|
89
|
+
setRequired() {
|
|
90
|
+
this._required = this.isNonNullType(this._field.type);
|
|
91
|
+
return this;
|
|
92
|
+
}
|
|
93
|
+
setType() {
|
|
94
|
+
this._type = this.propertyType(this._field, this._field.type);
|
|
95
|
+
return this;
|
|
96
|
+
}
|
|
97
|
+
setName() {
|
|
98
|
+
this._name = (0, change_case_all_1.camelCase)(this._field.name.value);
|
|
99
|
+
return this;
|
|
100
|
+
}
|
|
101
|
+
/** compose the freezed constructor property */
|
|
102
|
+
setShape() {
|
|
103
|
+
var _a, _b, _c, _d;
|
|
104
|
+
let shape = '';
|
|
105
|
+
const nodeName = this._node.name.value;
|
|
106
|
+
const fieldName = this._field.name.value;
|
|
107
|
+
// determine if should mark as final
|
|
108
|
+
const isFinal = this._decorators.includes('final') ||
|
|
109
|
+
((_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);
|
|
110
|
+
//append comment
|
|
111
|
+
shape += this._comment;
|
|
112
|
+
// append the decorators
|
|
113
|
+
shape += this._decorators
|
|
114
|
+
.filter(d => d !== 'final')
|
|
115
|
+
.map(d => (0, visitor_plugin_common_1.indent)(`${d}\n`, 2))
|
|
116
|
+
.join('');
|
|
117
|
+
// append required for non-nullable types
|
|
118
|
+
shape += (0, visitor_plugin_common_1.indent)(this._required ? 'required ' : '', 2);
|
|
119
|
+
// append isFinal
|
|
120
|
+
shape += isFinal ? 'final ' : '';
|
|
121
|
+
// append the Dart Type, name and trailing comma
|
|
122
|
+
shape += `${this._type} ${this._name},\n`;
|
|
123
|
+
// store it in the shape
|
|
124
|
+
this._shape = shape;
|
|
125
|
+
return this;
|
|
126
|
+
}
|
|
127
|
+
/** composes the full block */
|
|
128
|
+
setBlock() {
|
|
129
|
+
this._block = this._shape;
|
|
130
|
+
return this;
|
|
131
|
+
}
|
|
132
|
+
scalar(_scalar) {
|
|
133
|
+
var _a, _b;
|
|
134
|
+
if ((_b = (_a = this._config) === null || _a === void 0 ? void 0 : _a.customScalars) === null || _b === void 0 ? void 0 : _b[_scalar]) {
|
|
135
|
+
return this._config.customScalars[_scalar];
|
|
136
|
+
}
|
|
137
|
+
if (exports.DART_SCALARS[_scalar]) {
|
|
138
|
+
return exports.DART_SCALARS[_scalar];
|
|
139
|
+
}
|
|
140
|
+
return _scalar;
|
|
141
|
+
}
|
|
142
|
+
/** returns the block */
|
|
143
|
+
toString() {
|
|
144
|
+
if (!this._block) {
|
|
145
|
+
throw new Error('FreezedParameterBlock: setShape must be called before calling toString()');
|
|
146
|
+
}
|
|
147
|
+
return this._block;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
exports.FreezedParameterBlock = FreezedParameterBlock;
|
package/cjs/index.js
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.plugin = void 0;
|
|
4
|
+
const plugin_helpers_1 = require("@graphql-codegen/plugin-helpers");
|
|
5
|
+
const schema_ast_1 = require("@graphql-codegen/schema-ast");
|
|
6
|
+
const index_js_1 = require("./freezed-declaration-blocks/index.js");
|
|
7
|
+
const schema_visitor_js_1 = require("./schema-visitor.js");
|
|
8
|
+
const utils_js_1 = require("./utils.js");
|
|
9
|
+
const plugin = (schema, _documents, config) => {
|
|
10
|
+
// sets the defaults for the config
|
|
11
|
+
config = { ...new utils_js_1.DefaultFreezedPluginConfig(config) };
|
|
12
|
+
const { schema: _schema, ast } = (0, schema_ast_1.transformSchemaAST)(schema, config);
|
|
13
|
+
const { freezedFactoryBlockRepository, ...visitor } = (0, schema_visitor_js_1.schemaVisitor)(_schema, config);
|
|
14
|
+
const visitorResult = (0, plugin_helpers_1.oldVisit)(ast, { leave: visitor });
|
|
15
|
+
const generated = visitorResult.definitions.filter((def) => def instanceof index_js_1.FreezedDeclarationBlock);
|
|
16
|
+
return ((0, utils_js_1.addFreezedImportStatements)(config.fileName) +
|
|
17
|
+
generated
|
|
18
|
+
.map(freezedDeclarationBlock => freezedDeclarationBlock.toString().replace(/==>factory==>.+/gm, s => {
|
|
19
|
+
const pattern = s.replace('==>factory==>', '').trim();
|
|
20
|
+
const [key, appliesOn, name, typeName] = pattern.split('==>');
|
|
21
|
+
return freezedFactoryBlockRepository.retrieve(key, appliesOn, name, typeName !== null && typeName !== void 0 ? typeName : null).toString();
|
|
22
|
+
}))
|
|
23
|
+
.join('')
|
|
24
|
+
.trim() +
|
|
25
|
+
'\n');
|
|
26
|
+
};
|
|
27
|
+
exports.plugin = plugin;
|
package/cjs/package.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"type":"commonjs"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.schemaVisitor = void 0;
|
|
4
|
+
const utils_js_1 = require("./utils.js");
|
|
5
|
+
const schemaVisitor = (_schema, config) => {
|
|
6
|
+
const freezedFactoryBlockRepository = new utils_js_1.FreezedFactoryBlockRepository();
|
|
7
|
+
return {
|
|
8
|
+
freezedFactoryBlockRepository,
|
|
9
|
+
EnumTypeDefinition: (node) => (0, utils_js_1.transformDefinition)(config, freezedFactoryBlockRepository, node),
|
|
10
|
+
UnionTypeDefinition: (node) => (0, utils_js_1.transformDefinition)(config, freezedFactoryBlockRepository, node),
|
|
11
|
+
ObjectTypeDefinition: (node) => (0, utils_js_1.transformDefinition)(config, freezedFactoryBlockRepository, node),
|
|
12
|
+
InputObjectTypeDefinition: (node) => (0, utils_js_1.transformDefinition)(config, freezedFactoryBlockRepository, node),
|
|
13
|
+
};
|
|
14
|
+
};
|
|
15
|
+
exports.schemaVisitor = schemaVisitor;
|