@graphql-codegen/java-apollo-android 2.3.0-alpha-a52c122aa.0 → 2.3.0-alpha-bd464a586.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/base-java-visitor.js +114 -0
- package/cjs/custom-type-class.js +69 -0
- package/cjs/field-arguments.js +36 -0
- package/cjs/file-type.js +10 -0
- package/cjs/imports.js +45 -0
- package/cjs/index.js +5 -0
- package/cjs/input-type-visitor.js +187 -0
- package/cjs/operation-visitor.js +780 -0
- package/cjs/package.json +1 -0
- package/cjs/plugin.js +48 -0
- package/cjs/preset.js +95 -0
- package/cjs/types.js +2 -0
- package/cjs/visitor-config.js +2 -0
- package/esm/base-java-visitor.js +110 -0
- package/esm/custom-type-class.js +65 -0
- package/esm/field-arguments.js +32 -0
- package/esm/file-type.js +7 -0
- package/esm/imports.js +42 -0
- package/esm/index.js +2 -0
- package/esm/input-type-visitor.js +183 -0
- package/{index.mjs → esm/operation-visitor.js} +38 -620
- package/esm/plugin.js +44 -0
- package/esm/preset.js +92 -0
- package/esm/types.js +1 -0
- package/esm/visitor-config.js +1 -0
- package/package.json +24 -17
- package/{base-java-visitor.d.ts → typings/base-java-visitor.d.ts} +3 -3
- package/{custom-type-class.d.ts → typings/custom-type-class.d.ts} +3 -3
- package/{field-arguments.d.ts → typings/field-arguments.d.ts} +1 -1
- package/{file-type.d.ts → typings/file-type.d.ts} +0 -0
- package/{imports.d.ts → typings/imports.d.ts} +0 -0
- package/typings/index.d.ts +2 -0
- package/{input-type-visitor.d.ts → typings/input-type-visitor.d.ts} +3 -3
- package/{operation-visitor.d.ts → typings/operation-visitor.d.ts} +3 -3
- package/{plugin.d.ts → typings/plugin.d.ts} +4 -4
- package/{preset.d.ts → typings/preset.d.ts} +0 -0
- package/{types.d.ts → typings/types.d.ts} +0 -0
- package/{visitor-config.d.ts → typings/visitor-config.d.ts} +0 -0
- package/index.d.ts +0 -2
- package/index.js +0 -1364
|
@@ -0,0 +1,780 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.OperationVisitor = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const base_java_visitor_js_1 = require("./base-java-visitor.js");
|
|
6
|
+
const visitor_plugin_common_1 = require("@graphql-codegen/visitor-plugin-common");
|
|
7
|
+
const java_common_1 = require("@graphql-codegen/java-common");
|
|
8
|
+
const plugin_helpers_1 = require("@graphql-codegen/plugin-helpers");
|
|
9
|
+
const graphql_1 = require("graphql");
|
|
10
|
+
const imports_js_1 = require("./imports.js");
|
|
11
|
+
const crypto_1 = require("crypto");
|
|
12
|
+
const pluralize_1 = tslib_1.__importDefault(require("pluralize"));
|
|
13
|
+
const field_arguments_js_1 = require("./field-arguments.js");
|
|
14
|
+
const change_case_all_1 = require("change-case-all");
|
|
15
|
+
class OperationVisitor extends base_java_visitor_js_1.BaseJavaVisitor {
|
|
16
|
+
constructor(_schema, rawConfig, _availableFragments) {
|
|
17
|
+
super(_schema, rawConfig, {
|
|
18
|
+
package: rawConfig.package || (0, java_common_1.buildPackageNameFromPath)(process.cwd()),
|
|
19
|
+
fragmentPackage: rawConfig.fragmentPackage || 'fragment',
|
|
20
|
+
typePackage: rawConfig.typePackage || 'type',
|
|
21
|
+
});
|
|
22
|
+
this._availableFragments = _availableFragments;
|
|
23
|
+
this.visitingFragment = false;
|
|
24
|
+
}
|
|
25
|
+
printDocument(node) {
|
|
26
|
+
return (0, graphql_1.print)(node)
|
|
27
|
+
.replace(/\r?\n|\r/g, ' ')
|
|
28
|
+
.replace(/"/g, '\\"')
|
|
29
|
+
.trim();
|
|
30
|
+
}
|
|
31
|
+
getPackage() {
|
|
32
|
+
return this.visitingFragment ? this.config.fragmentPackage : this.config.package;
|
|
33
|
+
}
|
|
34
|
+
addCtor(className, node, cls) {
|
|
35
|
+
const variables = node.variableDefinitions || [];
|
|
36
|
+
const hasVariables = variables.length > 0;
|
|
37
|
+
const nonNullVariables = variables
|
|
38
|
+
.filter(v => v.type.kind === graphql_1.Kind.NON_NULL_TYPE)
|
|
39
|
+
.map(v => {
|
|
40
|
+
this._imports.add(imports_js_1.Imports.Utils);
|
|
41
|
+
return `Utils.checkNotNull(${v.variable.name.value}, "${v.variable.name.value} == null");`;
|
|
42
|
+
});
|
|
43
|
+
const impl = [
|
|
44
|
+
...nonNullVariables,
|
|
45
|
+
`this.variables = ${!hasVariables
|
|
46
|
+
? 'Operation.EMPTY_VARIABLES'
|
|
47
|
+
: `new ${className}.Variables(${variables.map(v => v.variable.name.value).join(', ')})`};`,
|
|
48
|
+
].join('\n');
|
|
49
|
+
cls.addClassMethod(className, null, impl, node.variableDefinitions.map(varDec => {
|
|
50
|
+
const outputType = (0, visitor_plugin_common_1.getBaseTypeNode)(varDec.type).name.value;
|
|
51
|
+
const schemaType = this._schema.getType(outputType);
|
|
52
|
+
const javaClass = this.getJavaClass(schemaType);
|
|
53
|
+
const typeToUse = this.getListTypeNodeWrapped(javaClass, varDec.type);
|
|
54
|
+
const isNonNull = varDec.type.kind === graphql_1.Kind.NON_NULL_TYPE;
|
|
55
|
+
return {
|
|
56
|
+
name: varDec.variable.name.value,
|
|
57
|
+
type: typeToUse,
|
|
58
|
+
annotations: [isNonNull ? 'Nonnull' : 'Nullable'],
|
|
59
|
+
};
|
|
60
|
+
}), null, 'public');
|
|
61
|
+
}
|
|
62
|
+
getRootType(operation) {
|
|
63
|
+
if (operation === 'query') {
|
|
64
|
+
return this._schema.getQueryType();
|
|
65
|
+
}
|
|
66
|
+
if (operation === 'mutation') {
|
|
67
|
+
return this._schema.getMutationType();
|
|
68
|
+
}
|
|
69
|
+
if (operation === 'subscription') {
|
|
70
|
+
return this._schema.getSubscriptionType();
|
|
71
|
+
}
|
|
72
|
+
return null;
|
|
73
|
+
}
|
|
74
|
+
createUniqueClassName(inUse, name, count = 0) {
|
|
75
|
+
const possibleNewName = count === 0 ? name : `${name}${count}`;
|
|
76
|
+
if (inUse.includes(possibleNewName)) {
|
|
77
|
+
return this.createUniqueClassName(inUse, name, count + 1);
|
|
78
|
+
}
|
|
79
|
+
return possibleNewName;
|
|
80
|
+
}
|
|
81
|
+
transformSelectionSet(options, isRoot = true) {
|
|
82
|
+
if (!options.result) {
|
|
83
|
+
options.result = {};
|
|
84
|
+
}
|
|
85
|
+
if (!(0, graphql_1.isObjectType)(options.schemaType) && !(0, graphql_1.isInterfaceType)(options.schemaType)) {
|
|
86
|
+
return options.result;
|
|
87
|
+
}
|
|
88
|
+
const className = this.createUniqueClassName(Object.keys(options.result), options.className);
|
|
89
|
+
const cls = new java_common_1.JavaDeclarationBlock()
|
|
90
|
+
.access('public')
|
|
91
|
+
.asKind('class')
|
|
92
|
+
.withName(className)
|
|
93
|
+
.implements(options.implements || []);
|
|
94
|
+
if (!options.nonStaticClass) {
|
|
95
|
+
cls.static();
|
|
96
|
+
}
|
|
97
|
+
options.result[className] = cls;
|
|
98
|
+
const fields = options.schemaType.getFields();
|
|
99
|
+
const childFields = [...(options.additionalFields || [])];
|
|
100
|
+
const childInlineFragments = [];
|
|
101
|
+
const childFragmentSpread = [...(options.additionalFragments || [])];
|
|
102
|
+
const selections = [...(options.selectionSet || [])];
|
|
103
|
+
const responseFieldArr = [];
|
|
104
|
+
for (const selection of selections) {
|
|
105
|
+
if (selection.kind === graphql_1.Kind.FIELD) {
|
|
106
|
+
this._imports.add(imports_js_1.Imports.ResponseField);
|
|
107
|
+
const field = fields[selection.name.value];
|
|
108
|
+
const isObject = selection.selectionSet && selection.selectionSet.selections && selection.selectionSet.selections.length > 0;
|
|
109
|
+
const isNonNull = (0, graphql_1.isNonNullType)(field.type);
|
|
110
|
+
const fieldAnnotation = isNonNull ? 'Nonnull' : 'Nullable';
|
|
111
|
+
this._imports.add(imports_js_1.Imports[fieldAnnotation]);
|
|
112
|
+
const baseType = (0, plugin_helpers_1.getBaseType)(field.type);
|
|
113
|
+
const isList = (0, graphql_1.isListType)(field.type) || ((0, graphql_1.isNonNullType)(field.type) && (0, graphql_1.isListType)(field.type.ofType));
|
|
114
|
+
if (isObject) {
|
|
115
|
+
let childClsName = this.convertName(field.name);
|
|
116
|
+
if (isList && pluralize_1.default.isPlural(childClsName)) {
|
|
117
|
+
childClsName = pluralize_1.default.singular(childClsName);
|
|
118
|
+
}
|
|
119
|
+
this.transformSelectionSet({
|
|
120
|
+
className: childClsName,
|
|
121
|
+
result: options.result,
|
|
122
|
+
selectionSet: selection.selectionSet.selections,
|
|
123
|
+
schemaType: baseType,
|
|
124
|
+
}, false);
|
|
125
|
+
childFields.push({
|
|
126
|
+
rawType: field.type,
|
|
127
|
+
isObject: true,
|
|
128
|
+
isList,
|
|
129
|
+
isFragment: false,
|
|
130
|
+
type: baseType,
|
|
131
|
+
isNonNull,
|
|
132
|
+
annotation: fieldAnnotation,
|
|
133
|
+
className: childClsName,
|
|
134
|
+
fieldName: field.name,
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
else {
|
|
138
|
+
const javaClass = this.getJavaClass(baseType);
|
|
139
|
+
childFields.push({
|
|
140
|
+
rawType: field.type,
|
|
141
|
+
isObject: false,
|
|
142
|
+
isFragment: false,
|
|
143
|
+
isList,
|
|
144
|
+
type: baseType,
|
|
145
|
+
isNonNull,
|
|
146
|
+
annotation: fieldAnnotation,
|
|
147
|
+
className: javaClass,
|
|
148
|
+
fieldName: field.name,
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
this._imports.add(imports_js_1.Imports.ResponseField);
|
|
152
|
+
this._imports.add(imports_js_1.Imports.Collections);
|
|
153
|
+
const operationArgs = (0, field_arguments_js_1.visitFieldArguments)(selection, this._imports);
|
|
154
|
+
const responseFieldMethod = this._resolveResponseFieldMethodForBaseType(field.type);
|
|
155
|
+
responseFieldArr.push(`ResponseField.${responseFieldMethod.fn}("${selection.alias ? selection.alias.value : selection.name.value}", "${selection.name.value}", ${operationArgs}, ${!(0, graphql_1.isNonNullType)(field.type)},${responseFieldMethod.custom ? ` CustomType.${baseType.name},` : ''} Collections.<ResponseField.Condition>emptyList())`);
|
|
156
|
+
}
|
|
157
|
+
else if (selection.kind === graphql_1.Kind.INLINE_FRAGMENT) {
|
|
158
|
+
if ((0, graphql_1.isUnionType)(options.schemaType) || (0, graphql_1.isInterfaceType)(options.schemaType)) {
|
|
159
|
+
childInlineFragments.push({
|
|
160
|
+
onType: selection.typeCondition.name.value,
|
|
161
|
+
node: selection,
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
else {
|
|
165
|
+
selections.push(...selection.selectionSet.selections);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
else if (selection.kind === graphql_1.Kind.FRAGMENT_SPREAD) {
|
|
169
|
+
const fragment = this._availableFragments.find(f => f.name === selection.name.value);
|
|
170
|
+
if (fragment) {
|
|
171
|
+
childFragmentSpread.push(fragment);
|
|
172
|
+
this._imports.add(`${this.config.fragmentPackage}.${fragment.name}`);
|
|
173
|
+
}
|
|
174
|
+
else {
|
|
175
|
+
throw new Error(`Fragment with name ${selection.name.value} was not loaded as document!`);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
if (childInlineFragments.length > 0) {
|
|
180
|
+
const childFieldsBase = [...childFields];
|
|
181
|
+
childFields.push(...childInlineFragments.map(inlineFragment => {
|
|
182
|
+
const cls = `As${inlineFragment.onType}`;
|
|
183
|
+
const schemaType = this._schema.getType(inlineFragment.onType);
|
|
184
|
+
this.transformSelectionSet({
|
|
185
|
+
additionalFields: childFieldsBase,
|
|
186
|
+
additionalFragments: childFragmentSpread,
|
|
187
|
+
className: cls,
|
|
188
|
+
result: options.result,
|
|
189
|
+
selectionSet: inlineFragment.node.selectionSet.selections,
|
|
190
|
+
schemaType,
|
|
191
|
+
}, false);
|
|
192
|
+
this._imports.add(imports_js_1.Imports.Nullable);
|
|
193
|
+
return {
|
|
194
|
+
isFragment: false,
|
|
195
|
+
rawType: schemaType,
|
|
196
|
+
isObject: true,
|
|
197
|
+
isList: false,
|
|
198
|
+
type: schemaType,
|
|
199
|
+
isNonNull: false,
|
|
200
|
+
annotation: 'Nullable',
|
|
201
|
+
className: cls,
|
|
202
|
+
fieldName: `as${inlineFragment.onType}`,
|
|
203
|
+
};
|
|
204
|
+
}));
|
|
205
|
+
responseFieldArr.push(...childInlineFragments.map(f => {
|
|
206
|
+
this._imports.add(imports_js_1.Imports.Arrays);
|
|
207
|
+
return `ResponseField.forInlineFragment("__typename", "__typename", Arrays.asList("${f.onType}"))`;
|
|
208
|
+
}));
|
|
209
|
+
}
|
|
210
|
+
if (childFragmentSpread.length > 0) {
|
|
211
|
+
responseFieldArr.push(`ResponseField.forFragment("__typename", "__typename", Arrays.asList(${childFragmentSpread
|
|
212
|
+
.map(f => `"${f.onType}"`)
|
|
213
|
+
.join(', ')}))`);
|
|
214
|
+
this._imports.add(imports_js_1.Imports.ResponseField);
|
|
215
|
+
this._imports.add(imports_js_1.Imports.Nonnull);
|
|
216
|
+
this._imports.add(imports_js_1.Imports.Arrays);
|
|
217
|
+
const fragmentsClassName = 'Fragments';
|
|
218
|
+
childFields.push({
|
|
219
|
+
isObject: true,
|
|
220
|
+
isList: false,
|
|
221
|
+
isFragment: true,
|
|
222
|
+
rawType: options.schemaType,
|
|
223
|
+
type: options.schemaType,
|
|
224
|
+
isNonNull: true,
|
|
225
|
+
annotation: 'Nonnull',
|
|
226
|
+
className: fragmentsClassName,
|
|
227
|
+
fieldName: 'fragments',
|
|
228
|
+
});
|
|
229
|
+
const fragmentsClass = new java_common_1.JavaDeclarationBlock()
|
|
230
|
+
.withName(fragmentsClassName)
|
|
231
|
+
.access('public')
|
|
232
|
+
.static()
|
|
233
|
+
.final()
|
|
234
|
+
.asKind('class');
|
|
235
|
+
const fragmentMapperClass = new java_common_1.JavaDeclarationBlock()
|
|
236
|
+
.withName('Mapper')
|
|
237
|
+
.access('public')
|
|
238
|
+
.static()
|
|
239
|
+
.final()
|
|
240
|
+
.implements([`FragmentResponseFieldMapper<${fragmentsClassName}>`])
|
|
241
|
+
.asKind('class');
|
|
242
|
+
fragmentsClass.addClassMethod(fragmentsClassName, null, childFragmentSpread
|
|
243
|
+
.map(spread => {
|
|
244
|
+
const varName = (0, change_case_all_1.camelCase)(spread.name);
|
|
245
|
+
this._imports.add(imports_js_1.Imports.Utils);
|
|
246
|
+
return `this.${varName} = Utils.checkNotNull(${varName}, "${varName} == null");`;
|
|
247
|
+
})
|
|
248
|
+
.join('\n'), childFragmentSpread.map(spread => ({
|
|
249
|
+
name: (0, change_case_all_1.camelCase)(spread.name),
|
|
250
|
+
type: spread.name,
|
|
251
|
+
annotations: ['Nonnull'],
|
|
252
|
+
})), [], 'public');
|
|
253
|
+
for (const spread of childFragmentSpread) {
|
|
254
|
+
const fragmentVarName = (0, change_case_all_1.camelCase)(spread.name);
|
|
255
|
+
fragmentsClass.addClassMember(fragmentVarName, spread.name, null, ['Nonnull'], 'private', { final: true });
|
|
256
|
+
fragmentsClass.addClassMethod(fragmentVarName, spread.name, `return this.${fragmentVarName};`, [], ['Nonnull'], 'public', {}, []);
|
|
257
|
+
fragmentMapperClass.addClassMember(`${fragmentVarName}FieldMapper`, `${spread.name}.Mapper`, `new ${spread.name}.Mapper()`, [], 'private', { final: true });
|
|
258
|
+
}
|
|
259
|
+
fragmentMapperClass.addClassMethod('map', fragmentsClassName, `
|
|
260
|
+
${childFragmentSpread
|
|
261
|
+
.map(spread => {
|
|
262
|
+
const fragmentVarName = (0, change_case_all_1.camelCase)(spread.name);
|
|
263
|
+
return `${spread.name} ${fragmentVarName} = null;
|
|
264
|
+
if (${spread.name}.POSSIBLE_TYPES.contains(conditionalType)) {
|
|
265
|
+
${fragmentVarName} = ${fragmentVarName}FieldMapper.map(reader);
|
|
266
|
+
}`;
|
|
267
|
+
})
|
|
268
|
+
.join('\n')}
|
|
269
|
+
|
|
270
|
+
return new Fragments(${childFragmentSpread
|
|
271
|
+
.map(spread => {
|
|
272
|
+
const fragmentVarName = (0, change_case_all_1.camelCase)(spread.name);
|
|
273
|
+
return `Utils.checkNotNull(${fragmentVarName}, "${fragmentVarName} == null")`;
|
|
274
|
+
})
|
|
275
|
+
.join(', ')});
|
|
276
|
+
`, [
|
|
277
|
+
{
|
|
278
|
+
name: 'reader',
|
|
279
|
+
type: 'ResponseReader',
|
|
280
|
+
},
|
|
281
|
+
{
|
|
282
|
+
name: 'conditionalType',
|
|
283
|
+
type: 'String',
|
|
284
|
+
annotations: ['Nonnull'],
|
|
285
|
+
},
|
|
286
|
+
], ['Nonnull'], 'public', {}, ['Override']);
|
|
287
|
+
this._imports.add(imports_js_1.Imports.String);
|
|
288
|
+
this._imports.add(imports_js_1.Imports.ResponseReader);
|
|
289
|
+
this._imports.add(imports_js_1.Imports.ResponseFieldMarshaller);
|
|
290
|
+
this._imports.add(imports_js_1.Imports.ResponseWriter);
|
|
291
|
+
fragmentsClass.addClassMethod('marshaller', 'ResponseFieldMarshaller', `return new ResponseFieldMarshaller() {
|
|
292
|
+
@Override
|
|
293
|
+
public void marshal(ResponseWriter writer) {
|
|
294
|
+
${childFragmentSpread
|
|
295
|
+
.map(spread => {
|
|
296
|
+
const fragmentVarName = (0, change_case_all_1.camelCase)(spread.name);
|
|
297
|
+
return (0, visitor_plugin_common_1.indentMultiline)(`final ${spread.name} $${fragmentVarName} = ${fragmentVarName};\nif ($${fragmentVarName} != null) { $${fragmentVarName}.marshaller().marshal(writer); }`, 2);
|
|
298
|
+
})
|
|
299
|
+
.join('\n')}
|
|
300
|
+
}
|
|
301
|
+
};
|
|
302
|
+
`, [], [], 'public');
|
|
303
|
+
fragmentsClass.addClassMember('$toString', 'String', null, [], 'private', { volatile: true });
|
|
304
|
+
fragmentsClass.addClassMember('$hashCode', 'int', null, [], 'private', { volatile: true });
|
|
305
|
+
fragmentsClass.addClassMember('$hashCodeMemoized', 'boolean', null, [], 'private', { volatile: true });
|
|
306
|
+
fragmentsClass.addClassMethod('toString', 'String', `if ($toString == null) {
|
|
307
|
+
$toString = "${fragmentsClassName}{"
|
|
308
|
+
${childFragmentSpread
|
|
309
|
+
.map(spread => {
|
|
310
|
+
const varName = (0, change_case_all_1.camelCase)(spread.name);
|
|
311
|
+
return (0, visitor_plugin_common_1.indent)(`+ "${varName}=" + ${varName} + ", "`, 2);
|
|
312
|
+
})
|
|
313
|
+
.join('\n')}
|
|
314
|
+
+ "}";
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
return $toString;`, [], [], 'public', {}, ['Override']);
|
|
318
|
+
// Add equals
|
|
319
|
+
fragmentsClass.addClassMethod('equals', 'boolean', `if (o == this) {
|
|
320
|
+
return true;
|
|
321
|
+
}
|
|
322
|
+
if (o instanceof ${fragmentsClassName}) {
|
|
323
|
+
${fragmentsClassName} that = (${fragmentsClassName}) o;
|
|
324
|
+
return ${childFragmentSpread
|
|
325
|
+
.map(spread => {
|
|
326
|
+
const varName = (0, change_case_all_1.camelCase)(spread.name);
|
|
327
|
+
return `this.${varName}.equals(that.${varName})`;
|
|
328
|
+
})
|
|
329
|
+
.join(' && ')};
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
return false;`, [{ name: 'o', type: 'Object' }], [], 'public', {}, ['Override']);
|
|
333
|
+
// hashCode
|
|
334
|
+
fragmentsClass.addClassMethod('hashCode', 'int', `if (!$hashCodeMemoized) {
|
|
335
|
+
int h = 1;
|
|
336
|
+
${childFragmentSpread
|
|
337
|
+
.map(spread => {
|
|
338
|
+
const varName = (0, change_case_all_1.camelCase)(spread.name);
|
|
339
|
+
return (0, visitor_plugin_common_1.indentMultiline)(`h *= 1000003;\nh ^= ${varName}.hashCode();`, 1);
|
|
340
|
+
})
|
|
341
|
+
.join('\n')}
|
|
342
|
+
$hashCode = h;
|
|
343
|
+
$hashCodeMemoized = true;
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
return $hashCode;`, [], [], 'public', {}, ['Override']);
|
|
347
|
+
this._imports.add(imports_js_1.Imports.FragmentResponseFieldMapper);
|
|
348
|
+
fragmentsClass.nestedClass(fragmentMapperClass);
|
|
349
|
+
cls.nestedClass(fragmentsClass);
|
|
350
|
+
}
|
|
351
|
+
if (responseFieldArr.length > 0 && !isRoot) {
|
|
352
|
+
responseFieldArr.unshift(`ResponseField.forString("__typename", "__typename", null, false, Collections.<ResponseField.Condition>emptyList())`);
|
|
353
|
+
}
|
|
354
|
+
if (!isRoot) {
|
|
355
|
+
this._imports.add(imports_js_1.Imports.Nonnull);
|
|
356
|
+
childFields.unshift({
|
|
357
|
+
isObject: false,
|
|
358
|
+
isFragment: false,
|
|
359
|
+
isList: false,
|
|
360
|
+
type: graphql_1.GraphQLString,
|
|
361
|
+
rawType: graphql_1.GraphQLString,
|
|
362
|
+
isNonNull: true,
|
|
363
|
+
annotation: 'Nonnull',
|
|
364
|
+
className: 'String',
|
|
365
|
+
fieldName: '__typename',
|
|
366
|
+
});
|
|
367
|
+
}
|
|
368
|
+
// Add members
|
|
369
|
+
childFields.forEach(c => {
|
|
370
|
+
cls.addClassMember(c.fieldName, this.getListTypeWrapped(c.className, c.rawType), null, [c.annotation], 'private', { final: true });
|
|
371
|
+
});
|
|
372
|
+
// Add $toString, $hashCode, $hashCodeMemoized
|
|
373
|
+
cls.addClassMember('$toString', 'String', null, [], 'private', { volatile: true });
|
|
374
|
+
cls.addClassMember('$hashCode', 'int', null, [], 'private', { volatile: true });
|
|
375
|
+
cls.addClassMember('$hashCodeMemoized', 'boolean', null, [], 'private', { volatile: true });
|
|
376
|
+
// Add responseFields for all fields
|
|
377
|
+
cls.addClassMember('$responseFields', 'ResponseField[]', `{\n${(0, visitor_plugin_common_1.indentMultiline)(responseFieldArr.join(',\n'), 2) + '\n }'}`, [], null, { static: true, final: true });
|
|
378
|
+
// Add Ctor
|
|
379
|
+
this._imports.add(imports_js_1.Imports.Utils);
|
|
380
|
+
cls.addClassMethod(className, null, childFields
|
|
381
|
+
.map(c => `this.${c.fieldName} = ${c.isNonNull ? `Utils.checkNotNull(${c.fieldName}, "${c.fieldName} == null")` : c.fieldName};`)
|
|
382
|
+
.join('\n'), childFields.map(c => ({
|
|
383
|
+
name: c.fieldName,
|
|
384
|
+
type: this.getListTypeWrapped(c.className, c.rawType),
|
|
385
|
+
annotations: [c.annotation],
|
|
386
|
+
})), null, 'public');
|
|
387
|
+
// Add getters for all members
|
|
388
|
+
childFields.forEach(c => {
|
|
389
|
+
cls.addClassMethod(c.fieldName, this.getListTypeWrapped(c.className, c.rawType), `return this.${c.fieldName};`, [], [c.annotation], 'public', {});
|
|
390
|
+
});
|
|
391
|
+
// Add .toString()
|
|
392
|
+
cls.addClassMethod('toString', 'String', `if ($toString == null) {
|
|
393
|
+
$toString = "${className}{"
|
|
394
|
+
${childFields.map(c => (0, visitor_plugin_common_1.indent)(`+ "${c.fieldName}=" + ${c.fieldName} + ", "`, 2)).join('\n')}
|
|
395
|
+
+ "}";
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
return $toString;`, [], [], 'public', {}, ['Override']);
|
|
399
|
+
// Add equals
|
|
400
|
+
cls.addClassMethod('equals', 'boolean', `if (o == this) {
|
|
401
|
+
return true;
|
|
402
|
+
}
|
|
403
|
+
if (o instanceof ${className}) {
|
|
404
|
+
${className} that = (${className}) o;
|
|
405
|
+
return ${childFields
|
|
406
|
+
.map(c => c.isNonNull
|
|
407
|
+
? `this.${c.fieldName}.equals(that.${c.fieldName})`
|
|
408
|
+
: `((this.${c.fieldName} == null) ? (that.${c.fieldName} == null) : this.${c.fieldName}.equals(that.${c.fieldName}))`)
|
|
409
|
+
.join(' && ')};
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
return false;`, [{ name: 'o', type: 'Object' }], [], 'public', {}, ['Override']);
|
|
413
|
+
// hashCode
|
|
414
|
+
cls.addClassMethod('hashCode', 'int', `if (!$hashCodeMemoized) {
|
|
415
|
+
int h = 1;
|
|
416
|
+
${childFields
|
|
417
|
+
.map(f => (0, visitor_plugin_common_1.indentMultiline)(`h *= 1000003;\nh ^= ${!f.isNonNull ? `(${f.fieldName} == null) ? 0 : ` : ''}${f.fieldName}.hashCode();`, 1))
|
|
418
|
+
.join('\n')}
|
|
419
|
+
$hashCode = h;
|
|
420
|
+
$hashCodeMemoized = true;
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
return $hashCode;`, [], [], 'public', {}, ['Override']);
|
|
424
|
+
this._imports.add(imports_js_1.Imports.ResponseReader);
|
|
425
|
+
this._imports.add(imports_js_1.Imports.ResponseFieldMarshaller);
|
|
426
|
+
this._imports.add(imports_js_1.Imports.ResponseWriter);
|
|
427
|
+
// marshaller
|
|
428
|
+
cls.addClassMethod('marshaller', 'ResponseFieldMarshaller', `return new ResponseFieldMarshaller() {
|
|
429
|
+
@Override
|
|
430
|
+
public void marshal(ResponseWriter writer) {
|
|
431
|
+
${childFields
|
|
432
|
+
.map((f, index) => {
|
|
433
|
+
const writerMethod = this._getWriterMethodByType(f.type);
|
|
434
|
+
if (f.isList) {
|
|
435
|
+
return (0, visitor_plugin_common_1.indentMultiline)(`writer.writeList($responseFields[${index}], ${f.fieldName}, new ResponseWriter.ListWriter() {
|
|
436
|
+
@Override
|
|
437
|
+
public void write(Object value, ResponseWriter.ListItemWriter listItemWriter) {
|
|
438
|
+
listItemWriter.${writerMethod.name}(((${f.className}) value)${writerMethod.useMarshaller ? '.marshaller()' : ''});
|
|
439
|
+
}
|
|
440
|
+
});`, 2);
|
|
441
|
+
}
|
|
442
|
+
let fValue = `${f.fieldName}${writerMethod.useMarshaller ? '.marshaller()' : ''}`;
|
|
443
|
+
if (writerMethod.checkNull || !f.isNonNull) {
|
|
444
|
+
fValue = `${f.fieldName} != null ? ${fValue} : null`;
|
|
445
|
+
}
|
|
446
|
+
return (0, visitor_plugin_common_1.indent)(`writer.${writerMethod.name}(${writerMethod.castTo ? `(${writerMethod.castTo}) ` : ''}$responseFields[${index}], ${fValue});`, 2);
|
|
447
|
+
})
|
|
448
|
+
.join('\n')}
|
|
449
|
+
}
|
|
450
|
+
};`, [], [], 'public');
|
|
451
|
+
cls.nestedClass(this.buildMapperClass(className, childFields));
|
|
452
|
+
return options.result;
|
|
453
|
+
}
|
|
454
|
+
getReaderFn(baseType) {
|
|
455
|
+
if ((0, graphql_1.isScalarType)(baseType)) {
|
|
456
|
+
if (baseType.name === 'String') {
|
|
457
|
+
return { fn: `readString` };
|
|
458
|
+
}
|
|
459
|
+
if (baseType.name === 'Int') {
|
|
460
|
+
return { fn: `readInt` };
|
|
461
|
+
}
|
|
462
|
+
if (baseType.name === 'Float') {
|
|
463
|
+
return { fn: `readDouble` };
|
|
464
|
+
}
|
|
465
|
+
if (baseType.name === 'Boolean') {
|
|
466
|
+
return { fn: `readBoolean` };
|
|
467
|
+
}
|
|
468
|
+
return { fn: `readCustomType`, custom: true };
|
|
469
|
+
}
|
|
470
|
+
if ((0, graphql_1.isEnumType)(baseType)) {
|
|
471
|
+
return { fn: `readString` };
|
|
472
|
+
}
|
|
473
|
+
return { fn: `readObject`, object: baseType.name };
|
|
474
|
+
}
|
|
475
|
+
buildMapperClass(parentClassName, childFields) {
|
|
476
|
+
const wrapList = (childField, rawType, edgeStr) => {
|
|
477
|
+
if ((0, graphql_1.isNonNullType)(rawType)) {
|
|
478
|
+
return wrapList(childField, rawType.ofType, edgeStr);
|
|
479
|
+
}
|
|
480
|
+
if ((0, graphql_1.isListType)(rawType)) {
|
|
481
|
+
const typeStr = this.getListTypeWrapped(childField.className, rawType.ofType);
|
|
482
|
+
const innerContent = wrapList(childField, rawType.ofType, edgeStr);
|
|
483
|
+
const inner = (0, graphql_1.isListType)(rawType.ofType) ? `return listItemReader.readList(${innerContent});` : innerContent;
|
|
484
|
+
return `new ResponseReader.ListReader<${typeStr}>() {
|
|
485
|
+
@Override
|
|
486
|
+
public ${typeStr} read(ResponseReader.ListItemReader listItemReader) {
|
|
487
|
+
${(0, visitor_plugin_common_1.indentMultiline)(inner, 2)}
|
|
488
|
+
}
|
|
489
|
+
}`;
|
|
490
|
+
}
|
|
491
|
+
return edgeStr;
|
|
492
|
+
};
|
|
493
|
+
this._imports.add(imports_js_1.Imports.ResponseReader);
|
|
494
|
+
const mapperBody = childFields.map((f, index) => {
|
|
495
|
+
const varDec = `final ${this.getListTypeWrapped(f.className, f.rawType)} ${f.fieldName} =`;
|
|
496
|
+
const readerFn = this.getReaderFn(f.type);
|
|
497
|
+
if (f.isFragment) {
|
|
498
|
+
return `${varDec} reader.readConditional($responseFields[${index}], new ResponseReader.ConditionalTypeReader<${f.className}>() {
|
|
499
|
+
@Override
|
|
500
|
+
public ${f.className} read(String conditionalType, ResponseReader reader) {
|
|
501
|
+
return fragmentsFieldMapper.map(reader, conditionalType);
|
|
502
|
+
}
|
|
503
|
+
});`;
|
|
504
|
+
}
|
|
505
|
+
if (f.isList) {
|
|
506
|
+
const listReader = readerFn.object
|
|
507
|
+
? `return listItemReader.${readerFn.fn}(new ResponseReader.ObjectReader<Item>() {
|
|
508
|
+
@Override
|
|
509
|
+
public Item read(ResponseReader reader) {
|
|
510
|
+
return ${f.fieldName}FieldMapper.map(reader);
|
|
511
|
+
}
|
|
512
|
+
});`
|
|
513
|
+
: `return listItemReader.${readerFn.fn}();`;
|
|
514
|
+
const wrappedList = wrapList(f, f.rawType, listReader);
|
|
515
|
+
return `${varDec} reader.readList($responseFields[${index}], ${wrappedList});`;
|
|
516
|
+
}
|
|
517
|
+
if (readerFn.object) {
|
|
518
|
+
return `${varDec} reader.readObject($responseFields[${index}], new ResponseReader.ObjectReader<${f.className}>() {
|
|
519
|
+
@Override
|
|
520
|
+
public ${f.className} read(ResponseReader reader) {
|
|
521
|
+
return ${f.fieldName}FieldMapper.map(reader);
|
|
522
|
+
}
|
|
523
|
+
});`;
|
|
524
|
+
}
|
|
525
|
+
return `${varDec} reader.${readerFn.fn}(${readerFn.custom ? '(ResponseField.CustomTypeField) ' : ''}$responseFields[${index}]);`;
|
|
526
|
+
});
|
|
527
|
+
const mapperImpl = [
|
|
528
|
+
...mapperBody,
|
|
529
|
+
`return new ${parentClassName}(${childFields.map(f => f.fieldName).join(', ')});`,
|
|
530
|
+
].join('\n');
|
|
531
|
+
const cls = new java_common_1.JavaDeclarationBlock()
|
|
532
|
+
.access('public')
|
|
533
|
+
.static()
|
|
534
|
+
.final()
|
|
535
|
+
.asKind('class')
|
|
536
|
+
.withName('Mapper')
|
|
537
|
+
.implements([`ResponseFieldMapper<${parentClassName}>`])
|
|
538
|
+
.addClassMethod('map', parentClassName, mapperImpl, [
|
|
539
|
+
{
|
|
540
|
+
name: 'reader',
|
|
541
|
+
type: 'ResponseReader',
|
|
542
|
+
},
|
|
543
|
+
], [], 'public', {}, ['Override']);
|
|
544
|
+
childFields
|
|
545
|
+
.filter(c => c.isObject)
|
|
546
|
+
.forEach(childField => {
|
|
547
|
+
cls.addClassMember(`${childField.fieldName}FieldMapper`, `${childField.className}.Mapper`, `new ${childField.className}.Mapper()`, [], 'private', { final: true });
|
|
548
|
+
});
|
|
549
|
+
return cls;
|
|
550
|
+
}
|
|
551
|
+
_resolveResponseFieldMethodForBaseType(baseType) {
|
|
552
|
+
if ((0, graphql_1.isListType)(baseType)) {
|
|
553
|
+
return { fn: `forList` };
|
|
554
|
+
}
|
|
555
|
+
if ((0, graphql_1.isNonNullType)(baseType)) {
|
|
556
|
+
return this._resolveResponseFieldMethodForBaseType(baseType.ofType);
|
|
557
|
+
}
|
|
558
|
+
if ((0, graphql_1.isScalarType)(baseType)) {
|
|
559
|
+
if (baseType.name === 'String') {
|
|
560
|
+
return { fn: `forString` };
|
|
561
|
+
}
|
|
562
|
+
if (baseType.name === 'Int') {
|
|
563
|
+
return { fn: `forInt` };
|
|
564
|
+
}
|
|
565
|
+
if (baseType.name === 'Float') {
|
|
566
|
+
return { fn: `forDouble` };
|
|
567
|
+
}
|
|
568
|
+
if (baseType.name === 'Boolean') {
|
|
569
|
+
return { fn: `forBoolean` };
|
|
570
|
+
}
|
|
571
|
+
this._imports.add(`${this.config.typePackage}.CustomType`);
|
|
572
|
+
return { fn: `forCustomType`, custom: true };
|
|
573
|
+
}
|
|
574
|
+
if ((0, graphql_1.isEnumType)(baseType)) {
|
|
575
|
+
return { fn: `forEnum` };
|
|
576
|
+
}
|
|
577
|
+
return { fn: `forObject` };
|
|
578
|
+
}
|
|
579
|
+
FragmentDefinition(node) {
|
|
580
|
+
this.visitingFragment = true;
|
|
581
|
+
const className = node.name.value;
|
|
582
|
+
const schemaType = this._schema.getType(node.typeCondition.name.value);
|
|
583
|
+
this._imports.add(imports_js_1.Imports.Arrays);
|
|
584
|
+
this._imports.add(imports_js_1.Imports.GraphqlFragment);
|
|
585
|
+
this._imports.add(imports_js_1.Imports.List);
|
|
586
|
+
this._imports.add(imports_js_1.Imports.String);
|
|
587
|
+
this._imports.add(imports_js_1.Imports.Collections);
|
|
588
|
+
this._imports.add(imports_js_1.Imports.Override);
|
|
589
|
+
this._imports.add(imports_js_1.Imports.Generated);
|
|
590
|
+
this._imports.add(imports_js_1.Imports.ResponseFieldMapper);
|
|
591
|
+
const dataClasses = this.transformSelectionSet({
|
|
592
|
+
className,
|
|
593
|
+
nonStaticClass: true,
|
|
594
|
+
implements: ['GraphqlFragment'],
|
|
595
|
+
selectionSet: node.selectionSet && node.selectionSet.selections ? node.selectionSet.selections : [],
|
|
596
|
+
result: {},
|
|
597
|
+
schemaType,
|
|
598
|
+
}, false);
|
|
599
|
+
const rootCls = dataClasses[className];
|
|
600
|
+
const printed = this.printDocument(node);
|
|
601
|
+
rootCls.addClassMember('FRAGMENT_DEFINITION', 'String', `"${printed}"`, [], 'public', {
|
|
602
|
+
static: true,
|
|
603
|
+
final: true,
|
|
604
|
+
});
|
|
605
|
+
const possibleTypes = (0, graphql_1.isObjectType)(schemaType) ? [schemaType.name] : this.getImplementingTypes(schemaType);
|
|
606
|
+
rootCls.addClassMember('POSSIBLE_TYPES', 'List<String>', `Collections.unmodifiableList(Arrays.asList(${possibleTypes.map(t => `"${t}"`).join(', ')}))`, [], 'public', { static: true, final: true });
|
|
607
|
+
Object.keys(dataClasses)
|
|
608
|
+
.filter(name => name !== className)
|
|
609
|
+
.forEach(clsName => {
|
|
610
|
+
rootCls.nestedClass(dataClasses[clsName]);
|
|
611
|
+
});
|
|
612
|
+
return rootCls.string;
|
|
613
|
+
}
|
|
614
|
+
OperationDefinition(node) {
|
|
615
|
+
this.visitingFragment = false;
|
|
616
|
+
const operationType = (0, change_case_all_1.pascalCase)(node.operation);
|
|
617
|
+
const operationSchemaType = this.getRootType(node.operation);
|
|
618
|
+
const className = node.name.value.endsWith(operationType) ? operationType : `${node.name.value}${operationType}`;
|
|
619
|
+
this._imports.add(imports_js_1.Imports[operationType]);
|
|
620
|
+
this._imports.add(imports_js_1.Imports.String);
|
|
621
|
+
this._imports.add(imports_js_1.Imports.Override);
|
|
622
|
+
this._imports.add(imports_js_1.Imports.Generated);
|
|
623
|
+
this._imports.add(imports_js_1.Imports.OperationName);
|
|
624
|
+
this._imports.add(imports_js_1.Imports.Operation);
|
|
625
|
+
this._imports.add(imports_js_1.Imports.ResponseFieldMapper);
|
|
626
|
+
const cls = new java_common_1.JavaDeclarationBlock()
|
|
627
|
+
.annotate([`Generated("Apollo GraphQL")`])
|
|
628
|
+
.access('public')
|
|
629
|
+
.final()
|
|
630
|
+
.asKind('class')
|
|
631
|
+
.withName(className);
|
|
632
|
+
const printed = this.printDocument(node);
|
|
633
|
+
cls.implements([
|
|
634
|
+
`${operationType}<${className}.Data, ${className}.Data, ${node.variableDefinitions.length === 0 ? 'Operation' : className}.Variables>`,
|
|
635
|
+
]);
|
|
636
|
+
cls.addClassMember('OPERATION_DEFINITION', 'String', `"${printed}"`, [], 'public', { static: true, final: true });
|
|
637
|
+
cls.addClassMember('QUERY_DOCUMENT', 'String', 'OPERATION_DEFINITION', [], 'public', { static: true, final: true });
|
|
638
|
+
cls.addClassMember('OPERATION_NAME', 'OperationName', `new OperationName() {
|
|
639
|
+
@Override
|
|
640
|
+
public String name() {
|
|
641
|
+
return "${node.name.value}";
|
|
642
|
+
}
|
|
643
|
+
}`, [], 'public', { static: true, final: true });
|
|
644
|
+
cls.addClassMember('variables', `${node.variableDefinitions.length === 0 ? 'Operation' : className}.Variables`, null, [], 'private', { final: true });
|
|
645
|
+
cls.addClassMethod('queryDocument', `String`, `return QUERY_DOCUMENT;`, [], [], 'public', {}, ['Override']);
|
|
646
|
+
cls.addClassMethod('wrapData', `${className}.Data`, `return data;`, [
|
|
647
|
+
{
|
|
648
|
+
name: 'data',
|
|
649
|
+
type: `${className}.Data`,
|
|
650
|
+
},
|
|
651
|
+
], [], 'public', {}, ['Override']);
|
|
652
|
+
cls.addClassMethod('variables', `${node.variableDefinitions.length === 0 ? 'Operation' : className}.Variables`, `return variables;`, [], [], 'public', {}, ['Override']);
|
|
653
|
+
cls.addClassMethod('responseFieldMapper', `ResponseFieldMapper<${className}.Data>`, `return new Data.Mapper();`, [], [], 'public', {}, ['Override']);
|
|
654
|
+
cls.addClassMethod('builder', `Builder`, `return new Builder();`, [], [], 'public', { static: true }, []);
|
|
655
|
+
cls.addClassMethod('name', `OperationName`, `return OPERATION_NAME;`, [], [], 'public', {}, ['Override']);
|
|
656
|
+
cls.addClassMethod('operationId', `String`, `return "${(0, crypto_1.createHash)('md5').update(printed).digest('hex')}";`, [], [], 'public', {}, []);
|
|
657
|
+
this.addCtor(className, node, cls);
|
|
658
|
+
this._imports.add(imports_js_1.Imports.Operation);
|
|
659
|
+
const dataClasses = this.transformSelectionSet({
|
|
660
|
+
className: 'Data',
|
|
661
|
+
implements: ['Operation.Data'],
|
|
662
|
+
selectionSet: node.selectionSet && node.selectionSet.selections ? node.selectionSet.selections : [],
|
|
663
|
+
result: {},
|
|
664
|
+
schemaType: operationSchemaType,
|
|
665
|
+
});
|
|
666
|
+
Object.keys(dataClasses).forEach(className => {
|
|
667
|
+
cls.nestedClass(dataClasses[className]);
|
|
668
|
+
});
|
|
669
|
+
cls.nestedClass(this.createBuilderClass(className, node.variableDefinitions || []));
|
|
670
|
+
cls.nestedClass(this.createVariablesClass(className, node.variableDefinitions || []));
|
|
671
|
+
return cls.string;
|
|
672
|
+
}
|
|
673
|
+
createVariablesClass(parentClassName, variables) {
|
|
674
|
+
const className = 'Variables';
|
|
675
|
+
const cls = new java_common_1.JavaDeclarationBlock()
|
|
676
|
+
.static()
|
|
677
|
+
.access('public')
|
|
678
|
+
.final()
|
|
679
|
+
.asKind('class')
|
|
680
|
+
.extends(['Operation.Variables'])
|
|
681
|
+
.withName(className);
|
|
682
|
+
const ctorImpl = [];
|
|
683
|
+
const ctorArgs = [];
|
|
684
|
+
variables.forEach(variable => {
|
|
685
|
+
ctorImpl.push(`this.${variable.variable.name.value} = ${variable.variable.name.value};`);
|
|
686
|
+
ctorImpl.push(`this.valueMap.put("${variable.variable.name.value}", ${variable.variable.name.value});`);
|
|
687
|
+
const baseTypeNode = (0, visitor_plugin_common_1.getBaseTypeNode)(variable.type);
|
|
688
|
+
const schemaType = this._schema.getType(baseTypeNode.name.value);
|
|
689
|
+
const javaClass = this.getJavaClass(schemaType);
|
|
690
|
+
const annotation = (0, graphql_1.isNonNullType)(variable.type) ? 'Nullable' : 'Nonnull';
|
|
691
|
+
this._imports.add(imports_js_1.Imports[annotation]);
|
|
692
|
+
ctorArgs.push({ name: variable.variable.name.value, type: javaClass, annotations: [annotation] });
|
|
693
|
+
cls.addClassMember(variable.variable.name.value, javaClass, null, [annotation], 'private');
|
|
694
|
+
cls.addClassMethod(variable.variable.name.value, javaClass, `return ${variable.variable.name.value};`, [], [], 'public');
|
|
695
|
+
});
|
|
696
|
+
this._imports.add(imports_js_1.Imports.LinkedHashMap);
|
|
697
|
+
this._imports.add(imports_js_1.Imports.Map);
|
|
698
|
+
cls.addClassMethod(className, null, ctorImpl.join('\n'), ctorArgs, [], 'public');
|
|
699
|
+
cls.addClassMember('valueMap', 'Map<String, Object>', 'new LinkedHashMap<>()', [], 'private', {
|
|
700
|
+
final: true,
|
|
701
|
+
transient: true,
|
|
702
|
+
});
|
|
703
|
+
cls.addClassMethod('valueMap', 'Map<String, Object>', 'return Collections.unmodifiableMap(valueMap);', [], [], 'public', {}, ['Override']);
|
|
704
|
+
const marshallerImpl = `return new InputFieldMarshaller() {
|
|
705
|
+
@Override
|
|
706
|
+
public void marshal(InputFieldWriter writer) throws IOException {
|
|
707
|
+
${variables
|
|
708
|
+
.map(v => {
|
|
709
|
+
const baseTypeNode = (0, visitor_plugin_common_1.getBaseTypeNode)(v.type);
|
|
710
|
+
const schemaType = this._schema.getType(baseTypeNode.name.value);
|
|
711
|
+
const writerMethod = this._getWriterMethodByType(schemaType, true);
|
|
712
|
+
return (0, visitor_plugin_common_1.indent)(`writer.${writerMethod.name}("${v.variable.name.value}", ${writerMethod.checkNull
|
|
713
|
+
? `${v.variable.name.value} != null ? ${v.variable.name.value}${writerMethod.useMarshaller ? '.marshaller()' : ''} : null`
|
|
714
|
+
: v.variable.name.value});`, 2);
|
|
715
|
+
})
|
|
716
|
+
.join('\n')}
|
|
717
|
+
}
|
|
718
|
+
};`;
|
|
719
|
+
this._imports.add(imports_js_1.Imports.InputFieldMarshaller);
|
|
720
|
+
this._imports.add(imports_js_1.Imports.InputFieldWriter);
|
|
721
|
+
this._imports.add(imports_js_1.Imports.IOException);
|
|
722
|
+
cls.addClassMethod('marshaller', 'InputFieldMarshaller', marshallerImpl, [], [], 'public', {}, ['Override']);
|
|
723
|
+
return cls;
|
|
724
|
+
}
|
|
725
|
+
_getWriterMethodByType(schemaType, idAsString = false) {
|
|
726
|
+
if ((0, graphql_1.isScalarType)(schemaType)) {
|
|
727
|
+
if (base_java_visitor_js_1.SCALAR_TO_WRITER_METHOD[schemaType.name] && (idAsString || schemaType.name !== 'ID')) {
|
|
728
|
+
return {
|
|
729
|
+
name: base_java_visitor_js_1.SCALAR_TO_WRITER_METHOD[schemaType.name],
|
|
730
|
+
checkNull: false,
|
|
731
|
+
useMarshaller: false,
|
|
732
|
+
};
|
|
733
|
+
}
|
|
734
|
+
return { name: 'writeCustom', checkNull: false, useMarshaller: false, castTo: 'ResponseField.CustomTypeField' };
|
|
735
|
+
}
|
|
736
|
+
if ((0, graphql_1.isInputObjectType)(schemaType)) {
|
|
737
|
+
return { name: 'writeObject', checkNull: true, useMarshaller: true };
|
|
738
|
+
}
|
|
739
|
+
if ((0, graphql_1.isEnumType)(schemaType)) {
|
|
740
|
+
return { name: 'writeString', checkNull: false, useMarshaller: false };
|
|
741
|
+
}
|
|
742
|
+
if ((0, graphql_1.isObjectType)(schemaType) || (0, graphql_1.isInterfaceType)(schemaType)) {
|
|
743
|
+
return { name: 'writeObject', checkNull: true, useMarshaller: true };
|
|
744
|
+
}
|
|
745
|
+
return { name: 'writeString', useMarshaller: false, checkNull: false };
|
|
746
|
+
}
|
|
747
|
+
createBuilderClass(parentClassName, variables) {
|
|
748
|
+
const builderClassName = 'Builder';
|
|
749
|
+
const cls = new java_common_1.JavaDeclarationBlock()
|
|
750
|
+
.static()
|
|
751
|
+
.final()
|
|
752
|
+
.access('public')
|
|
753
|
+
.asKind('class')
|
|
754
|
+
.withName(builderClassName)
|
|
755
|
+
.addClassMethod(builderClassName, null, '');
|
|
756
|
+
variables.forEach(variable => {
|
|
757
|
+
const baseTypeNode = (0, visitor_plugin_common_1.getBaseTypeNode)(variable.type);
|
|
758
|
+
const schemaType = this._schema.getType(baseTypeNode.name.value);
|
|
759
|
+
const javaClass = this.getJavaClass(schemaType);
|
|
760
|
+
const annotation = (0, graphql_1.isNonNullType)(variable.type) ? 'Nonnull' : 'Nullable';
|
|
761
|
+
this._imports.add(imports_js_1.Imports[annotation]);
|
|
762
|
+
cls.addClassMember(variable.variable.name.value, javaClass, null, [annotation], 'private');
|
|
763
|
+
cls.addClassMethod(variable.variable.name.value, builderClassName, `this.${variable.variable.name.value} = ${variable.variable.name.value};\nreturn this;`, [
|
|
764
|
+
{
|
|
765
|
+
name: variable.variable.name.value,
|
|
766
|
+
type: javaClass,
|
|
767
|
+
annotations: [annotation],
|
|
768
|
+
},
|
|
769
|
+
], [], 'public');
|
|
770
|
+
});
|
|
771
|
+
this._imports.add(imports_js_1.Imports.Utils);
|
|
772
|
+
const nonNullChecks = variables
|
|
773
|
+
.filter(f => (0, graphql_1.isNonNullType)(f))
|
|
774
|
+
.map(f => `Utils.checkNotNull(${f.variable.name.value}, "${f.variable.name.value} == null");`);
|
|
775
|
+
const returnStatement = `return new ${parentClassName}(${variables.map(v => v.variable.name.value).join(', ')});`;
|
|
776
|
+
cls.addClassMethod('build', parentClassName, `${[...nonNullChecks, returnStatement].join('\n')}`, [], [], 'public');
|
|
777
|
+
return cls;
|
|
778
|
+
}
|
|
779
|
+
}
|
|
780
|
+
exports.OperationVisitor = OperationVisitor;
|