@graphql-codegen/java-apollo-android 2.2.13 → 2.3.0-alpha-29eb1293b.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} +8 -565
- 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 +23 -16
- 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} +1 -1
- 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 -1339
package/index.js
DELETED
|
@@ -1,1339 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
-
|
|
5
|
-
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
|
|
6
|
-
|
|
7
|
-
const pluginHelpers = require('@graphql-codegen/plugin-helpers');
|
|
8
|
-
const graphql = require('graphql');
|
|
9
|
-
const visitorPluginCommon = require('@graphql-codegen/visitor-plugin-common');
|
|
10
|
-
const javaCommon = require('@graphql-codegen/java-common');
|
|
11
|
-
const crypto = require('crypto');
|
|
12
|
-
const pluralize = _interopDefault(require('pluralize'));
|
|
13
|
-
const changeCaseAll = require('change-case-all');
|
|
14
|
-
const path = require('path');
|
|
15
|
-
|
|
16
|
-
const Imports = {
|
|
17
|
-
// Primitives
|
|
18
|
-
String: 'java.lang.String',
|
|
19
|
-
Boolean: 'java.lang.Boolean',
|
|
20
|
-
Integer: 'java.lang.Integer',
|
|
21
|
-
Object: 'java.lang.Object',
|
|
22
|
-
Float: 'java.lang.Float',
|
|
23
|
-
Long: 'java.lang.Long',
|
|
24
|
-
// Java Base
|
|
25
|
-
Class: 'java.lang.Class',
|
|
26
|
-
Arrays: 'java.util.Arrays',
|
|
27
|
-
List: 'java.util.List',
|
|
28
|
-
IOException: 'java.io.IOException',
|
|
29
|
-
Collections: 'java.util.Collections',
|
|
30
|
-
LinkedHashMap: 'java.util.LinkedHashMap',
|
|
31
|
-
Map: 'java.util.Map',
|
|
32
|
-
// Annotations
|
|
33
|
-
Nonnull: 'javax.annotation.Nonnull',
|
|
34
|
-
Nullable: 'javax.annotation.Nullable',
|
|
35
|
-
Override: 'java.lang.Override',
|
|
36
|
-
Generated: 'javax.annotation.Generated',
|
|
37
|
-
// Apollo Android
|
|
38
|
-
ScalarType: 'com.apollographql.apollo.api.ScalarType',
|
|
39
|
-
GraphqlFragment: 'com.apollographql.apollo.api.GraphqlFragment',
|
|
40
|
-
Operation: 'com.apollographql.apollo.api.Operation',
|
|
41
|
-
OperationName: 'com.apollographql.apollo.api.OperationName',
|
|
42
|
-
Mutation: 'com.apollographql.apollo.api.Mutation',
|
|
43
|
-
Query: 'com.apollographql.apollo.api.Query',
|
|
44
|
-
Subscription: 'com.apollographql.apollo.api.Subscription',
|
|
45
|
-
ResponseField: 'com.apollographql.apollo.api.ResponseField',
|
|
46
|
-
ResponseFieldMapper: 'com.apollographql.apollo.api.ResponseFieldMapper',
|
|
47
|
-
ResponseFieldMarshaller: 'com.apollographql.apollo.api.ResponseFieldMarshaller',
|
|
48
|
-
ResponseReader: 'com.apollographql.apollo.api.ResponseReader',
|
|
49
|
-
ResponseWriter: 'com.apollographql.apollo.api.ResponseWriter',
|
|
50
|
-
FragmentResponseFieldMapper: 'com.apollographql.apollo.api.FragmentResponseFieldMapper',
|
|
51
|
-
UnmodifiableMapBuilder: 'com.apollographql.apollo.api.internal.UnmodifiableMapBuilder',
|
|
52
|
-
Utils: 'com.apollographql.apollo.api.internal.Utils',
|
|
53
|
-
InputType: 'com.apollographql.apollo.api.InputType',
|
|
54
|
-
Input: 'com.apollographql.apollo.api.Input',
|
|
55
|
-
InputFieldMarshaller: 'com.apollographql.apollo.api.InputFieldMarshaller',
|
|
56
|
-
InputFieldWriter: 'com.apollographql.apollo.api.InputFieldWriter',
|
|
57
|
-
};
|
|
58
|
-
|
|
59
|
-
const SCALAR_TO_WRITER_METHOD = {
|
|
60
|
-
ID: 'writeString',
|
|
61
|
-
String: 'writeString',
|
|
62
|
-
Int: 'writeInt',
|
|
63
|
-
Boolean: 'writeBoolean',
|
|
64
|
-
Float: 'writeDouble',
|
|
65
|
-
};
|
|
66
|
-
function isTypeNode(type) {
|
|
67
|
-
return type && !!type.kind;
|
|
68
|
-
}
|
|
69
|
-
class BaseJavaVisitor extends visitorPluginCommon.BaseVisitor {
|
|
70
|
-
constructor(_schema, rawConfig, additionalConfig) {
|
|
71
|
-
super(rawConfig, {
|
|
72
|
-
...additionalConfig,
|
|
73
|
-
scalars: visitorPluginCommon.buildScalars(_schema, { ID: 'String' }, javaCommon.JAVA_SCALARS),
|
|
74
|
-
});
|
|
75
|
-
this._schema = _schema;
|
|
76
|
-
this._imports = new Set();
|
|
77
|
-
}
|
|
78
|
-
getPackage() {
|
|
79
|
-
return '';
|
|
80
|
-
}
|
|
81
|
-
additionalContent() {
|
|
82
|
-
return '';
|
|
83
|
-
}
|
|
84
|
-
getImports() {
|
|
85
|
-
return Array.from(this._imports).map(imp => `import ${imp};`);
|
|
86
|
-
}
|
|
87
|
-
getImplementingTypes(node) {
|
|
88
|
-
const allTypesMap = this._schema.getTypeMap();
|
|
89
|
-
const implementingTypes = [];
|
|
90
|
-
for (const graphqlType of Object.values(allTypesMap)) {
|
|
91
|
-
if (graphqlType instanceof graphql.GraphQLObjectType) {
|
|
92
|
-
const allInterfaces = graphqlType.getInterfaces();
|
|
93
|
-
if (allInterfaces.find(int => int.name === node.name)) {
|
|
94
|
-
implementingTypes.push(graphqlType.name);
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
return implementingTypes;
|
|
99
|
-
}
|
|
100
|
-
transformType(type) {
|
|
101
|
-
let schemaType;
|
|
102
|
-
let isNonNull;
|
|
103
|
-
if (isTypeNode(type)) {
|
|
104
|
-
const baseTypeNode = visitorPluginCommon.getBaseTypeNode(type);
|
|
105
|
-
schemaType = this._schema.getType(baseTypeNode.name.value);
|
|
106
|
-
isNonNull = type.kind === graphql.Kind.NON_NULL_TYPE;
|
|
107
|
-
}
|
|
108
|
-
else {
|
|
109
|
-
schemaType = this._schema.getType(pluginHelpers.getBaseType(type).name);
|
|
110
|
-
isNonNull = graphql.isNonNullType(type);
|
|
111
|
-
}
|
|
112
|
-
const javaType = this.getJavaClass(schemaType);
|
|
113
|
-
const annotation = isNonNull ? 'Nonnull' : 'Nullable';
|
|
114
|
-
const typeToUse = isTypeNode(type)
|
|
115
|
-
? this.getListTypeNodeWrapped(javaType, type)
|
|
116
|
-
: this.getListTypeWrapped(javaType, type);
|
|
117
|
-
return {
|
|
118
|
-
baseType: schemaType.name,
|
|
119
|
-
javaType,
|
|
120
|
-
isNonNull,
|
|
121
|
-
annotation,
|
|
122
|
-
typeToUse,
|
|
123
|
-
};
|
|
124
|
-
}
|
|
125
|
-
// Replaces a GraphQL type with a Java class
|
|
126
|
-
getJavaClass(schemaType) {
|
|
127
|
-
let typeToUse = schemaType.name;
|
|
128
|
-
if (graphql.isScalarType(schemaType)) {
|
|
129
|
-
const scalar = this.scalars[schemaType.name] || 'Object';
|
|
130
|
-
if (Imports[scalar]) {
|
|
131
|
-
this._imports.add(Imports[scalar]);
|
|
132
|
-
}
|
|
133
|
-
typeToUse = scalar;
|
|
134
|
-
}
|
|
135
|
-
else if (graphql.isInputObjectType(schemaType)) {
|
|
136
|
-
// Make sure to import it if it's in use
|
|
137
|
-
this._imports.add(`${this.config.typePackage}.${schemaType.name}`);
|
|
138
|
-
}
|
|
139
|
-
return typeToUse;
|
|
140
|
-
}
|
|
141
|
-
getListTypeWrapped(toWrap, type) {
|
|
142
|
-
if (graphql.isNonNullType(type)) {
|
|
143
|
-
return this.getListTypeWrapped(toWrap, type.ofType);
|
|
144
|
-
}
|
|
145
|
-
if (graphql.isListType(type)) {
|
|
146
|
-
const child = this.getListTypeWrapped(toWrap, type.ofType);
|
|
147
|
-
this._imports.add(Imports.List);
|
|
148
|
-
return `List<${child}>`;
|
|
149
|
-
}
|
|
150
|
-
return toWrap;
|
|
151
|
-
}
|
|
152
|
-
getListTypeNodeWrapped(toWrap, type) {
|
|
153
|
-
if (type.kind === graphql.Kind.NON_NULL_TYPE) {
|
|
154
|
-
return this.getListTypeNodeWrapped(toWrap, type.type);
|
|
155
|
-
}
|
|
156
|
-
if (type.kind === graphql.Kind.LIST_TYPE) {
|
|
157
|
-
const child = this.getListTypeNodeWrapped(toWrap, type.type);
|
|
158
|
-
this._imports.add(Imports.List);
|
|
159
|
-
return `List<${child}>`;
|
|
160
|
-
}
|
|
161
|
-
return toWrap;
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
class InputTypeVisitor extends BaseJavaVisitor {
|
|
166
|
-
constructor(_schema, rawConfig) {
|
|
167
|
-
super(_schema, rawConfig, {
|
|
168
|
-
typePackage: rawConfig.typePackage || 'type',
|
|
169
|
-
});
|
|
170
|
-
}
|
|
171
|
-
getPackage() {
|
|
172
|
-
return this.config.typePackage;
|
|
173
|
-
}
|
|
174
|
-
addInputMembers(cls, fields) {
|
|
175
|
-
fields.forEach(field => {
|
|
176
|
-
const type = this.transformType(field.type);
|
|
177
|
-
const actualType = type.isNonNull ? type.typeToUse : `Input<${type.typeToUse}>`;
|
|
178
|
-
const annotations = type.isNonNull ? [type.annotation] : [];
|
|
179
|
-
this._imports.add(Imports[type.annotation]);
|
|
180
|
-
cls.addClassMember(field.name.value, actualType, null, annotations, 'private', { final: true });
|
|
181
|
-
cls.addClassMethod(field.name.value, actualType, `return this.${field.name.value};`, [], [type.annotation], 'public');
|
|
182
|
-
});
|
|
183
|
-
}
|
|
184
|
-
addInputCtor(cls, className, fields) {
|
|
185
|
-
const impl = fields.map(field => `this.${field.name.value} = ${field.name.value};`).join('\n');
|
|
186
|
-
cls.addClassMethod(className, null, impl, fields.map(f => {
|
|
187
|
-
const type = this.transformType(f.type);
|
|
188
|
-
const actualType = type.isNonNull ? type.typeToUse : `Input<${type.typeToUse}>`;
|
|
189
|
-
this._imports.add(Imports[type.annotation]);
|
|
190
|
-
return {
|
|
191
|
-
name: f.name.value,
|
|
192
|
-
type: actualType,
|
|
193
|
-
annotations: type.isNonNull ? [type.annotation] : [],
|
|
194
|
-
};
|
|
195
|
-
}), [], 'public');
|
|
196
|
-
}
|
|
197
|
-
getFieldWriterCall(field, listItemCall = false) {
|
|
198
|
-
const baseType = visitorPluginCommon.getBaseTypeNode(field.type);
|
|
199
|
-
const schemaType = this._schema.getType(baseType.name.value);
|
|
200
|
-
const isNonNull = field.type.kind === graphql.Kind.NON_NULL_TYPE;
|
|
201
|
-
let writerMethod = null;
|
|
202
|
-
if (graphql.isScalarType(schemaType)) {
|
|
203
|
-
writerMethod = SCALAR_TO_WRITER_METHOD[schemaType.name] || 'writeCustom';
|
|
204
|
-
}
|
|
205
|
-
else if (graphql.isInputObjectType(schemaType)) {
|
|
206
|
-
return listItemCall
|
|
207
|
-
? `writeObject($item.marshaller())`
|
|
208
|
-
: `writeObject("${field.name.value}", ${field.name.value}.value != null ? ${field.name.value}.value.marshaller() : null)`;
|
|
209
|
-
}
|
|
210
|
-
else if (graphql.isEnumType(schemaType)) {
|
|
211
|
-
writerMethod = 'writeString';
|
|
212
|
-
}
|
|
213
|
-
return listItemCall
|
|
214
|
-
? `${writerMethod}($item)`
|
|
215
|
-
: `${writerMethod}("${field.name.value}", ${field.name.value}${isNonNull ? '' : '.value'})`;
|
|
216
|
-
}
|
|
217
|
-
getFieldWithTypePrefix(field, wrapWith = null, applyNullable = false) {
|
|
218
|
-
this._imports.add(Imports.Input);
|
|
219
|
-
const typeToUse = this.getJavaClass(this._schema.getType(visitorPluginCommon.getBaseTypeNode(field.type).name.value));
|
|
220
|
-
const isNonNull = field.type.kind === graphql.Kind.NON_NULL_TYPE;
|
|
221
|
-
const name = field.kind === graphql.Kind.INPUT_VALUE_DEFINITION ? field.name.value : field.variable.name.value;
|
|
222
|
-
if (isNonNull) {
|
|
223
|
-
this._imports.add(Imports.Nonnull);
|
|
224
|
-
return `@Nonnull ${typeToUse} ${name}`;
|
|
225
|
-
}
|
|
226
|
-
if (wrapWith) {
|
|
227
|
-
return typeof wrapWith === 'function' ? `${wrapWith(typeToUse)} ${name}` : `${wrapWith}<${typeToUse}> ${name}`;
|
|
228
|
-
}
|
|
229
|
-
if (applyNullable) {
|
|
230
|
-
this._imports.add(Imports.Nullable);
|
|
231
|
-
}
|
|
232
|
-
return `${applyNullable ? '@Nullable ' : ''}${typeToUse} ${name}`;
|
|
233
|
-
}
|
|
234
|
-
buildFieldsMarshaller(field) {
|
|
235
|
-
const isNonNull = field.type.kind === graphql.Kind.NON_NULL_TYPE;
|
|
236
|
-
const isArray = field.type.kind === graphql.Kind.LIST_TYPE ||
|
|
237
|
-
(field.type.kind === graphql.Kind.NON_NULL_TYPE && field.type.type.kind === graphql.Kind.LIST_TYPE);
|
|
238
|
-
const call = this.getFieldWriterCall(field, isArray);
|
|
239
|
-
const baseTypeNode = visitorPluginCommon.getBaseTypeNode(field.type);
|
|
240
|
-
const listItemType = this.getJavaClass(this._schema.getType(baseTypeNode.name.value));
|
|
241
|
-
let result = '';
|
|
242
|
-
// TODO: Refactor
|
|
243
|
-
if (isArray) {
|
|
244
|
-
result = `writer.writeList("${field.name.value}", ${field.name.value}.value != null ? new InputFieldWriter.ListWriter() {
|
|
245
|
-
@Override
|
|
246
|
-
public void write(InputFieldWriter.ListItemWriter listItemWriter) throws IOException {
|
|
247
|
-
for (${listItemType} $item : ${field.name.value}.value) {
|
|
248
|
-
listItemWriter.${call};
|
|
249
|
-
}
|
|
250
|
-
}
|
|
251
|
-
} : null);`;
|
|
252
|
-
}
|
|
253
|
-
else {
|
|
254
|
-
result = visitorPluginCommon.indent(`writer.${call};`);
|
|
255
|
-
}
|
|
256
|
-
if (isNonNull) {
|
|
257
|
-
return result;
|
|
258
|
-
}
|
|
259
|
-
return visitorPluginCommon.indentMultiline(`if(${field.name.value}.defined) {
|
|
260
|
-
${visitorPluginCommon.indentMultiline(result)}
|
|
261
|
-
}`);
|
|
262
|
-
}
|
|
263
|
-
buildMarshallerOverride(fields) {
|
|
264
|
-
this._imports.add(Imports.Override);
|
|
265
|
-
this._imports.add(Imports.IOException);
|
|
266
|
-
this._imports.add(Imports.InputFieldWriter);
|
|
267
|
-
this._imports.add(Imports.InputFieldMarshaller);
|
|
268
|
-
const allMarshallers = fields.map(field => visitorPluginCommon.indentMultiline(this.buildFieldsMarshaller(field), 2));
|
|
269
|
-
return visitorPluginCommon.indentMultiline(`@Override
|
|
270
|
-
public InputFieldMarshaller marshaller() {
|
|
271
|
-
return new InputFieldMarshaller() {
|
|
272
|
-
@Override
|
|
273
|
-
public void marshal(InputFieldWriter writer) throws IOException {
|
|
274
|
-
${allMarshallers.join('\n')}
|
|
275
|
-
}
|
|
276
|
-
};
|
|
277
|
-
}`);
|
|
278
|
-
}
|
|
279
|
-
buildBuilderNestedClass(className, fields) {
|
|
280
|
-
const builderClassName = 'Builder';
|
|
281
|
-
const privateFields = fields
|
|
282
|
-
.map(field => {
|
|
283
|
-
const isArray = field.type.kind === graphql.Kind.LIST_TYPE ||
|
|
284
|
-
(field.type.kind === graphql.Kind.NON_NULL_TYPE && field.type.type.kind === graphql.Kind.LIST_TYPE);
|
|
285
|
-
const fieldType = this.getFieldWithTypePrefix(field, v => (!isArray ? `Input<${v}>` : `Input<List<${v}>>`));
|
|
286
|
-
const isNonNull = field.type.kind === graphql.Kind.NON_NULL_TYPE;
|
|
287
|
-
return `private ${fieldType}${isNonNull ? '' : ' = Input.absent()'};`;
|
|
288
|
-
})
|
|
289
|
-
.map(s => visitorPluginCommon.indent(s));
|
|
290
|
-
const setters = fields
|
|
291
|
-
.map(field => {
|
|
292
|
-
const isArray = field.type.kind === graphql.Kind.LIST_TYPE ||
|
|
293
|
-
(field.type.kind === graphql.Kind.NON_NULL_TYPE && field.type.type.kind === graphql.Kind.LIST_TYPE);
|
|
294
|
-
const fieldType = this.getFieldWithTypePrefix(field, isArray ? 'List' : null);
|
|
295
|
-
const isNonNull = field.type.kind === graphql.Kind.NON_NULL_TYPE;
|
|
296
|
-
return `\npublic ${builderClassName} ${field.name.value}(${isNonNull ? '' : '@Nullable '}${fieldType}) {
|
|
297
|
-
this.${field.name.value} = ${isNonNull ? field.name.value : `Input.fromNullable(${field.name.value})`};
|
|
298
|
-
return this;
|
|
299
|
-
}`;
|
|
300
|
-
})
|
|
301
|
-
.map(s => visitorPluginCommon.indentMultiline(s));
|
|
302
|
-
const nonNullFields = fields
|
|
303
|
-
.filter(f => f.type.kind === graphql.Kind.NON_NULL_TYPE)
|
|
304
|
-
.map(nnField => {
|
|
305
|
-
this._imports.add(Imports.Utils);
|
|
306
|
-
return visitorPluginCommon.indent(`Utils.checkNotNull(${nnField.name.value}, "${nnField.name.value} == null");`, 1);
|
|
307
|
-
});
|
|
308
|
-
const ctor = '\n' + visitorPluginCommon.indent(`${builderClassName}() {}`);
|
|
309
|
-
const buildFn = visitorPluginCommon.indentMultiline(`public ${className} build() {
|
|
310
|
-
${nonNullFields.join('\n')}
|
|
311
|
-
return new ${className}(${fields.map(f => f.name.value).join(', ')});
|
|
312
|
-
}`);
|
|
313
|
-
const body = [...privateFields, ctor, ...setters, '', buildFn].join('\n');
|
|
314
|
-
return visitorPluginCommon.indentMultiline(new javaCommon.JavaDeclarationBlock()
|
|
315
|
-
.withName(builderClassName)
|
|
316
|
-
.access('public')
|
|
317
|
-
.final()
|
|
318
|
-
.static()
|
|
319
|
-
.withBlock(body)
|
|
320
|
-
.asKind('class').string);
|
|
321
|
-
}
|
|
322
|
-
InputObjectTypeDefinition(node) {
|
|
323
|
-
const className = node.name.value;
|
|
324
|
-
this._imports.add(Imports.InputType);
|
|
325
|
-
this._imports.add(Imports.Generated);
|
|
326
|
-
const cls = new javaCommon.JavaDeclarationBlock()
|
|
327
|
-
.annotate([`Generated("Apollo GraphQL")`])
|
|
328
|
-
.access('public')
|
|
329
|
-
.final()
|
|
330
|
-
.asKind('class')
|
|
331
|
-
.withName(className)
|
|
332
|
-
.implements(['InputType']);
|
|
333
|
-
this.addInputMembers(cls, node.fields);
|
|
334
|
-
this.addInputCtor(cls, className, node.fields);
|
|
335
|
-
cls.addClassMethod('builder', 'Builder', 'return new Builder();', [], [], 'public', { static: true });
|
|
336
|
-
const marshallerOverride = this.buildMarshallerOverride(node.fields);
|
|
337
|
-
const builderClass = this.buildBuilderNestedClass(className, node.fields);
|
|
338
|
-
const classBlock = [marshallerOverride, '', builderClass].join('\n');
|
|
339
|
-
cls.withBlock(classBlock);
|
|
340
|
-
return cls.string;
|
|
341
|
-
}
|
|
342
|
-
}
|
|
343
|
-
|
|
344
|
-
function visitFieldArguments(selection, imports) {
|
|
345
|
-
if (!selection.arguments || selection.arguments.length === 0) {
|
|
346
|
-
return 'null';
|
|
347
|
-
}
|
|
348
|
-
imports.add(Imports.UnmodifiableMapBuilder);
|
|
349
|
-
imports.add(Imports.String);
|
|
350
|
-
imports.add(Imports.Object);
|
|
351
|
-
return pluginHelpers.oldVisit(selection, {
|
|
352
|
-
leave: {
|
|
353
|
-
Field: (node) => {
|
|
354
|
-
return (`new UnmodifiableMapBuilder<String, Object>(${node.arguments.length})` + node.arguments.join('') + '.build()');
|
|
355
|
-
},
|
|
356
|
-
Argument: (node) => {
|
|
357
|
-
return `.put("${node.name.value}", ${node.value})`;
|
|
358
|
-
},
|
|
359
|
-
ObjectValue: (node) => {
|
|
360
|
-
return `new UnmodifiableMapBuilder<String, Object>(${node.fields.length})` + node.fields.join('') + '.build()';
|
|
361
|
-
},
|
|
362
|
-
ObjectField: (node) => {
|
|
363
|
-
return `.put("${node.name.value}", ${node.value})`;
|
|
364
|
-
},
|
|
365
|
-
Variable: (node) => {
|
|
366
|
-
return `new UnmodifiableMapBuilder<String, Object>(2).put("kind", "Variable").put("variableName", "${node.name.value}").build()`;
|
|
367
|
-
},
|
|
368
|
-
StringValue: (node) => `"${node.value}"`,
|
|
369
|
-
IntValue: (node) => `"${node.value}"`,
|
|
370
|
-
FloatValue: (node) => `"${node.value}"`,
|
|
371
|
-
},
|
|
372
|
-
});
|
|
373
|
-
}
|
|
374
|
-
|
|
375
|
-
class OperationVisitor extends BaseJavaVisitor {
|
|
376
|
-
constructor(_schema, rawConfig, _availableFragments) {
|
|
377
|
-
super(_schema, rawConfig, {
|
|
378
|
-
package: rawConfig.package || javaCommon.buildPackageNameFromPath(process.cwd()),
|
|
379
|
-
fragmentPackage: rawConfig.fragmentPackage || 'fragment',
|
|
380
|
-
typePackage: rawConfig.typePackage || 'type',
|
|
381
|
-
});
|
|
382
|
-
this._availableFragments = _availableFragments;
|
|
383
|
-
this.visitingFragment = false;
|
|
384
|
-
}
|
|
385
|
-
printDocument(node) {
|
|
386
|
-
return graphql.print(node)
|
|
387
|
-
.replace(/\r?\n|\r/g, ' ')
|
|
388
|
-
.replace(/"/g, '\\"')
|
|
389
|
-
.trim();
|
|
390
|
-
}
|
|
391
|
-
getPackage() {
|
|
392
|
-
return this.visitingFragment ? this.config.fragmentPackage : this.config.package;
|
|
393
|
-
}
|
|
394
|
-
addCtor(className, node, cls) {
|
|
395
|
-
const variables = node.variableDefinitions || [];
|
|
396
|
-
const hasVariables = variables.length > 0;
|
|
397
|
-
const nonNullVariables = variables
|
|
398
|
-
.filter(v => v.type.kind === graphql.Kind.NON_NULL_TYPE)
|
|
399
|
-
.map(v => {
|
|
400
|
-
this._imports.add(Imports.Utils);
|
|
401
|
-
return `Utils.checkNotNull(${v.variable.name.value}, "${v.variable.name.value} == null");`;
|
|
402
|
-
});
|
|
403
|
-
const impl = [
|
|
404
|
-
...nonNullVariables,
|
|
405
|
-
`this.variables = ${!hasVariables
|
|
406
|
-
? 'Operation.EMPTY_VARIABLES'
|
|
407
|
-
: `new ${className}.Variables(${variables.map(v => v.variable.name.value).join(', ')})`};`,
|
|
408
|
-
].join('\n');
|
|
409
|
-
cls.addClassMethod(className, null, impl, node.variableDefinitions.map(varDec => {
|
|
410
|
-
const outputType = visitorPluginCommon.getBaseTypeNode(varDec.type).name.value;
|
|
411
|
-
const schemaType = this._schema.getType(outputType);
|
|
412
|
-
const javaClass = this.getJavaClass(schemaType);
|
|
413
|
-
const typeToUse = this.getListTypeNodeWrapped(javaClass, varDec.type);
|
|
414
|
-
const isNonNull = varDec.type.kind === graphql.Kind.NON_NULL_TYPE;
|
|
415
|
-
return {
|
|
416
|
-
name: varDec.variable.name.value,
|
|
417
|
-
type: typeToUse,
|
|
418
|
-
annotations: [isNonNull ? 'Nonnull' : 'Nullable'],
|
|
419
|
-
};
|
|
420
|
-
}), null, 'public');
|
|
421
|
-
}
|
|
422
|
-
getRootType(operation) {
|
|
423
|
-
if (operation === 'query') {
|
|
424
|
-
return this._schema.getQueryType();
|
|
425
|
-
}
|
|
426
|
-
if (operation === 'mutation') {
|
|
427
|
-
return this._schema.getMutationType();
|
|
428
|
-
}
|
|
429
|
-
if (operation === 'subscription') {
|
|
430
|
-
return this._schema.getSubscriptionType();
|
|
431
|
-
}
|
|
432
|
-
return null;
|
|
433
|
-
}
|
|
434
|
-
createUniqueClassName(inUse, name, count = 0) {
|
|
435
|
-
const possibleNewName = count === 0 ? name : `${name}${count}`;
|
|
436
|
-
if (inUse.includes(possibleNewName)) {
|
|
437
|
-
return this.createUniqueClassName(inUse, name, count + 1);
|
|
438
|
-
}
|
|
439
|
-
return possibleNewName;
|
|
440
|
-
}
|
|
441
|
-
transformSelectionSet(options, isRoot = true) {
|
|
442
|
-
if (!options.result) {
|
|
443
|
-
options.result = {};
|
|
444
|
-
}
|
|
445
|
-
if (!graphql.isObjectType(options.schemaType) && !graphql.isInterfaceType(options.schemaType)) {
|
|
446
|
-
return options.result;
|
|
447
|
-
}
|
|
448
|
-
const className = this.createUniqueClassName(Object.keys(options.result), options.className);
|
|
449
|
-
const cls = new javaCommon.JavaDeclarationBlock()
|
|
450
|
-
.access('public')
|
|
451
|
-
.asKind('class')
|
|
452
|
-
.withName(className)
|
|
453
|
-
.implements(options.implements || []);
|
|
454
|
-
if (!options.nonStaticClass) {
|
|
455
|
-
cls.static();
|
|
456
|
-
}
|
|
457
|
-
options.result[className] = cls;
|
|
458
|
-
const fields = options.schemaType.getFields();
|
|
459
|
-
const childFields = [...(options.additionalFields || [])];
|
|
460
|
-
const childInlineFragments = [];
|
|
461
|
-
const childFragmentSpread = [...(options.additionalFragments || [])];
|
|
462
|
-
const selections = [...(options.selectionSet || [])];
|
|
463
|
-
const responseFieldArr = [];
|
|
464
|
-
for (const selection of selections) {
|
|
465
|
-
if (selection.kind === graphql.Kind.FIELD) {
|
|
466
|
-
this._imports.add(Imports.ResponseField);
|
|
467
|
-
const field = fields[selection.name.value];
|
|
468
|
-
const isObject = selection.selectionSet && selection.selectionSet.selections && selection.selectionSet.selections.length > 0;
|
|
469
|
-
const isNonNull = graphql.isNonNullType(field.type);
|
|
470
|
-
const fieldAnnotation = isNonNull ? 'Nonnull' : 'Nullable';
|
|
471
|
-
this._imports.add(Imports[fieldAnnotation]);
|
|
472
|
-
const baseType = pluginHelpers.getBaseType(field.type);
|
|
473
|
-
const isList = graphql.isListType(field.type) || (graphql.isNonNullType(field.type) && graphql.isListType(field.type.ofType));
|
|
474
|
-
if (isObject) {
|
|
475
|
-
let childClsName = this.convertName(field.name);
|
|
476
|
-
if (isList && pluralize.isPlural(childClsName)) {
|
|
477
|
-
childClsName = pluralize.singular(childClsName);
|
|
478
|
-
}
|
|
479
|
-
this.transformSelectionSet({
|
|
480
|
-
className: childClsName,
|
|
481
|
-
result: options.result,
|
|
482
|
-
selectionSet: selection.selectionSet.selections,
|
|
483
|
-
schemaType: baseType,
|
|
484
|
-
}, false);
|
|
485
|
-
childFields.push({
|
|
486
|
-
rawType: field.type,
|
|
487
|
-
isObject: true,
|
|
488
|
-
isList,
|
|
489
|
-
isFragment: false,
|
|
490
|
-
type: baseType,
|
|
491
|
-
isNonNull,
|
|
492
|
-
annotation: fieldAnnotation,
|
|
493
|
-
className: childClsName,
|
|
494
|
-
fieldName: field.name,
|
|
495
|
-
});
|
|
496
|
-
}
|
|
497
|
-
else {
|
|
498
|
-
const javaClass = this.getJavaClass(baseType);
|
|
499
|
-
childFields.push({
|
|
500
|
-
rawType: field.type,
|
|
501
|
-
isObject: false,
|
|
502
|
-
isFragment: false,
|
|
503
|
-
isList,
|
|
504
|
-
type: baseType,
|
|
505
|
-
isNonNull,
|
|
506
|
-
annotation: fieldAnnotation,
|
|
507
|
-
className: javaClass,
|
|
508
|
-
fieldName: field.name,
|
|
509
|
-
});
|
|
510
|
-
}
|
|
511
|
-
this._imports.add(Imports.ResponseField);
|
|
512
|
-
this._imports.add(Imports.Collections);
|
|
513
|
-
const operationArgs = visitFieldArguments(selection, this._imports);
|
|
514
|
-
const responseFieldMethod = this._resolveResponseFieldMethodForBaseType(field.type);
|
|
515
|
-
responseFieldArr.push(`ResponseField.${responseFieldMethod.fn}("${selection.alias ? selection.alias.value : selection.name.value}", "${selection.name.value}", ${operationArgs}, ${!graphql.isNonNullType(field.type)},${responseFieldMethod.custom ? ` CustomType.${baseType.name},` : ''} Collections.<ResponseField.Condition>emptyList())`);
|
|
516
|
-
}
|
|
517
|
-
else if (selection.kind === graphql.Kind.INLINE_FRAGMENT) {
|
|
518
|
-
if (graphql.isUnionType(options.schemaType) || graphql.isInterfaceType(options.schemaType)) {
|
|
519
|
-
childInlineFragments.push({
|
|
520
|
-
onType: selection.typeCondition.name.value,
|
|
521
|
-
node: selection,
|
|
522
|
-
});
|
|
523
|
-
}
|
|
524
|
-
else {
|
|
525
|
-
selections.push(...selection.selectionSet.selections);
|
|
526
|
-
}
|
|
527
|
-
}
|
|
528
|
-
else if (selection.kind === graphql.Kind.FRAGMENT_SPREAD) {
|
|
529
|
-
const fragment = this._availableFragments.find(f => f.name === selection.name.value);
|
|
530
|
-
if (fragment) {
|
|
531
|
-
childFragmentSpread.push(fragment);
|
|
532
|
-
this._imports.add(`${this.config.fragmentPackage}.${fragment.name}`);
|
|
533
|
-
}
|
|
534
|
-
else {
|
|
535
|
-
throw new Error(`Fragment with name ${selection.name.value} was not loaded as document!`);
|
|
536
|
-
}
|
|
537
|
-
}
|
|
538
|
-
}
|
|
539
|
-
if (childInlineFragments.length > 0) {
|
|
540
|
-
const childFieldsBase = [...childFields];
|
|
541
|
-
childFields.push(...childInlineFragments.map(inlineFragment => {
|
|
542
|
-
const cls = `As${inlineFragment.onType}`;
|
|
543
|
-
const schemaType = this._schema.getType(inlineFragment.onType);
|
|
544
|
-
this.transformSelectionSet({
|
|
545
|
-
additionalFields: childFieldsBase,
|
|
546
|
-
additionalFragments: childFragmentSpread,
|
|
547
|
-
className: cls,
|
|
548
|
-
result: options.result,
|
|
549
|
-
selectionSet: inlineFragment.node.selectionSet.selections,
|
|
550
|
-
schemaType,
|
|
551
|
-
}, false);
|
|
552
|
-
this._imports.add(Imports.Nullable);
|
|
553
|
-
return {
|
|
554
|
-
isFragment: false,
|
|
555
|
-
rawType: schemaType,
|
|
556
|
-
isObject: true,
|
|
557
|
-
isList: false,
|
|
558
|
-
type: schemaType,
|
|
559
|
-
isNonNull: false,
|
|
560
|
-
annotation: 'Nullable',
|
|
561
|
-
className: cls,
|
|
562
|
-
fieldName: `as${inlineFragment.onType}`,
|
|
563
|
-
};
|
|
564
|
-
}));
|
|
565
|
-
responseFieldArr.push(...childInlineFragments.map(f => {
|
|
566
|
-
this._imports.add(Imports.Arrays);
|
|
567
|
-
return `ResponseField.forInlineFragment("__typename", "__typename", Arrays.asList("${f.onType}"))`;
|
|
568
|
-
}));
|
|
569
|
-
}
|
|
570
|
-
if (childFragmentSpread.length > 0) {
|
|
571
|
-
responseFieldArr.push(`ResponseField.forFragment("__typename", "__typename", Arrays.asList(${childFragmentSpread
|
|
572
|
-
.map(f => `"${f.onType}"`)
|
|
573
|
-
.join(', ')}))`);
|
|
574
|
-
this._imports.add(Imports.ResponseField);
|
|
575
|
-
this._imports.add(Imports.Nonnull);
|
|
576
|
-
this._imports.add(Imports.Arrays);
|
|
577
|
-
const fragmentsClassName = 'Fragments';
|
|
578
|
-
childFields.push({
|
|
579
|
-
isObject: true,
|
|
580
|
-
isList: false,
|
|
581
|
-
isFragment: true,
|
|
582
|
-
rawType: options.schemaType,
|
|
583
|
-
type: options.schemaType,
|
|
584
|
-
isNonNull: true,
|
|
585
|
-
annotation: 'Nonnull',
|
|
586
|
-
className: fragmentsClassName,
|
|
587
|
-
fieldName: 'fragments',
|
|
588
|
-
});
|
|
589
|
-
const fragmentsClass = new javaCommon.JavaDeclarationBlock()
|
|
590
|
-
.withName(fragmentsClassName)
|
|
591
|
-
.access('public')
|
|
592
|
-
.static()
|
|
593
|
-
.final()
|
|
594
|
-
.asKind('class');
|
|
595
|
-
const fragmentMapperClass = new javaCommon.JavaDeclarationBlock()
|
|
596
|
-
.withName('Mapper')
|
|
597
|
-
.access('public')
|
|
598
|
-
.static()
|
|
599
|
-
.final()
|
|
600
|
-
.implements([`FragmentResponseFieldMapper<${fragmentsClassName}>`])
|
|
601
|
-
.asKind('class');
|
|
602
|
-
fragmentsClass.addClassMethod(fragmentsClassName, null, childFragmentSpread
|
|
603
|
-
.map(spread => {
|
|
604
|
-
const varName = changeCaseAll.camelCase(spread.name);
|
|
605
|
-
this._imports.add(Imports.Utils);
|
|
606
|
-
return `this.${varName} = Utils.checkNotNull(${varName}, "${varName} == null");`;
|
|
607
|
-
})
|
|
608
|
-
.join('\n'), childFragmentSpread.map(spread => ({
|
|
609
|
-
name: changeCaseAll.camelCase(spread.name),
|
|
610
|
-
type: spread.name,
|
|
611
|
-
annotations: ['Nonnull'],
|
|
612
|
-
})), [], 'public');
|
|
613
|
-
for (const spread of childFragmentSpread) {
|
|
614
|
-
const fragmentVarName = changeCaseAll.camelCase(spread.name);
|
|
615
|
-
fragmentsClass.addClassMember(fragmentVarName, spread.name, null, ['Nonnull'], 'private', { final: true });
|
|
616
|
-
fragmentsClass.addClassMethod(fragmentVarName, spread.name, `return this.${fragmentVarName};`, [], ['Nonnull'], 'public', {}, []);
|
|
617
|
-
fragmentMapperClass.addClassMember(`${fragmentVarName}FieldMapper`, `${spread.name}.Mapper`, `new ${spread.name}.Mapper()`, [], 'private', { final: true });
|
|
618
|
-
}
|
|
619
|
-
fragmentMapperClass.addClassMethod('map', fragmentsClassName, `
|
|
620
|
-
${childFragmentSpread
|
|
621
|
-
.map(spread => {
|
|
622
|
-
const fragmentVarName = changeCaseAll.camelCase(spread.name);
|
|
623
|
-
return `${spread.name} ${fragmentVarName} = null;
|
|
624
|
-
if (${spread.name}.POSSIBLE_TYPES.contains(conditionalType)) {
|
|
625
|
-
${fragmentVarName} = ${fragmentVarName}FieldMapper.map(reader);
|
|
626
|
-
}`;
|
|
627
|
-
})
|
|
628
|
-
.join('\n')}
|
|
629
|
-
|
|
630
|
-
return new Fragments(${childFragmentSpread
|
|
631
|
-
.map(spread => {
|
|
632
|
-
const fragmentVarName = changeCaseAll.camelCase(spread.name);
|
|
633
|
-
return `Utils.checkNotNull(${fragmentVarName}, "${fragmentVarName} == null")`;
|
|
634
|
-
})
|
|
635
|
-
.join(', ')});
|
|
636
|
-
`, [
|
|
637
|
-
{
|
|
638
|
-
name: 'reader',
|
|
639
|
-
type: 'ResponseReader',
|
|
640
|
-
},
|
|
641
|
-
{
|
|
642
|
-
name: 'conditionalType',
|
|
643
|
-
type: 'String',
|
|
644
|
-
annotations: ['Nonnull'],
|
|
645
|
-
},
|
|
646
|
-
], ['Nonnull'], 'public', {}, ['Override']);
|
|
647
|
-
this._imports.add(Imports.String);
|
|
648
|
-
this._imports.add(Imports.ResponseReader);
|
|
649
|
-
this._imports.add(Imports.ResponseFieldMarshaller);
|
|
650
|
-
this._imports.add(Imports.ResponseWriter);
|
|
651
|
-
fragmentsClass.addClassMethod('marshaller', 'ResponseFieldMarshaller', `return new ResponseFieldMarshaller() {
|
|
652
|
-
@Override
|
|
653
|
-
public void marshal(ResponseWriter writer) {
|
|
654
|
-
${childFragmentSpread
|
|
655
|
-
.map(spread => {
|
|
656
|
-
const fragmentVarName = changeCaseAll.camelCase(spread.name);
|
|
657
|
-
return visitorPluginCommon.indentMultiline(`final ${spread.name} $${fragmentVarName} = ${fragmentVarName};\nif ($${fragmentVarName} != null) { $${fragmentVarName}.marshaller().marshal(writer); }`, 2);
|
|
658
|
-
})
|
|
659
|
-
.join('\n')}
|
|
660
|
-
}
|
|
661
|
-
};
|
|
662
|
-
`, [], [], 'public');
|
|
663
|
-
fragmentsClass.addClassMember('$toString', 'String', null, [], 'private', { volatile: true });
|
|
664
|
-
fragmentsClass.addClassMember('$hashCode', 'int', null, [], 'private', { volatile: true });
|
|
665
|
-
fragmentsClass.addClassMember('$hashCodeMemoized', 'boolean', null, [], 'private', { volatile: true });
|
|
666
|
-
fragmentsClass.addClassMethod('toString', 'String', `if ($toString == null) {
|
|
667
|
-
$toString = "${fragmentsClassName}{"
|
|
668
|
-
${childFragmentSpread
|
|
669
|
-
.map(spread => {
|
|
670
|
-
const varName = changeCaseAll.camelCase(spread.name);
|
|
671
|
-
return visitorPluginCommon.indent(`+ "${varName}=" + ${varName} + ", "`, 2);
|
|
672
|
-
})
|
|
673
|
-
.join('\n')}
|
|
674
|
-
+ "}";
|
|
675
|
-
}
|
|
676
|
-
|
|
677
|
-
return $toString;`, [], [], 'public', {}, ['Override']);
|
|
678
|
-
// Add equals
|
|
679
|
-
fragmentsClass.addClassMethod('equals', 'boolean', `if (o == this) {
|
|
680
|
-
return true;
|
|
681
|
-
}
|
|
682
|
-
if (o instanceof ${fragmentsClassName}) {
|
|
683
|
-
${fragmentsClassName} that = (${fragmentsClassName}) o;
|
|
684
|
-
return ${childFragmentSpread
|
|
685
|
-
.map(spread => {
|
|
686
|
-
const varName = changeCaseAll.camelCase(spread.name);
|
|
687
|
-
return `this.${varName}.equals(that.${varName})`;
|
|
688
|
-
})
|
|
689
|
-
.join(' && ')};
|
|
690
|
-
}
|
|
691
|
-
|
|
692
|
-
return false;`, [{ name: 'o', type: 'Object' }], [], 'public', {}, ['Override']);
|
|
693
|
-
// hashCode
|
|
694
|
-
fragmentsClass.addClassMethod('hashCode', 'int', `if (!$hashCodeMemoized) {
|
|
695
|
-
int h = 1;
|
|
696
|
-
${childFragmentSpread
|
|
697
|
-
.map(spread => {
|
|
698
|
-
const varName = changeCaseAll.camelCase(spread.name);
|
|
699
|
-
return visitorPluginCommon.indentMultiline(`h *= 1000003;\nh ^= ${varName}.hashCode();`, 1);
|
|
700
|
-
})
|
|
701
|
-
.join('\n')}
|
|
702
|
-
$hashCode = h;
|
|
703
|
-
$hashCodeMemoized = true;
|
|
704
|
-
}
|
|
705
|
-
|
|
706
|
-
return $hashCode;`, [], [], 'public', {}, ['Override']);
|
|
707
|
-
this._imports.add(Imports.FragmentResponseFieldMapper);
|
|
708
|
-
fragmentsClass.nestedClass(fragmentMapperClass);
|
|
709
|
-
cls.nestedClass(fragmentsClass);
|
|
710
|
-
}
|
|
711
|
-
if (responseFieldArr.length > 0 && !isRoot) {
|
|
712
|
-
responseFieldArr.unshift(`ResponseField.forString("__typename", "__typename", null, false, Collections.<ResponseField.Condition>emptyList())`);
|
|
713
|
-
}
|
|
714
|
-
if (!isRoot) {
|
|
715
|
-
this._imports.add(Imports.Nonnull);
|
|
716
|
-
childFields.unshift({
|
|
717
|
-
isObject: false,
|
|
718
|
-
isFragment: false,
|
|
719
|
-
isList: false,
|
|
720
|
-
type: graphql.GraphQLString,
|
|
721
|
-
rawType: graphql.GraphQLString,
|
|
722
|
-
isNonNull: true,
|
|
723
|
-
annotation: 'Nonnull',
|
|
724
|
-
className: 'String',
|
|
725
|
-
fieldName: '__typename',
|
|
726
|
-
});
|
|
727
|
-
}
|
|
728
|
-
// Add members
|
|
729
|
-
childFields.forEach(c => {
|
|
730
|
-
cls.addClassMember(c.fieldName, this.getListTypeWrapped(c.className, c.rawType), null, [c.annotation], 'private', { final: true });
|
|
731
|
-
});
|
|
732
|
-
// Add $toString, $hashCode, $hashCodeMemoized
|
|
733
|
-
cls.addClassMember('$toString', 'String', null, [], 'private', { volatile: true });
|
|
734
|
-
cls.addClassMember('$hashCode', 'int', null, [], 'private', { volatile: true });
|
|
735
|
-
cls.addClassMember('$hashCodeMemoized', 'boolean', null, [], 'private', { volatile: true });
|
|
736
|
-
// Add responseFields for all fields
|
|
737
|
-
cls.addClassMember('$responseFields', 'ResponseField[]', `{\n${visitorPluginCommon.indentMultiline(responseFieldArr.join(',\n'), 2) + '\n }'}`, [], null, { static: true, final: true });
|
|
738
|
-
// Add Ctor
|
|
739
|
-
this._imports.add(Imports.Utils);
|
|
740
|
-
cls.addClassMethod(className, null, childFields
|
|
741
|
-
.map(c => `this.${c.fieldName} = ${c.isNonNull ? `Utils.checkNotNull(${c.fieldName}, "${c.fieldName} == null")` : c.fieldName};`)
|
|
742
|
-
.join('\n'), childFields.map(c => ({
|
|
743
|
-
name: c.fieldName,
|
|
744
|
-
type: this.getListTypeWrapped(c.className, c.rawType),
|
|
745
|
-
annotations: [c.annotation],
|
|
746
|
-
})), null, 'public');
|
|
747
|
-
// Add getters for all members
|
|
748
|
-
childFields.forEach(c => {
|
|
749
|
-
cls.addClassMethod(c.fieldName, this.getListTypeWrapped(c.className, c.rawType), `return this.${c.fieldName};`, [], [c.annotation], 'public', {});
|
|
750
|
-
});
|
|
751
|
-
// Add .toString()
|
|
752
|
-
cls.addClassMethod('toString', 'String', `if ($toString == null) {
|
|
753
|
-
$toString = "${className}{"
|
|
754
|
-
${childFields.map(c => visitorPluginCommon.indent(`+ "${c.fieldName}=" + ${c.fieldName} + ", "`, 2)).join('\n')}
|
|
755
|
-
+ "}";
|
|
756
|
-
}
|
|
757
|
-
|
|
758
|
-
return $toString;`, [], [], 'public', {}, ['Override']);
|
|
759
|
-
// Add equals
|
|
760
|
-
cls.addClassMethod('equals', 'boolean', `if (o == this) {
|
|
761
|
-
return true;
|
|
762
|
-
}
|
|
763
|
-
if (o instanceof ${className}) {
|
|
764
|
-
${className} that = (${className}) o;
|
|
765
|
-
return ${childFields
|
|
766
|
-
.map(c => c.isNonNull
|
|
767
|
-
? `this.${c.fieldName}.equals(that.${c.fieldName})`
|
|
768
|
-
: `((this.${c.fieldName} == null) ? (that.${c.fieldName} == null) : this.${c.fieldName}.equals(that.${c.fieldName}))`)
|
|
769
|
-
.join(' && ')};
|
|
770
|
-
}
|
|
771
|
-
|
|
772
|
-
return false;`, [{ name: 'o', type: 'Object' }], [], 'public', {}, ['Override']);
|
|
773
|
-
// hashCode
|
|
774
|
-
cls.addClassMethod('hashCode', 'int', `if (!$hashCodeMemoized) {
|
|
775
|
-
int h = 1;
|
|
776
|
-
${childFields
|
|
777
|
-
.map(f => visitorPluginCommon.indentMultiline(`h *= 1000003;\nh ^= ${!f.isNonNull ? `(${f.fieldName} == null) ? 0 : ` : ''}${f.fieldName}.hashCode();`, 1))
|
|
778
|
-
.join('\n')}
|
|
779
|
-
$hashCode = h;
|
|
780
|
-
$hashCodeMemoized = true;
|
|
781
|
-
}
|
|
782
|
-
|
|
783
|
-
return $hashCode;`, [], [], 'public', {}, ['Override']);
|
|
784
|
-
this._imports.add(Imports.ResponseReader);
|
|
785
|
-
this._imports.add(Imports.ResponseFieldMarshaller);
|
|
786
|
-
this._imports.add(Imports.ResponseWriter);
|
|
787
|
-
// marshaller
|
|
788
|
-
cls.addClassMethod('marshaller', 'ResponseFieldMarshaller', `return new ResponseFieldMarshaller() {
|
|
789
|
-
@Override
|
|
790
|
-
public void marshal(ResponseWriter writer) {
|
|
791
|
-
${childFields
|
|
792
|
-
.map((f, index) => {
|
|
793
|
-
const writerMethod = this._getWriterMethodByType(f.type);
|
|
794
|
-
if (f.isList) {
|
|
795
|
-
return visitorPluginCommon.indentMultiline(`writer.writeList($responseFields[${index}], ${f.fieldName}, new ResponseWriter.ListWriter() {
|
|
796
|
-
@Override
|
|
797
|
-
public void write(Object value, ResponseWriter.ListItemWriter listItemWriter) {
|
|
798
|
-
listItemWriter.${writerMethod.name}(((${f.className}) value)${writerMethod.useMarshaller ? '.marshaller()' : ''});
|
|
799
|
-
}
|
|
800
|
-
});`, 2);
|
|
801
|
-
}
|
|
802
|
-
let fValue = `${f.fieldName}${writerMethod.useMarshaller ? '.marshaller()' : ''}`;
|
|
803
|
-
if (writerMethod.checkNull || !f.isNonNull) {
|
|
804
|
-
fValue = `${f.fieldName} != null ? ${fValue} : null`;
|
|
805
|
-
}
|
|
806
|
-
return visitorPluginCommon.indent(`writer.${writerMethod.name}(${writerMethod.castTo ? `(${writerMethod.castTo}) ` : ''}$responseFields[${index}], ${fValue});`, 2);
|
|
807
|
-
})
|
|
808
|
-
.join('\n')}
|
|
809
|
-
}
|
|
810
|
-
};`, [], [], 'public');
|
|
811
|
-
cls.nestedClass(this.buildMapperClass(className, childFields));
|
|
812
|
-
return options.result;
|
|
813
|
-
}
|
|
814
|
-
getReaderFn(baseType) {
|
|
815
|
-
if (graphql.isScalarType(baseType)) {
|
|
816
|
-
if (baseType.name === 'String') {
|
|
817
|
-
return { fn: `readString` };
|
|
818
|
-
}
|
|
819
|
-
if (baseType.name === 'Int') {
|
|
820
|
-
return { fn: `readInt` };
|
|
821
|
-
}
|
|
822
|
-
if (baseType.name === 'Float') {
|
|
823
|
-
return { fn: `readDouble` };
|
|
824
|
-
}
|
|
825
|
-
if (baseType.name === 'Boolean') {
|
|
826
|
-
return { fn: `readBoolean` };
|
|
827
|
-
}
|
|
828
|
-
return { fn: `readCustomType`, custom: true };
|
|
829
|
-
}
|
|
830
|
-
if (graphql.isEnumType(baseType)) {
|
|
831
|
-
return { fn: `readString` };
|
|
832
|
-
}
|
|
833
|
-
return { fn: `readObject`, object: baseType.name };
|
|
834
|
-
}
|
|
835
|
-
buildMapperClass(parentClassName, childFields) {
|
|
836
|
-
const wrapList = (childField, rawType, edgeStr) => {
|
|
837
|
-
if (graphql.isNonNullType(rawType)) {
|
|
838
|
-
return wrapList(childField, rawType.ofType, edgeStr);
|
|
839
|
-
}
|
|
840
|
-
if (graphql.isListType(rawType)) {
|
|
841
|
-
const typeStr = this.getListTypeWrapped(childField.className, rawType.ofType);
|
|
842
|
-
const innerContent = wrapList(childField, rawType.ofType, edgeStr);
|
|
843
|
-
const inner = graphql.isListType(rawType.ofType) ? `return listItemReader.readList(${innerContent});` : innerContent;
|
|
844
|
-
return `new ResponseReader.ListReader<${typeStr}>() {
|
|
845
|
-
@Override
|
|
846
|
-
public ${typeStr} read(ResponseReader.ListItemReader listItemReader) {
|
|
847
|
-
${visitorPluginCommon.indentMultiline(inner, 2)}
|
|
848
|
-
}
|
|
849
|
-
}`;
|
|
850
|
-
}
|
|
851
|
-
return edgeStr;
|
|
852
|
-
};
|
|
853
|
-
this._imports.add(Imports.ResponseReader);
|
|
854
|
-
const mapperBody = childFields.map((f, index) => {
|
|
855
|
-
const varDec = `final ${this.getListTypeWrapped(f.className, f.rawType)} ${f.fieldName} =`;
|
|
856
|
-
const readerFn = this.getReaderFn(f.type);
|
|
857
|
-
if (f.isFragment) {
|
|
858
|
-
return `${varDec} reader.readConditional($responseFields[${index}], new ResponseReader.ConditionalTypeReader<${f.className}>() {
|
|
859
|
-
@Override
|
|
860
|
-
public ${f.className} read(String conditionalType, ResponseReader reader) {
|
|
861
|
-
return fragmentsFieldMapper.map(reader, conditionalType);
|
|
862
|
-
}
|
|
863
|
-
});`;
|
|
864
|
-
}
|
|
865
|
-
if (f.isList) {
|
|
866
|
-
const listReader = readerFn.object
|
|
867
|
-
? `return listItemReader.${readerFn.fn}(new ResponseReader.ObjectReader<Item>() {
|
|
868
|
-
@Override
|
|
869
|
-
public Item read(ResponseReader reader) {
|
|
870
|
-
return ${f.fieldName}FieldMapper.map(reader);
|
|
871
|
-
}
|
|
872
|
-
});`
|
|
873
|
-
: `return listItemReader.${readerFn.fn}();`;
|
|
874
|
-
const wrappedList = wrapList(f, f.rawType, listReader);
|
|
875
|
-
return `${varDec} reader.readList($responseFields[${index}], ${wrappedList});`;
|
|
876
|
-
}
|
|
877
|
-
if (readerFn.object) {
|
|
878
|
-
return `${varDec} reader.readObject($responseFields[${index}], new ResponseReader.ObjectReader<${f.className}>() {
|
|
879
|
-
@Override
|
|
880
|
-
public ${f.className} read(ResponseReader reader) {
|
|
881
|
-
return ${f.fieldName}FieldMapper.map(reader);
|
|
882
|
-
}
|
|
883
|
-
});`;
|
|
884
|
-
}
|
|
885
|
-
return `${varDec} reader.${readerFn.fn}(${readerFn.custom ? '(ResponseField.CustomTypeField) ' : ''}$responseFields[${index}]);`;
|
|
886
|
-
});
|
|
887
|
-
const mapperImpl = [
|
|
888
|
-
...mapperBody,
|
|
889
|
-
`return new ${parentClassName}(${childFields.map(f => f.fieldName).join(', ')});`,
|
|
890
|
-
].join('\n');
|
|
891
|
-
const cls = new javaCommon.JavaDeclarationBlock()
|
|
892
|
-
.access('public')
|
|
893
|
-
.static()
|
|
894
|
-
.final()
|
|
895
|
-
.asKind('class')
|
|
896
|
-
.withName('Mapper')
|
|
897
|
-
.implements([`ResponseFieldMapper<${parentClassName}>`])
|
|
898
|
-
.addClassMethod('map', parentClassName, mapperImpl, [
|
|
899
|
-
{
|
|
900
|
-
name: 'reader',
|
|
901
|
-
type: 'ResponseReader',
|
|
902
|
-
},
|
|
903
|
-
], [], 'public', {}, ['Override']);
|
|
904
|
-
childFields
|
|
905
|
-
.filter(c => c.isObject)
|
|
906
|
-
.forEach(childField => {
|
|
907
|
-
cls.addClassMember(`${childField.fieldName}FieldMapper`, `${childField.className}.Mapper`, `new ${childField.className}.Mapper()`, [], 'private', { final: true });
|
|
908
|
-
});
|
|
909
|
-
return cls;
|
|
910
|
-
}
|
|
911
|
-
_resolveResponseFieldMethodForBaseType(baseType) {
|
|
912
|
-
if (graphql.isListType(baseType)) {
|
|
913
|
-
return { fn: `forList` };
|
|
914
|
-
}
|
|
915
|
-
if (graphql.isNonNullType(baseType)) {
|
|
916
|
-
return this._resolveResponseFieldMethodForBaseType(baseType.ofType);
|
|
917
|
-
}
|
|
918
|
-
if (graphql.isScalarType(baseType)) {
|
|
919
|
-
if (baseType.name === 'String') {
|
|
920
|
-
return { fn: `forString` };
|
|
921
|
-
}
|
|
922
|
-
if (baseType.name === 'Int') {
|
|
923
|
-
return { fn: `forInt` };
|
|
924
|
-
}
|
|
925
|
-
if (baseType.name === 'Float') {
|
|
926
|
-
return { fn: `forDouble` };
|
|
927
|
-
}
|
|
928
|
-
if (baseType.name === 'Boolean') {
|
|
929
|
-
return { fn: `forBoolean` };
|
|
930
|
-
}
|
|
931
|
-
this._imports.add(`${this.config.typePackage}.CustomType`);
|
|
932
|
-
return { fn: `forCustomType`, custom: true };
|
|
933
|
-
}
|
|
934
|
-
if (graphql.isEnumType(baseType)) {
|
|
935
|
-
return { fn: `forEnum` };
|
|
936
|
-
}
|
|
937
|
-
return { fn: `forObject` };
|
|
938
|
-
}
|
|
939
|
-
FragmentDefinition(node) {
|
|
940
|
-
this.visitingFragment = true;
|
|
941
|
-
const className = node.name.value;
|
|
942
|
-
const schemaType = this._schema.getType(node.typeCondition.name.value);
|
|
943
|
-
this._imports.add(Imports.Arrays);
|
|
944
|
-
this._imports.add(Imports.GraphqlFragment);
|
|
945
|
-
this._imports.add(Imports.List);
|
|
946
|
-
this._imports.add(Imports.String);
|
|
947
|
-
this._imports.add(Imports.Collections);
|
|
948
|
-
this._imports.add(Imports.Override);
|
|
949
|
-
this._imports.add(Imports.Generated);
|
|
950
|
-
this._imports.add(Imports.ResponseFieldMapper);
|
|
951
|
-
const dataClasses = this.transformSelectionSet({
|
|
952
|
-
className,
|
|
953
|
-
nonStaticClass: true,
|
|
954
|
-
implements: ['GraphqlFragment'],
|
|
955
|
-
selectionSet: node.selectionSet && node.selectionSet.selections ? node.selectionSet.selections : [],
|
|
956
|
-
result: {},
|
|
957
|
-
schemaType,
|
|
958
|
-
}, false);
|
|
959
|
-
const rootCls = dataClasses[className];
|
|
960
|
-
const printed = this.printDocument(node);
|
|
961
|
-
rootCls.addClassMember('FRAGMENT_DEFINITION', 'String', `"${printed}"`, [], 'public', {
|
|
962
|
-
static: true,
|
|
963
|
-
final: true,
|
|
964
|
-
});
|
|
965
|
-
const possibleTypes = graphql.isObjectType(schemaType) ? [schemaType.name] : this.getImplementingTypes(schemaType);
|
|
966
|
-
rootCls.addClassMember('POSSIBLE_TYPES', 'List<String>', `Collections.unmodifiableList(Arrays.asList(${possibleTypes.map(t => `"${t}"`).join(', ')}))`, [], 'public', { static: true, final: true });
|
|
967
|
-
Object.keys(dataClasses)
|
|
968
|
-
.filter(name => name !== className)
|
|
969
|
-
.forEach(clsName => {
|
|
970
|
-
rootCls.nestedClass(dataClasses[clsName]);
|
|
971
|
-
});
|
|
972
|
-
return rootCls.string;
|
|
973
|
-
}
|
|
974
|
-
OperationDefinition(node) {
|
|
975
|
-
this.visitingFragment = false;
|
|
976
|
-
const operationType = changeCaseAll.pascalCase(node.operation);
|
|
977
|
-
const operationSchemaType = this.getRootType(node.operation);
|
|
978
|
-
const className = node.name.value.endsWith(operationType) ? operationType : `${node.name.value}${operationType}`;
|
|
979
|
-
this._imports.add(Imports[operationType]);
|
|
980
|
-
this._imports.add(Imports.String);
|
|
981
|
-
this._imports.add(Imports.Override);
|
|
982
|
-
this._imports.add(Imports.Generated);
|
|
983
|
-
this._imports.add(Imports.OperationName);
|
|
984
|
-
this._imports.add(Imports.Operation);
|
|
985
|
-
this._imports.add(Imports.ResponseFieldMapper);
|
|
986
|
-
const cls = new javaCommon.JavaDeclarationBlock()
|
|
987
|
-
.annotate([`Generated("Apollo GraphQL")`])
|
|
988
|
-
.access('public')
|
|
989
|
-
.final()
|
|
990
|
-
.asKind('class')
|
|
991
|
-
.withName(className);
|
|
992
|
-
const printed = this.printDocument(node);
|
|
993
|
-
cls.implements([
|
|
994
|
-
`${operationType}<${className}.Data, ${className}.Data, ${node.variableDefinitions.length === 0 ? 'Operation' : className}.Variables>`,
|
|
995
|
-
]);
|
|
996
|
-
cls.addClassMember('OPERATION_DEFINITION', 'String', `"${printed}"`, [], 'public', { static: true, final: true });
|
|
997
|
-
cls.addClassMember('QUERY_DOCUMENT', 'String', 'OPERATION_DEFINITION', [], 'public', { static: true, final: true });
|
|
998
|
-
cls.addClassMember('OPERATION_NAME', 'OperationName', `new OperationName() {
|
|
999
|
-
@Override
|
|
1000
|
-
public String name() {
|
|
1001
|
-
return "${node.name.value}";
|
|
1002
|
-
}
|
|
1003
|
-
}`, [], 'public', { static: true, final: true });
|
|
1004
|
-
cls.addClassMember('variables', `${node.variableDefinitions.length === 0 ? 'Operation' : className}.Variables`, null, [], 'private', { final: true });
|
|
1005
|
-
cls.addClassMethod('queryDocument', `String`, `return QUERY_DOCUMENT;`, [], [], 'public', {}, ['Override']);
|
|
1006
|
-
cls.addClassMethod('wrapData', `${className}.Data`, `return data;`, [
|
|
1007
|
-
{
|
|
1008
|
-
name: 'data',
|
|
1009
|
-
type: `${className}.Data`,
|
|
1010
|
-
},
|
|
1011
|
-
], [], 'public', {}, ['Override']);
|
|
1012
|
-
cls.addClassMethod('variables', `${node.variableDefinitions.length === 0 ? 'Operation' : className}.Variables`, `return variables;`, [], [], 'public', {}, ['Override']);
|
|
1013
|
-
cls.addClassMethod('responseFieldMapper', `ResponseFieldMapper<${className}.Data>`, `return new Data.Mapper();`, [], [], 'public', {}, ['Override']);
|
|
1014
|
-
cls.addClassMethod('builder', `Builder`, `return new Builder();`, [], [], 'public', { static: true }, []);
|
|
1015
|
-
cls.addClassMethod('name', `OperationName`, `return OPERATION_NAME;`, [], [], 'public', {}, ['Override']);
|
|
1016
|
-
cls.addClassMethod('operationId', `String`, `return "${crypto.createHash('md5').update(printed).digest('hex')}";`, [], [], 'public', {}, []);
|
|
1017
|
-
this.addCtor(className, node, cls);
|
|
1018
|
-
this._imports.add(Imports.Operation);
|
|
1019
|
-
const dataClasses = this.transformSelectionSet({
|
|
1020
|
-
className: 'Data',
|
|
1021
|
-
implements: ['Operation.Data'],
|
|
1022
|
-
selectionSet: node.selectionSet && node.selectionSet.selections ? node.selectionSet.selections : [],
|
|
1023
|
-
result: {},
|
|
1024
|
-
schemaType: operationSchemaType,
|
|
1025
|
-
});
|
|
1026
|
-
Object.keys(dataClasses).forEach(className => {
|
|
1027
|
-
cls.nestedClass(dataClasses[className]);
|
|
1028
|
-
});
|
|
1029
|
-
cls.nestedClass(this.createBuilderClass(className, node.variableDefinitions || []));
|
|
1030
|
-
cls.nestedClass(this.createVariablesClass(className, node.variableDefinitions || []));
|
|
1031
|
-
return cls.string;
|
|
1032
|
-
}
|
|
1033
|
-
createVariablesClass(parentClassName, variables) {
|
|
1034
|
-
const className = 'Variables';
|
|
1035
|
-
const cls = new javaCommon.JavaDeclarationBlock()
|
|
1036
|
-
.static()
|
|
1037
|
-
.access('public')
|
|
1038
|
-
.final()
|
|
1039
|
-
.asKind('class')
|
|
1040
|
-
.extends(['Operation.Variables'])
|
|
1041
|
-
.withName(className);
|
|
1042
|
-
const ctorImpl = [];
|
|
1043
|
-
const ctorArgs = [];
|
|
1044
|
-
variables.forEach(variable => {
|
|
1045
|
-
ctorImpl.push(`this.${variable.variable.name.value} = ${variable.variable.name.value};`);
|
|
1046
|
-
ctorImpl.push(`this.valueMap.put("${variable.variable.name.value}", ${variable.variable.name.value});`);
|
|
1047
|
-
const baseTypeNode = visitorPluginCommon.getBaseTypeNode(variable.type);
|
|
1048
|
-
const schemaType = this._schema.getType(baseTypeNode.name.value);
|
|
1049
|
-
const javaClass = this.getJavaClass(schemaType);
|
|
1050
|
-
const annotation = graphql.isNonNullType(variable.type) ? 'Nullable' : 'Nonnull';
|
|
1051
|
-
this._imports.add(Imports[annotation]);
|
|
1052
|
-
ctorArgs.push({ name: variable.variable.name.value, type: javaClass, annotations: [annotation] });
|
|
1053
|
-
cls.addClassMember(variable.variable.name.value, javaClass, null, [annotation], 'private');
|
|
1054
|
-
cls.addClassMethod(variable.variable.name.value, javaClass, `return ${variable.variable.name.value};`, [], [], 'public');
|
|
1055
|
-
});
|
|
1056
|
-
this._imports.add(Imports.LinkedHashMap);
|
|
1057
|
-
this._imports.add(Imports.Map);
|
|
1058
|
-
cls.addClassMethod(className, null, ctorImpl.join('\n'), ctorArgs, [], 'public');
|
|
1059
|
-
cls.addClassMember('valueMap', 'Map<String, Object>', 'new LinkedHashMap<>()', [], 'private', {
|
|
1060
|
-
final: true,
|
|
1061
|
-
transient: true,
|
|
1062
|
-
});
|
|
1063
|
-
cls.addClassMethod('valueMap', 'Map<String, Object>', 'return Collections.unmodifiableMap(valueMap);', [], [], 'public', {}, ['Override']);
|
|
1064
|
-
const marshallerImpl = `return new InputFieldMarshaller() {
|
|
1065
|
-
@Override
|
|
1066
|
-
public void marshal(InputFieldWriter writer) throws IOException {
|
|
1067
|
-
${variables
|
|
1068
|
-
.map(v => {
|
|
1069
|
-
const baseTypeNode = visitorPluginCommon.getBaseTypeNode(v.type);
|
|
1070
|
-
const schemaType = this._schema.getType(baseTypeNode.name.value);
|
|
1071
|
-
const writerMethod = this._getWriterMethodByType(schemaType, true);
|
|
1072
|
-
return visitorPluginCommon.indent(`writer.${writerMethod.name}("${v.variable.name.value}", ${writerMethod.checkNull
|
|
1073
|
-
? `${v.variable.name.value} != null ? ${v.variable.name.value}${writerMethod.useMarshaller ? '.marshaller()' : ''} : null`
|
|
1074
|
-
: v.variable.name.value});`, 2);
|
|
1075
|
-
})
|
|
1076
|
-
.join('\n')}
|
|
1077
|
-
}
|
|
1078
|
-
};`;
|
|
1079
|
-
this._imports.add(Imports.InputFieldMarshaller);
|
|
1080
|
-
this._imports.add(Imports.InputFieldWriter);
|
|
1081
|
-
this._imports.add(Imports.IOException);
|
|
1082
|
-
cls.addClassMethod('marshaller', 'InputFieldMarshaller', marshallerImpl, [], [], 'public', {}, ['Override']);
|
|
1083
|
-
return cls;
|
|
1084
|
-
}
|
|
1085
|
-
_getWriterMethodByType(schemaType, idAsString = false) {
|
|
1086
|
-
if (graphql.isScalarType(schemaType)) {
|
|
1087
|
-
if (SCALAR_TO_WRITER_METHOD[schemaType.name] && (idAsString || schemaType.name !== 'ID')) {
|
|
1088
|
-
return {
|
|
1089
|
-
name: SCALAR_TO_WRITER_METHOD[schemaType.name],
|
|
1090
|
-
checkNull: false,
|
|
1091
|
-
useMarshaller: false,
|
|
1092
|
-
};
|
|
1093
|
-
}
|
|
1094
|
-
return { name: 'writeCustom', checkNull: false, useMarshaller: false, castTo: 'ResponseField.CustomTypeField' };
|
|
1095
|
-
}
|
|
1096
|
-
if (graphql.isInputObjectType(schemaType)) {
|
|
1097
|
-
return { name: 'writeObject', checkNull: true, useMarshaller: true };
|
|
1098
|
-
}
|
|
1099
|
-
if (graphql.isEnumType(schemaType)) {
|
|
1100
|
-
return { name: 'writeString', checkNull: false, useMarshaller: false };
|
|
1101
|
-
}
|
|
1102
|
-
if (graphql.isObjectType(schemaType) || graphql.isInterfaceType(schemaType)) {
|
|
1103
|
-
return { name: 'writeObject', checkNull: true, useMarshaller: true };
|
|
1104
|
-
}
|
|
1105
|
-
return { name: 'writeString', useMarshaller: false, checkNull: false };
|
|
1106
|
-
}
|
|
1107
|
-
createBuilderClass(parentClassName, variables) {
|
|
1108
|
-
const builderClassName = 'Builder';
|
|
1109
|
-
const cls = new javaCommon.JavaDeclarationBlock()
|
|
1110
|
-
.static()
|
|
1111
|
-
.final()
|
|
1112
|
-
.access('public')
|
|
1113
|
-
.asKind('class')
|
|
1114
|
-
.withName(builderClassName)
|
|
1115
|
-
.addClassMethod(builderClassName, null, '');
|
|
1116
|
-
variables.forEach(variable => {
|
|
1117
|
-
const baseTypeNode = visitorPluginCommon.getBaseTypeNode(variable.type);
|
|
1118
|
-
const schemaType = this._schema.getType(baseTypeNode.name.value);
|
|
1119
|
-
const javaClass = this.getJavaClass(schemaType);
|
|
1120
|
-
const annotation = graphql.isNonNullType(variable.type) ? 'Nonnull' : 'Nullable';
|
|
1121
|
-
this._imports.add(Imports[annotation]);
|
|
1122
|
-
cls.addClassMember(variable.variable.name.value, javaClass, null, [annotation], 'private');
|
|
1123
|
-
cls.addClassMethod(variable.variable.name.value, builderClassName, `this.${variable.variable.name.value} = ${variable.variable.name.value};\nreturn this;`, [
|
|
1124
|
-
{
|
|
1125
|
-
name: variable.variable.name.value,
|
|
1126
|
-
type: javaClass,
|
|
1127
|
-
annotations: [annotation],
|
|
1128
|
-
},
|
|
1129
|
-
], [], 'public');
|
|
1130
|
-
});
|
|
1131
|
-
this._imports.add(Imports.Utils);
|
|
1132
|
-
const nonNullChecks = variables
|
|
1133
|
-
.filter(f => graphql.isNonNullType(f))
|
|
1134
|
-
.map(f => `Utils.checkNotNull(${f.variable.name.value}, "${f.variable.name.value} == null");`);
|
|
1135
|
-
const returnStatement = `return new ${parentClassName}(${variables.map(v => v.variable.name.value).join(', ')});`;
|
|
1136
|
-
cls.addClassMethod('build', parentClassName, `${[...nonNullChecks, returnStatement].join('\n')}`, [], [], 'public');
|
|
1137
|
-
return cls;
|
|
1138
|
-
}
|
|
1139
|
-
}
|
|
1140
|
-
|
|
1141
|
-
var FileType;
|
|
1142
|
-
(function (FileType) {
|
|
1143
|
-
FileType[FileType["INPUT_TYPE"] = 0] = "INPUT_TYPE";
|
|
1144
|
-
FileType[FileType["OPERATION"] = 1] = "OPERATION";
|
|
1145
|
-
FileType[FileType["FRAGMENT"] = 2] = "FRAGMENT";
|
|
1146
|
-
FileType[FileType["CUSTOM_TYPES"] = 3] = "CUSTOM_TYPES";
|
|
1147
|
-
})(FileType || (FileType = {}));
|
|
1148
|
-
|
|
1149
|
-
const filteredScalars = ['String', 'Float', 'Int', 'Boolean'];
|
|
1150
|
-
class CustomTypeClassVisitor extends BaseJavaVisitor {
|
|
1151
|
-
constructor(schema, rawConfig) {
|
|
1152
|
-
super(schema, rawConfig, {
|
|
1153
|
-
typePackage: rawConfig.typePackage || 'type',
|
|
1154
|
-
});
|
|
1155
|
-
}
|
|
1156
|
-
extract(name) {
|
|
1157
|
-
const lastIndex = name.lastIndexOf('.');
|
|
1158
|
-
if (lastIndex === -1) {
|
|
1159
|
-
return {
|
|
1160
|
-
className: name,
|
|
1161
|
-
importFrom: Imports[name] || null,
|
|
1162
|
-
};
|
|
1163
|
-
}
|
|
1164
|
-
return {
|
|
1165
|
-
className: name.substring(lastIndex + 1),
|
|
1166
|
-
importFrom: name,
|
|
1167
|
-
};
|
|
1168
|
-
}
|
|
1169
|
-
additionalContent() {
|
|
1170
|
-
this._imports.add(Imports.ScalarType);
|
|
1171
|
-
this._imports.add(Imports.Class);
|
|
1172
|
-
this._imports.add(Imports.Override);
|
|
1173
|
-
this._imports.add(Imports.Generated);
|
|
1174
|
-
const allTypes = this._schema.getTypeMap();
|
|
1175
|
-
const enumValues = Object.keys(allTypes)
|
|
1176
|
-
.filter(t => graphql.isScalarType(allTypes[t]) && !filteredScalars.includes(t))
|
|
1177
|
-
.map(t => allTypes[t])
|
|
1178
|
-
.map(scalarType => {
|
|
1179
|
-
const uppercaseName = scalarType.name.toUpperCase();
|
|
1180
|
-
const javaType = this.extract(this.scalars[scalarType.name] || 'String');
|
|
1181
|
-
if (javaType.importFrom) {
|
|
1182
|
-
this._imports.add(javaType.importFrom);
|
|
1183
|
-
}
|
|
1184
|
-
return visitorPluginCommon.indentMultiline(`${uppercaseName} {
|
|
1185
|
-
@Override
|
|
1186
|
-
public String typeName() {
|
|
1187
|
-
return "${scalarType.name}";
|
|
1188
|
-
}
|
|
1189
|
-
|
|
1190
|
-
@Override
|
|
1191
|
-
public Class javaType() {
|
|
1192
|
-
return ${javaType.className}.class;
|
|
1193
|
-
}
|
|
1194
|
-
}`);
|
|
1195
|
-
})
|
|
1196
|
-
.join(',\n\n');
|
|
1197
|
-
return new javaCommon.JavaDeclarationBlock()
|
|
1198
|
-
.annotate([`Generated("Apollo GraphQL")`])
|
|
1199
|
-
.access('public')
|
|
1200
|
-
.asKind('enum')
|
|
1201
|
-
.withName('CustomType')
|
|
1202
|
-
.implements(['ScalarType'])
|
|
1203
|
-
.withBlock(enumValues).string;
|
|
1204
|
-
}
|
|
1205
|
-
getPackage() {
|
|
1206
|
-
return this.config.typePackage;
|
|
1207
|
-
}
|
|
1208
|
-
}
|
|
1209
|
-
|
|
1210
|
-
const plugin = (schema, documents, config) => {
|
|
1211
|
-
const allAst = graphql.concatAST(documents.map(v => v.document));
|
|
1212
|
-
const allFragments = [
|
|
1213
|
-
...allAst.definitions.filter(d => d.kind === graphql.Kind.FRAGMENT_DEFINITION).map(fragmentDef => ({
|
|
1214
|
-
node: fragmentDef,
|
|
1215
|
-
name: fragmentDef.name.value,
|
|
1216
|
-
onType: fragmentDef.typeCondition.name.value,
|
|
1217
|
-
isExternal: false,
|
|
1218
|
-
})),
|
|
1219
|
-
...(config.externalFragments || []),
|
|
1220
|
-
];
|
|
1221
|
-
let visitor;
|
|
1222
|
-
switch (config.fileType) {
|
|
1223
|
-
case FileType.FRAGMENT:
|
|
1224
|
-
case FileType.OPERATION: {
|
|
1225
|
-
visitor = new OperationVisitor(schema, config, allFragments);
|
|
1226
|
-
break;
|
|
1227
|
-
}
|
|
1228
|
-
case FileType.INPUT_TYPE: {
|
|
1229
|
-
visitor = new InputTypeVisitor(schema, config);
|
|
1230
|
-
break;
|
|
1231
|
-
}
|
|
1232
|
-
case FileType.CUSTOM_TYPES: {
|
|
1233
|
-
visitor = new CustomTypeClassVisitor(schema, config);
|
|
1234
|
-
break;
|
|
1235
|
-
}
|
|
1236
|
-
}
|
|
1237
|
-
if (!visitor) {
|
|
1238
|
-
return { content: '' };
|
|
1239
|
-
}
|
|
1240
|
-
const visitResult = pluginHelpers.oldVisit(allAst, visitor);
|
|
1241
|
-
const additionalContent = visitor.additionalContent();
|
|
1242
|
-
const imports = visitor.getImports();
|
|
1243
|
-
return {
|
|
1244
|
-
prepend: [`package ${visitor.getPackage()};\n`, ...imports],
|
|
1245
|
-
content: '\n' + [...visitResult.definitions.filter(a => a && typeof a === 'string'), additionalContent].join('\n'),
|
|
1246
|
-
};
|
|
1247
|
-
};
|
|
1248
|
-
|
|
1249
|
-
const packageNameToDirectory = (packageName) => {
|
|
1250
|
-
return `./${packageName.split('.').join('/')}/`;
|
|
1251
|
-
};
|
|
1252
|
-
const preset = {
|
|
1253
|
-
buildGeneratesSection: options => {
|
|
1254
|
-
const outDir = options.baseOutputDir;
|
|
1255
|
-
const inputTypesAst = [];
|
|
1256
|
-
graphql.visit(options.schema, {
|
|
1257
|
-
InputObjectTypeDefinition: {
|
|
1258
|
-
enter(node) {
|
|
1259
|
-
inputTypesAst.push(node);
|
|
1260
|
-
},
|
|
1261
|
-
},
|
|
1262
|
-
});
|
|
1263
|
-
const inputTypesDocumentNode = { kind: graphql.Kind.DOCUMENT, definitions: inputTypesAst };
|
|
1264
|
-
const allAst = graphql.concatAST(options.documents.map(v => v.document));
|
|
1265
|
-
const operationsAst = allAst.definitions.filter(d => d.kind === graphql.Kind.OPERATION_DEFINITION);
|
|
1266
|
-
const fragments = allAst.definitions.filter(d => d.kind === graphql.Kind.FRAGMENT_DEFINITION);
|
|
1267
|
-
const externalFragments = fragments.map(frag => ({
|
|
1268
|
-
isExternal: true,
|
|
1269
|
-
importFrom: frag.name.value,
|
|
1270
|
-
name: frag.name.value,
|
|
1271
|
-
onType: frag.typeCondition.name.value,
|
|
1272
|
-
node: frag,
|
|
1273
|
-
}));
|
|
1274
|
-
return [
|
|
1275
|
-
{
|
|
1276
|
-
filename: path.join(outDir, packageNameToDirectory(options.config.typePackage), 'CustomType.java'),
|
|
1277
|
-
plugins: options.plugins,
|
|
1278
|
-
pluginMap: options.pluginMap,
|
|
1279
|
-
config: {
|
|
1280
|
-
...options.config,
|
|
1281
|
-
fileType: FileType.CUSTOM_TYPES,
|
|
1282
|
-
},
|
|
1283
|
-
schema: options.schema,
|
|
1284
|
-
documents: [],
|
|
1285
|
-
},
|
|
1286
|
-
...inputTypesDocumentNode.definitions.map((ast) => {
|
|
1287
|
-
const document = { kind: graphql.Kind.DOCUMENT, definitions: [ast] };
|
|
1288
|
-
return {
|
|
1289
|
-
filename: path.join(outDir, packageNameToDirectory(options.config.typePackage), ast.name.value + '.java'),
|
|
1290
|
-
plugins: options.plugins,
|
|
1291
|
-
pluginMap: options.pluginMap,
|
|
1292
|
-
config: {
|
|
1293
|
-
...options.config,
|
|
1294
|
-
fileType: FileType.INPUT_TYPE,
|
|
1295
|
-
skipDocumentsValidation: true,
|
|
1296
|
-
},
|
|
1297
|
-
schema: options.schema,
|
|
1298
|
-
documents: [{ document, location: '' }],
|
|
1299
|
-
};
|
|
1300
|
-
}),
|
|
1301
|
-
...operationsAst.map((ast) => {
|
|
1302
|
-
const fileName = ast.name.value.toLowerCase().endsWith(ast.operation)
|
|
1303
|
-
? ast.name.value
|
|
1304
|
-
: `${ast.name.value}${changeCaseAll.pascalCase(ast.operation)}`;
|
|
1305
|
-
const document = { kind: graphql.Kind.DOCUMENT, definitions: [ast] };
|
|
1306
|
-
return {
|
|
1307
|
-
filename: path.join(outDir, packageNameToDirectory(options.config.package), fileName + '.java'),
|
|
1308
|
-
plugins: options.plugins,
|
|
1309
|
-
pluginMap: options.pluginMap,
|
|
1310
|
-
config: {
|
|
1311
|
-
...options.config,
|
|
1312
|
-
fileType: FileType.OPERATION,
|
|
1313
|
-
externalFragments,
|
|
1314
|
-
},
|
|
1315
|
-
schema: options.schema,
|
|
1316
|
-
documents: [{ document, location: '' }],
|
|
1317
|
-
};
|
|
1318
|
-
}),
|
|
1319
|
-
...fragments.map((ast) => {
|
|
1320
|
-
const document = { kind: graphql.Kind.DOCUMENT, definitions: [ast] };
|
|
1321
|
-
return {
|
|
1322
|
-
filename: path.join(outDir, packageNameToDirectory(options.config.fragmentPackage), ast.name.value + '.java'),
|
|
1323
|
-
plugins: options.plugins,
|
|
1324
|
-
pluginMap: options.pluginMap,
|
|
1325
|
-
config: {
|
|
1326
|
-
...options.config,
|
|
1327
|
-
fileType: FileType.FRAGMENT,
|
|
1328
|
-
externalFragments,
|
|
1329
|
-
},
|
|
1330
|
-
schema: options.schema,
|
|
1331
|
-
documents: [{ document, location: '' }],
|
|
1332
|
-
};
|
|
1333
|
-
}),
|
|
1334
|
-
];
|
|
1335
|
-
},
|
|
1336
|
-
};
|
|
1337
|
-
|
|
1338
|
-
exports.plugin = plugin;
|
|
1339
|
-
exports.preset = preset;
|