@graphql-codegen/flutter-freezed 2.11.2 → 3.0.0
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/config-value.js +132 -0
- package/cjs/config/pattern.js +731 -0
- package/cjs/config/plugin-config.js +140 -0
- package/cjs/freezed-declaration-blocks/class-block.js +87 -224
- package/cjs/freezed-declaration-blocks/enum-block.js +54 -0
- package/cjs/freezed-declaration-blocks/factory-block.js +81 -113
- package/cjs/freezed-declaration-blocks/index.js +91 -4
- package/cjs/freezed-declaration-blocks/node-repository.js +26 -0
- package/cjs/freezed-declaration-blocks/parameter-block.js +55 -143
- package/cjs/index.js +10 -14
- package/cjs/schema-visitor.js +8 -7
- package/cjs/utils.js +64 -219
- package/esm/config/config-value.js +128 -0
- package/esm/config/pattern.js +723 -0
- package/esm/config/plugin-config.js +137 -0
- package/esm/freezed-declaration-blocks/class-block.js +86 -223
- package/esm/freezed-declaration-blocks/enum-block.js +50 -0
- package/esm/freezed-declaration-blocks/factory-block.js +81 -113
- package/esm/freezed-declaration-blocks/index.js +89 -3
- package/esm/freezed-declaration-blocks/node-repository.js +22 -0
- package/esm/freezed-declaration-blocks/parameter-block.js +53 -141
- package/esm/index.js +11 -15
- package/esm/schema-visitor.js +8 -7
- package/esm/utils.js +56 -212
- package/package.json +5 -5
- package/typings/config/config-value.d.cts +24 -0
- package/typings/config/config-value.d.ts +24 -0
- package/typings/config/pattern.d.cts +252 -0
- package/typings/config/pattern.d.ts +252 -0
- package/typings/config/plugin-config.d.cts +683 -0
- package/typings/config/plugin-config.d.ts +683 -0
- package/typings/freezed-declaration-blocks/class-block.d.cts +11 -35
- package/typings/freezed-declaration-blocks/class-block.d.ts +11 -35
- package/typings/freezed-declaration-blocks/enum-block.d.cts +10 -0
- package/typings/freezed-declaration-blocks/enum-block.d.ts +10 -0
- package/typings/freezed-declaration-blocks/factory-block.d.cts +16 -37
- package/typings/freezed-declaration-blocks/factory-block.d.ts +16 -37
- package/typings/freezed-declaration-blocks/index.d.cts +27 -3
- package/typings/freezed-declaration-blocks/index.d.ts +27 -3
- package/typings/freezed-declaration-blocks/node-repository.d.cts +10 -0
- package/typings/freezed-declaration-blocks/node-repository.d.ts +10 -0
- package/typings/freezed-declaration-blocks/parameter-block.d.cts +11 -44
- package/typings/freezed-declaration-blocks/parameter-block.d.ts +11 -44
- package/typings/index.d.cts +1 -1
- package/typings/index.d.ts +1 -1
- package/typings/schema-visitor.d.cts +7 -7
- package/typings/schema-visitor.d.ts +7 -7
- package/typings/utils.d.cts +33 -72
- package/typings/utils.d.ts +33 -72
- package/cjs/config.js +0 -2
- package/esm/config.js +0 -1
- package/typings/config.d.cts +0 -479
- package/typings/config.d.ts +0 -479
|
@@ -1,121 +1,89 @@
|
|
|
1
1
|
import { indent } from '@graphql-codegen/visitor-plugin-common';
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
this.
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
/** the parameters of this factory constructor */
|
|
19
|
-
// TODO: handle other parameter types like positional parameters later.
|
|
20
|
-
// TODO: sticking to named parameters because GraphQL is a typed language
|
|
21
|
-
this._parameters = [];
|
|
22
|
-
this._config = _config;
|
|
23
|
-
this._node = _node;
|
|
24
|
-
this._freezedConfigValue = new FreezedConfigValue(_config, _node.name.value);
|
|
25
|
-
}
|
|
26
|
-
init() {
|
|
27
|
-
/*
|
|
28
|
-
setDecorators(), setName() and setType() will be called
|
|
29
|
-
when the factory is retrieved from the repository
|
|
30
|
-
*/
|
|
31
|
-
this.setComment().setParameters().setShape().setBlock();
|
|
32
|
-
return this;
|
|
33
|
-
}
|
|
34
|
-
setComment() {
|
|
35
|
-
var _a;
|
|
36
|
-
const comment = (_a = this._node.description) === null || _a === void 0 ? void 0 : _a.value;
|
|
37
|
-
if (comment && comment !== null && comment !== '') {
|
|
38
|
-
this._comment = indent(`/// ${comment} \n`);
|
|
39
|
-
}
|
|
40
|
-
return this;
|
|
41
|
-
}
|
|
42
|
-
setDecorators(appliesOn, nodeName) {
|
|
43
|
-
this._decorators = [
|
|
44
|
-
...transformCustomDecorators(getCustomDecorators(this._config, appliesOn.split(','), nodeName), this._node),
|
|
45
|
-
];
|
|
46
|
-
return this;
|
|
47
|
-
}
|
|
48
|
-
setKey(key) {
|
|
49
|
-
this._key = pascalCase(key);
|
|
50
|
-
return this;
|
|
2
|
+
import { Config } from '../config/config-value.js';
|
|
3
|
+
import { TypeName } from '../config/pattern.js';
|
|
4
|
+
import { APPLIES_ON_DEFAULT_FACTORY, APPLIES_ON_UNION_FACTORY, APPLIES_ON_MERGED_FACTORY, APPLIES_ON_DEFAULT_FACTORY_PARAMETERS, APPLIES_ON_UNION_FACTORY_PARAMETERS, APPLIES_ON_MERGED_FACTORY_PARAMETERS, } from '../config/plugin-config.js';
|
|
5
|
+
import { Block } from './index.js';
|
|
6
|
+
import { ParameterBlock } from './parameter-block.js';
|
|
7
|
+
import { stringIsNotEmpty } from '../utils.js';
|
|
8
|
+
export class FactoryBlock {
|
|
9
|
+
static build(config, node, blockAppliesOn, className, factoryName) {
|
|
10
|
+
let block = '';
|
|
11
|
+
block += Block.buildComment(node);
|
|
12
|
+
block += this.buildDecorators(config, blockAppliesOn, className, factoryName);
|
|
13
|
+
block += this.buildHeader(config, blockAppliesOn, className, factoryName);
|
|
14
|
+
block += this.buildBody(config, node, blockAppliesOn);
|
|
15
|
+
factoryName = blockAppliesOn.includes('default_factory') ? className : factoryName;
|
|
16
|
+
block += this.buildFooter(config, blockAppliesOn, factoryName);
|
|
17
|
+
return block;
|
|
51
18
|
}
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
19
|
+
}
|
|
20
|
+
FactoryBlock.buildDecorators = (config, blockAppliesOn, className, factoryName) => {
|
|
21
|
+
// TODO: @Assert
|
|
22
|
+
const typeName = factoryName ? TypeName.fromUnionOfTypeNames(className, factoryName) : className;
|
|
23
|
+
const deprecatedDecorator = Config.deprecated(config, blockAppliesOn, typeName);
|
|
24
|
+
const decorators = [deprecatedDecorator].join('');
|
|
25
|
+
return stringIsNotEmpty(decorators) ? indent(decorators) : decorators;
|
|
26
|
+
};
|
|
27
|
+
FactoryBlock.buildHeader = (config, blockAppliesOn, className, factoryName) => {
|
|
28
|
+
const typeName = factoryName ? TypeName.fromUnionOfTypeNames(className, factoryName) : className;
|
|
29
|
+
const immutable = Config.immutable(config, typeName);
|
|
30
|
+
// const mutableInputs = Config.mutableInputs(config, factoryName);
|
|
31
|
+
// const mutable = immutable !== true || (node.kind === Kind.INPUT_OBJECT_TYPE_DEFINITION && mutableInputs);
|
|
32
|
+
const constFactory = immutable ? indent('const factory') : indent('factory');
|
|
33
|
+
const _className = Block.buildBlockName(config, blockAppliesOn, className.value, className, undefined, 'PascalCase');
|
|
34
|
+
if (factoryName) {
|
|
35
|
+
const _factoryName = Block.buildBlockName(config, blockAppliesOn, factoryName.value, factoryName, undefined, 'camelCase');
|
|
36
|
+
return `${constFactory} ${_className}.${_factoryName}({\n`;
|
|
55
37
|
}
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
38
|
+
return `${constFactory} ${_className}({\n`;
|
|
39
|
+
};
|
|
40
|
+
FactoryBlock.buildBody = (config, node, appliesOn) => {
|
|
41
|
+
var _a, _b;
|
|
42
|
+
let appliesOnParameters = [];
|
|
43
|
+
if (appliesOn.includes('default_factory')) {
|
|
44
|
+
appliesOnParameters = APPLIES_ON_DEFAULT_FACTORY_PARAMETERS;
|
|
61
45
|
}
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
// TODO: get this from config directly
|
|
65
|
-
const mergeInputs = this._freezedConfigValue.get('mergeInputs');
|
|
66
|
-
const appliesOn = this._namedConstructor
|
|
67
|
-
? ['union_factory_parameter']
|
|
68
|
-
: (mergeInputs !== null && mergeInputs !== void 0 ? mergeInputs : mergeInputs !== [])
|
|
69
|
-
? ['merged_input_parameter']
|
|
70
|
-
: ['class_factory_parameter'];
|
|
71
|
-
if (this._node.kind !== Kind.UNION_TYPE_DEFINITION && this._node.kind !== Kind.ENUM_TYPE_DEFINITION) {
|
|
72
|
-
this._parameters =
|
|
73
|
-
(_c = (_b = (_a = this._node) === null || _a === void 0 ? void 0 : _a.fields) === null || _b === void 0 ? void 0 : _b.map((field) => new FreezedParameterBlock(this._config, appliesOn, this._node, field).init())) !== null && _c !== void 0 ? _c : [];
|
|
74
|
-
}
|
|
75
|
-
return this;
|
|
46
|
+
else if (appliesOn.includes('union_factory')) {
|
|
47
|
+
appliesOnParameters = APPLIES_ON_UNION_FACTORY_PARAMETERS;
|
|
76
48
|
}
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
return this;
|
|
49
|
+
else if (appliesOn.includes('merged_factory')) {
|
|
50
|
+
appliesOnParameters = APPLIES_ON_MERGED_FACTORY_PARAMETERS;
|
|
80
51
|
}
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
block += indent(`}) = `);
|
|
104
|
-
// but first decide whether prefix the key with an underscore
|
|
105
|
-
if (!this._namedConstructor) {
|
|
106
|
-
block += '_';
|
|
107
|
-
}
|
|
108
|
-
// finally, append the key
|
|
109
|
-
block += `${this._key};\n`;
|
|
110
|
-
// store it in the shape
|
|
111
|
-
this._block = block;
|
|
112
|
-
return this;
|
|
52
|
+
return ((_b = (_a = node.fields) === null || _a === void 0 ? void 0 : _a.map((field) => {
|
|
53
|
+
return ParameterBlock.build(config, node, field, appliesOnParameters);
|
|
54
|
+
}).join('')) !== null && _b !== void 0 ? _b : '');
|
|
55
|
+
};
|
|
56
|
+
FactoryBlock.buildFooter = (config, blockAppliesOn, factoryName) => {
|
|
57
|
+
const _ = blockAppliesOn.includes('default_factory') ? '_' : '';
|
|
58
|
+
const _factoryName = Block.buildBlockName(config, blockAppliesOn, factoryName.value, factoryName, undefined, 'PascalCase');
|
|
59
|
+
return indent(`}) = ${_}${_factoryName};\n\n`);
|
|
60
|
+
};
|
|
61
|
+
FactoryBlock.serializeDefaultFactory = (className) => {
|
|
62
|
+
return `${Block.tokens.defaultFactory}${className.value}==>${APPLIES_ON_DEFAULT_FACTORY.join(',')}\n`;
|
|
63
|
+
};
|
|
64
|
+
FactoryBlock.serializeUnionFactory = (className, factoryName) => {
|
|
65
|
+
return `${Block.tokens.unionFactory}${className.value}==>${factoryName.value}==>${APPLIES_ON_UNION_FACTORY.join(',')}\n`;
|
|
66
|
+
};
|
|
67
|
+
FactoryBlock.serializeMergedFactory = (className, factoryName) => {
|
|
68
|
+
return `${Block.tokens.mergedFactory}${className.value}==>${factoryName.value}==>${APPLIES_ON_MERGED_FACTORY.join(',')}\n`;
|
|
69
|
+
};
|
|
70
|
+
FactoryBlock.deserializeFactory = (config, nodeRepository, blockAppliesOn, className) => {
|
|
71
|
+
const node = nodeRepository.get(className.value);
|
|
72
|
+
if (node) {
|
|
73
|
+
return FactoryBlock.buildFromFactory(config, node, blockAppliesOn, className);
|
|
113
74
|
}
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
return
|
|
75
|
+
return '';
|
|
76
|
+
};
|
|
77
|
+
FactoryBlock.deserializeNamedFactory = (config, nodeRepository, blockAppliesOn, className, factoryName) => {
|
|
78
|
+
const node = nodeRepository.get(factoryName.value);
|
|
79
|
+
if (node) {
|
|
80
|
+
return FactoryBlock.buildFromNamedFactory(config, node, blockAppliesOn, className, factoryName);
|
|
120
81
|
}
|
|
121
|
-
|
|
82
|
+
return '';
|
|
83
|
+
};
|
|
84
|
+
FactoryBlock.buildFromFactory = (config, node, blockAppliesOn, className) => {
|
|
85
|
+
return FactoryBlock.build(config, node, blockAppliesOn, className);
|
|
86
|
+
};
|
|
87
|
+
FactoryBlock.buildFromNamedFactory = (config, node, blockAppliesOn, className, factoryName) => {
|
|
88
|
+
return FactoryBlock.build(config, node, blockAppliesOn, className, factoryName);
|
|
89
|
+
};
|
|
@@ -1,3 +1,89 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
import { snakeCase } from 'change-case-all';
|
|
2
|
+
import { Kind } from 'graphql';
|
|
3
|
+
import { TypeName } from '../config/pattern.js';
|
|
4
|
+
import { Config } from '../config/config-value.js';
|
|
5
|
+
import { dartCasing, escapeDartKeyword, isDartKeyword, nodeIsObjectType } from '../utils.js';
|
|
6
|
+
import { ClassBlock } from './class-block.js';
|
|
7
|
+
import { EnumBlock } from './enum-block.js';
|
|
8
|
+
import { FactoryBlock } from './factory-block.js';
|
|
9
|
+
export class Block {
|
|
10
|
+
}
|
|
11
|
+
Block.buildImportStatements = (fileName) => {
|
|
12
|
+
if (fileName.length < 1) {
|
|
13
|
+
throw new Error('fileName is required and must not be empty');
|
|
14
|
+
}
|
|
15
|
+
const segments = fileName.split('/');
|
|
16
|
+
const target = segments[segments.length - 1];
|
|
17
|
+
const expectedFileName = snakeCase(target.replace(/\.dart/g, ''));
|
|
18
|
+
return [
|
|
19
|
+
`import 'package:freezed_annotation/freezed_annotation.dart';\n`,
|
|
20
|
+
`import 'package:flutter/foundation.dart';\n\n`,
|
|
21
|
+
`part '${expectedFileName}.freezed.dart';\n`,
|
|
22
|
+
`part '${expectedFileName}.g.dart';\n\n`,
|
|
23
|
+
].join('');
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* Transforms the AST nodes into Freezed classes/models
|
|
27
|
+
* @param config The plugin configuration object
|
|
28
|
+
* @param node the AST node passed by the schema visitor
|
|
29
|
+
* @param nodeRepository A map that stores the name of the Graphql Type as the key and it AST node as the value. Used to build FactoryBlocks from placeholders for mergedInputs and Union Types
|
|
30
|
+
* @returns a string output of a `FreezedDeclarationBlock` which represents a Freezed class/model in Dart
|
|
31
|
+
*/
|
|
32
|
+
Block.build = (config, node, nodeRepository) => {
|
|
33
|
+
// ignore these...
|
|
34
|
+
const typeName = TypeName.fromString(node.name.value);
|
|
35
|
+
if (['Query', 'Mutation', 'Subscription', ...Config.ignoreTypes(config, typeName)].includes(typeName.value)) {
|
|
36
|
+
return '';
|
|
37
|
+
}
|
|
38
|
+
// registers all the ObjectTypes
|
|
39
|
+
if (nodeIsObjectType(node)) {
|
|
40
|
+
nodeRepository.register(node);
|
|
41
|
+
}
|
|
42
|
+
return node.kind === Kind.ENUM_TYPE_DEFINITION ? EnumBlock.build(config, node) : ClassBlock.build(config, node);
|
|
43
|
+
};
|
|
44
|
+
Block.buildComment = (node) => {
|
|
45
|
+
var _a;
|
|
46
|
+
const comment = (_a = node === null || node === void 0 ? void 0 : node.description) === null || _a === void 0 ? void 0 : _a.value;
|
|
47
|
+
return comment && (comment === null || comment === void 0 ? void 0 : comment.length) > 0
|
|
48
|
+
? `${comment
|
|
49
|
+
.trim()
|
|
50
|
+
.split(/\n/gm)
|
|
51
|
+
.map(c => `/// ${c.trim().replace(/^#/, '')}\n`)
|
|
52
|
+
.join('')}`
|
|
53
|
+
: '';
|
|
54
|
+
};
|
|
55
|
+
Block.buildBlockName = (config, blockAppliesOn, identifier, typeName, fieldName, blockCasing) => {
|
|
56
|
+
identifier = dartCasing(identifier, blockCasing);
|
|
57
|
+
if (isDartKeyword(identifier)) {
|
|
58
|
+
return escapeDartKeyword(config, blockAppliesOn, identifier, typeName, fieldName);
|
|
59
|
+
}
|
|
60
|
+
return identifier;
|
|
61
|
+
};
|
|
62
|
+
Block.tokens = {
|
|
63
|
+
defaultFactory: '==>default_factory==>',
|
|
64
|
+
unionFactory: '==>union_factory==>',
|
|
65
|
+
mergedFactory: '==>merged_factory==>',
|
|
66
|
+
fromJsonToJson: '==>from_json_to_json==>',
|
|
67
|
+
};
|
|
68
|
+
Block.regexpForToken = (tokenName) => {
|
|
69
|
+
return RegExp(`${Block.tokens[tokenName]}.+\n`, 'gm');
|
|
70
|
+
};
|
|
71
|
+
Block.replaceTokens = (config, nodeRepository, generatedBlocks) => generatedBlocks
|
|
72
|
+
.map(block => {
|
|
73
|
+
block = Block.replaceDefaultFactoryToken(block, config, nodeRepository);
|
|
74
|
+
block = Block.replaceNamedFactoryToken(block, config, nodeRepository, 'unionFactory');
|
|
75
|
+
block = Block.replaceNamedFactoryToken(block, config, nodeRepository, 'mergedFactory');
|
|
76
|
+
// TODO: one more for parameter fromJson and toJson tokens inside @JsonKey
|
|
77
|
+
return block;
|
|
78
|
+
})
|
|
79
|
+
.join('');
|
|
80
|
+
Block.replaceDefaultFactoryToken = (block, config, nodeRepository) => block.replace(Block.regexpForToken('defaultFactory'), token => {
|
|
81
|
+
const pattern = token.replace(Block.tokens.defaultFactory, '').trim();
|
|
82
|
+
const [className, blockAppliesOn] = pattern.split('==>');
|
|
83
|
+
return FactoryBlock.deserializeFactory(config, nodeRepository, blockAppliesOn.split(','), TypeName.fromString(className));
|
|
84
|
+
});
|
|
85
|
+
Block.replaceNamedFactoryToken = (block, config, nodeRepository, blockType) => block.replace(Block.regexpForToken(blockType), token => {
|
|
86
|
+
const pattern = token.replace(Block.tokens[blockType], '').trim();
|
|
87
|
+
const [className, factoryName, blockAppliesOn] = pattern.split('==>');
|
|
88
|
+
return FactoryBlock.deserializeNamedFactory(config, nodeRepository, blockAppliesOn.split(','), TypeName.fromString(className), TypeName.fromString(factoryName));
|
|
89
|
+
});
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
//#region NodeRepository classes
|
|
2
|
+
import { nodeIsObjectType } from '../utils.js';
|
|
3
|
+
/**
|
|
4
|
+
* stores an instance of `ObjectTypeDefinitionNode` or `InputObjectTypeDefinitionNode` using the node name as the key
|
|
5
|
+
* and returns that node when replacing placeholders
|
|
6
|
+
* */
|
|
7
|
+
export class NodeRepository {
|
|
8
|
+
constructor() {
|
|
9
|
+
this._store = {};
|
|
10
|
+
}
|
|
11
|
+
get(key) {
|
|
12
|
+
return this._store[key];
|
|
13
|
+
}
|
|
14
|
+
register(node) {
|
|
15
|
+
if (!nodeIsObjectType(node)) {
|
|
16
|
+
throw new Error('Node is not an ObjectTypeDefinitionNode or InputObjectTypeDefinitionNode');
|
|
17
|
+
}
|
|
18
|
+
this._store[node.name.value] = node;
|
|
19
|
+
return node;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
//#endregion
|
|
@@ -1,146 +1,58 @@
|
|
|
1
|
+
var _a;
|
|
2
|
+
import { atJsonKeyDecorator, stringIsNotEmpty } from '../utils.js';
|
|
3
|
+
import { Config } from '../config/config-value.js';
|
|
4
|
+
import { FieldName, TypeName } from '../config/pattern.js';
|
|
5
|
+
import { Block } from './index.js';
|
|
1
6
|
import { indent } from '@graphql-codegen/visitor-plugin-common';
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
DateTime: 'DateTime',
|
|
14
|
-
};
|
|
15
|
-
export class FreezedParameterBlock {
|
|
16
|
-
constructor(_config, _appliesOn, _node, _field) {
|
|
17
|
-
this._config = _config;
|
|
18
|
-
this._appliesOn = _appliesOn;
|
|
19
|
-
this._node = _node;
|
|
20
|
-
this._field = _field;
|
|
21
|
-
/** document the property */
|
|
22
|
-
this._comment = '';
|
|
23
|
-
/** a list of decorators to copy paste to the generator */
|
|
24
|
-
this._decorators = [];
|
|
25
|
-
this.propertyType = (field, type, parentType) => {
|
|
26
|
-
if (this.isNonNullType(type)) {
|
|
27
|
-
return this.propertyType(field, type.type, type);
|
|
28
|
-
}
|
|
29
|
-
if (this.isListType(type)) {
|
|
30
|
-
const T = this.propertyType(field, type.type, type);
|
|
31
|
-
return `List<${T}>${this.isNonNullType(parentType) ? '' : '?'}`;
|
|
32
|
-
}
|
|
33
|
-
if (this.isNamedType(type)) {
|
|
34
|
-
return `${this.scalar(type.name.value)}${this.isNonNullType(parentType) ? '' : '?'}`;
|
|
35
|
-
}
|
|
36
|
-
return '';
|
|
37
|
-
};
|
|
38
|
-
this.isListType = (type) => (type === null || type === void 0 ? void 0 : type.kind) === 'ListType';
|
|
39
|
-
this.isNonNullType = (type) => (type === null || type === void 0 ? void 0 : type.kind) === 'NonNullType';
|
|
40
|
-
this.isNamedType = (type) => (type === null || type === void 0 ? void 0 : type.kind) === 'NamedType';
|
|
41
|
-
this._config = _config;
|
|
42
|
-
this._appliesOn = _appliesOn;
|
|
43
|
-
this._node = _node;
|
|
44
|
-
this._field = _field;
|
|
45
|
-
this._freezedConfigValue = new FreezedConfigValue(_config, _node.name.value);
|
|
46
|
-
}
|
|
47
|
-
init() {
|
|
48
|
-
this.setComment().setDecorators().setRequired().setType().setName().setShape().setBlock();
|
|
49
|
-
return this;
|
|
50
|
-
}
|
|
51
|
-
setComment() {
|
|
52
|
-
var _a;
|
|
53
|
-
const comment = (_a = this._field.description) === null || _a === void 0 ? void 0 : _a.value;
|
|
54
|
-
if (comment && comment !== null && comment !== '') {
|
|
55
|
-
this._comment = indent(`/// ${comment}\n`, 2);
|
|
56
|
-
}
|
|
57
|
-
return this;
|
|
58
|
-
}
|
|
59
|
-
setDecorators() {
|
|
60
|
-
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
61
|
-
const nodeName = this._node.name.value;
|
|
62
|
-
const fieldName = this._field.name.value;
|
|
63
|
-
// determine if should mark as deprecated
|
|
64
|
-
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;
|
|
65
|
-
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;
|
|
66
|
-
if (this._freezedConfigValue.get('alwaysUseJsonKeyName') || fieldName !== camelCase(fieldName)) {
|
|
67
|
-
this._decorators = [...this._decorators, `@JsonKey(name: '${fieldName}')\n`];
|
|
68
|
-
}
|
|
69
|
-
this._decorators = [
|
|
70
|
-
...this._decorators,
|
|
71
|
-
...transformCustomDecorators(getCustomDecorators(this._config, this._appliesOn, this._node.name.value, fieldName), this._node, this._field),
|
|
72
|
-
];
|
|
73
|
-
// @deprecated
|
|
74
|
-
// if this._decorators doesn't include an @deprecated decorator but the field is marked as @deprecated...
|
|
75
|
-
if (!this._decorators.includes('@deprecated') && isDeprecated) {
|
|
76
|
-
this._decorators = [...this._decorators, '@deprecated\n'];
|
|
77
|
-
}
|
|
78
|
-
// @Default
|
|
79
|
-
if (defaultValue) {
|
|
80
|
-
//overwrite the customDecorator's defaultValue
|
|
81
|
-
this._decorators = this._decorators.filter(d => !d.startsWith('@Default'));
|
|
82
|
-
this._decorators = [...this._decorators, `@Default(value: ${defaultValue})\n`];
|
|
83
|
-
}
|
|
84
|
-
return this;
|
|
85
|
-
}
|
|
86
|
-
setRequired() {
|
|
87
|
-
this._required = this.isNonNullType(this._field.type);
|
|
88
|
-
return this;
|
|
89
|
-
}
|
|
90
|
-
setType() {
|
|
91
|
-
this._type = this.propertyType(this._field, this._field.type);
|
|
92
|
-
return this;
|
|
93
|
-
}
|
|
94
|
-
setName() {
|
|
95
|
-
this._name = camelCase(this._field.name.value);
|
|
96
|
-
return this;
|
|
7
|
+
export class ParameterBlock {
|
|
8
|
+
static build(config, node, field, blockAppliesOn) {
|
|
9
|
+
const typeName = TypeName.fromString(node.name.value);
|
|
10
|
+
const fieldName = FieldName.fromString(field.name.value);
|
|
11
|
+
const parameterName = Block.buildBlockName(config, blockAppliesOn, fieldName.value, typeName, fieldName, 'camelCase');
|
|
12
|
+
let block = '';
|
|
13
|
+
block += Block.buildComment(field);
|
|
14
|
+
block += this.buildDecorators(config, typeName, fieldName, parameterName, blockAppliesOn);
|
|
15
|
+
block += this.buildBody(config, field, typeName, fieldName, parameterName, blockAppliesOn);
|
|
16
|
+
// return indentMultiline(block, 2);
|
|
17
|
+
return block;
|
|
97
18
|
}
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
setBlock() {
|
|
126
|
-
this._block = this._shape;
|
|
127
|
-
return this;
|
|
19
|
+
}
|
|
20
|
+
_a = ParameterBlock;
|
|
21
|
+
ParameterBlock.buildDecorators = (config, typeName, fieldName, parameterName, blockAppliesOn) => {
|
|
22
|
+
const deprecatedDecorator = Config.deprecated(config, blockAppliesOn, typeName, fieldName);
|
|
23
|
+
const defaultValueDecorator = Config.defaultValues(config, blockAppliesOn, typeName, fieldName);
|
|
24
|
+
const jsonKeyDecorator = atJsonKeyDecorator({
|
|
25
|
+
name: fieldName.value !== parameterName ? fieldName.value : undefined,
|
|
26
|
+
});
|
|
27
|
+
return [
|
|
28
|
+
deprecatedDecorator,
|
|
29
|
+
defaultValueDecorator,
|
|
30
|
+
jsonKeyDecorator,
|
|
31
|
+
// TODO: add decorator for unionValueName
|
|
32
|
+
]
|
|
33
|
+
.filter(decorator => stringIsNotEmpty(decorator))
|
|
34
|
+
.map(decorator => indent(decorator, 2))
|
|
35
|
+
.join('');
|
|
36
|
+
};
|
|
37
|
+
ParameterBlock.buildBody = (config, field, typeName, fieldName, parameterName, blockAppliesOn) => {
|
|
38
|
+
const required = _a.isNonNullType(field.type) ? 'required ' : '';
|
|
39
|
+
const final = Config.final(config, blockAppliesOn, typeName, fieldName) ? 'final ' : '';
|
|
40
|
+
const dartType = _a.parameterType(config, field.type);
|
|
41
|
+
return indent(`${required}${final}${dartType} ${parameterName},\n`, 2);
|
|
42
|
+
};
|
|
43
|
+
ParameterBlock.parameterType = (config, type, parentType) => {
|
|
44
|
+
if (_a.isNonNullType(type)) {
|
|
45
|
+
return _a.parameterType(config, type.type, type);
|
|
128
46
|
}
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
return this._config.customScalars[_scalar];
|
|
133
|
-
}
|
|
134
|
-
if (DART_SCALARS[_scalar]) {
|
|
135
|
-
return DART_SCALARS[_scalar];
|
|
136
|
-
}
|
|
137
|
-
return _scalar;
|
|
47
|
+
if (_a.isListType(type)) {
|
|
48
|
+
const T = _a.parameterType(config, type.type, type);
|
|
49
|
+
return `List<${T}>${_a.isNonNullType(parentType) ? '' : '?'}`;
|
|
138
50
|
}
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
if (!this._block) {
|
|
142
|
-
throw new Error('FreezedParameterBlock: setShape must be called before calling toString()');
|
|
143
|
-
}
|
|
144
|
-
return this._block;
|
|
51
|
+
if (_a.isNamedType(type)) {
|
|
52
|
+
return `${Config.customScalars(config, type.name.value)}${_a.isNonNullType(parentType) ? '' : '?'}`;
|
|
145
53
|
}
|
|
146
|
-
|
|
54
|
+
return '';
|
|
55
|
+
};
|
|
56
|
+
ParameterBlock.isListType = (type) => (type === null || type === void 0 ? void 0 : type.kind) === 'ListType';
|
|
57
|
+
ParameterBlock.isNonNullType = (type) => (type === null || type === void 0 ? void 0 : type.kind) === 'NonNullType';
|
|
58
|
+
ParameterBlock.isNamedType = (type) => (type === null || type === void 0 ? void 0 : type.kind) === 'NamedType';
|
package/esm/index.js
CHANGED
|
@@ -1,22 +1,18 @@
|
|
|
1
1
|
import { oldVisit } from '@graphql-codegen/plugin-helpers';
|
|
2
2
|
import { transformSchemaAST } from '@graphql-codegen/schema-ast';
|
|
3
|
-
import {
|
|
3
|
+
import { defaultFreezedPluginConfig } from './config/plugin-config.js';
|
|
4
|
+
import { Block } from './freezed-declaration-blocks/index.js';
|
|
4
5
|
import { schemaVisitor } from './schema-visitor.js';
|
|
5
|
-
|
|
6
|
-
|
|
6
|
+
export const plugin = (schema, _documents, _config, info) => {
|
|
7
|
+
var _a;
|
|
7
8
|
// sets the defaults for the config
|
|
8
|
-
config = { ...
|
|
9
|
+
const config = { ...defaultFreezedPluginConfig, ..._config };
|
|
9
10
|
const { schema: _schema, ast } = transformSchemaAST(schema, config);
|
|
10
|
-
const {
|
|
11
|
+
const { nodeRepository, ...visitor } = schemaVisitor(_schema, config);
|
|
11
12
|
const visitorResult = oldVisit(ast, { leave: visitor });
|
|
12
|
-
const
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
const [key, appliesOn, name, typeName] = pattern.split('==>');
|
|
18
|
-
return freezedFactoryBlockRepository.retrieve(key, appliesOn, name, typeName !== null && typeName !== void 0 ? typeName : null);
|
|
19
|
-
}))
|
|
20
|
-
.join('')
|
|
21
|
-
.trim());
|
|
13
|
+
const importStatements = Block.buildImportStatements((_a = info === null || info === void 0 ? void 0 : info.outputFile) !== null && _a !== void 0 ? _a : 'app_models');
|
|
14
|
+
const generatedBlocks = visitorResult.definitions.filter((def) => typeof def === 'string' && def.length > 0);
|
|
15
|
+
// return [importStatements, ...generatedBlocks].join('').trim();
|
|
16
|
+
const output = Block.replaceTokens(config, nodeRepository, generatedBlocks);
|
|
17
|
+
return [importStatements, output].join('').trim();
|
|
22
18
|
};
|
package/esm/schema-visitor.js
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Block } from './freezed-declaration-blocks/index.js';
|
|
2
|
+
import { NodeRepository } from './freezed-declaration-blocks/node-repository.js';
|
|
2
3
|
export const schemaVisitor = (_schema, config) => {
|
|
3
|
-
const
|
|
4
|
+
const nodeRepository = new NodeRepository();
|
|
4
5
|
return {
|
|
5
|
-
|
|
6
|
-
EnumTypeDefinition: (node) =>
|
|
7
|
-
UnionTypeDefinition: (node) =>
|
|
8
|
-
ObjectTypeDefinition: (node) =>
|
|
9
|
-
InputObjectTypeDefinition: (node) =>
|
|
6
|
+
nodeRepository,
|
|
7
|
+
EnumTypeDefinition: (node) => Block.build(config, node, nodeRepository),
|
|
8
|
+
UnionTypeDefinition: (node) => Block.build(config, node, nodeRepository),
|
|
9
|
+
ObjectTypeDefinition: (node) => Block.build(config, node, nodeRepository),
|
|
10
|
+
InputObjectTypeDefinition: (node) => Block.build(config, node, nodeRepository),
|
|
10
11
|
};
|
|
11
12
|
};
|