ruby-graphql-java-client-generator 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 773a17dfd5b053414f33095d99bab7ff5cfd5459206fad0bea3aeb071d2a0327
4
+ data.tar.gz: ff4442c643a6d61dcb2577bf7f7f9f4093ed4006a186c644e522f729b1084475
5
+ SHA512:
6
+ metadata.gz: debf89a729b76a3d95ec67d4e351d57e8d041a9f83e9d15c3a4e917b1bf21667c8de845e5ad318d6588de7c48793a6b683d121e9338889fec0cd803f83008a51
7
+ data.tar.gz: b715736c059f720eb92b870d8d2e29b44cc5907d0221e4a02691239c4f6462acca5cdb5c68fd9f5219f94b373e16f5f88d7bfdd672aa2187d64f3052d811c3d8
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Shopify
4
+ Copyright (c) 2019 Adobe
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in
14
+ all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
+ THE SOFTWARE.
@@ -0,0 +1,7 @@
1
+ # Ruby GraphQL Java Client Generator
2
+
3
+ This project is a fork from https://github.com/adobe/graphql-java-generator in the context of a PoC.
4
+
5
+ ## License
6
+
7
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,15 @@
1
+ class GraphQLJavaGen
2
+ class Annotation
3
+ attr_reader :name, :imports
4
+
5
+ def initialize(name, imports: [], &block)
6
+ @name = name
7
+ @imports = imports
8
+ @condition = block
9
+ end
10
+
11
+ def annotate?(field)
12
+ @condition.call(field)
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,53 @@
1
+ class GraphQLJavaGen
2
+ # Reformat code that uses curly brace blocks
3
+ class Reformatter
4
+ INDENT_START_CHARS = ["{", "("]
5
+ INDENT_END_CHARS = ["}", ")"]
6
+
7
+ def initialize(indent: "\t")
8
+ @indent = indent
9
+ end
10
+
11
+ def reformat(code)
12
+ output = ""
13
+ indent_level = 0
14
+ squeeze_newlines = true
15
+ javadoc = false
16
+
17
+ code.lines.each do |line|
18
+ stripped_line = line.strip
19
+
20
+ if INDENT_END_CHARS.include?(stripped_line[0])
21
+ indent_level -= 1
22
+ # no blank lines immediately preceding end of block
23
+ output.rstrip!
24
+ output << "\n"
25
+ end
26
+
27
+ if stripped_line.empty?
28
+ output << "\n" unless squeeze_newlines
29
+ squeeze_newlines = true
30
+ elsif javadoc
31
+ output << @indent * indent_level + " " << line.lstrip
32
+ squeeze_newlines = false
33
+ else
34
+ output << @indent * indent_level << line.lstrip
35
+ squeeze_newlines = false
36
+ end
37
+
38
+ if INDENT_START_CHARS.include?(stripped_line[-1])
39
+ indent_level += 1
40
+ # no blank lines following start of block
41
+ squeeze_newlines = true
42
+ end
43
+
44
+ if !javadoc && stripped_line.start_with?("/**")
45
+ javadoc = true
46
+ elsif javadoc && stripped_line.end_with?("*/")
47
+ javadoc = false;
48
+ end
49
+ end
50
+ output
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,33 @@
1
+ class GraphQLJavaGen
2
+ class Scalar
3
+ attr_reader :type_name, :imports
4
+
5
+ WRAPPER_OBJECT = {
6
+ 'int' => 'Integer',
7
+ 'long' => 'Long',
8
+ 'double' => 'Double',
9
+ 'boolean' => 'Boolean',
10
+ }
11
+ WRAPPER_OBJECT.default_proc = ->(_, key) { key }
12
+ private_constant :WRAPPER_OBJECT
13
+
14
+ def initialize(type_name:, java_type:, deserialize_expr:, imports: [])
15
+ @type_name = type_name
16
+ @java_type = java_type
17
+ @deserialize_expr = deserialize_expr
18
+ @imports = imports
19
+ end
20
+
21
+ def nullable_type
22
+ WRAPPER_OBJECT[@java_type]
23
+ end
24
+
25
+ def non_nullable_type
26
+ @java_type
27
+ end
28
+
29
+ def deserialize(expr)
30
+ @deserialize_expr.call(expr)
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,384 @@
1
+ <%= render_license %>
2
+
3
+ package <%= package_name %>;
4
+
5
+ import com.google.gson.Gson;
6
+ import com.google.gson.GsonBuilder;
7
+ import com.google.gson.JsonElement;
8
+ import com.google.gson.JsonObject;
9
+ import poc.graphql.client.support.AbstractResponse;
10
+ import poc.graphql.client.support.Arguments;
11
+ import poc.graphql.client.support.Error;
12
+ import poc.graphql.client.support.AbstractQuery;
13
+ import poc.graphql.client.support.SchemaViolationError;
14
+ import poc.graphql.client.support.TopLevelResponse;
15
+ import poc.graphql.client.support.Input;
16
+ <% imports.each do |import| %>
17
+ import <%= import %>;
18
+ <% end %>
19
+
20
+ import java.io.Serializable;
21
+ import java.util.ArrayList;
22
+ import java.util.List;
23
+ import java.util.Map;
24
+
25
+ public class <%= schema_name %> {
26
+ <% [[:query, schema.query_root_name], [:mutation, schema.mutation_root_name]].each do |operation_type, root_name| %>
27
+ <% next unless root_name %>
28
+ public static <%= root_name %>Query <%= operation_type %>(<%= root_name %>QueryDefinition queryDef) {
29
+ StringBuilder queryString = new StringBuilder("<%= operation_type unless operation_type == :query %>{");
30
+ <%= root_name %>Query query = new <%= root_name %>Query(queryString);
31
+ queryDef.define(query);
32
+ queryString.append('}');
33
+ return query;
34
+ }
35
+
36
+ public static class <%= operation_type.capitalize %>Response {
37
+ private TopLevelResponse response;
38
+ private <%= root_name %> data;
39
+
40
+ public <%= operation_type.capitalize %>Response(TopLevelResponse response) throws SchemaViolationError {
41
+ this.response = response;
42
+ this.data = response.getData() != null ? new <%= root_name %>(response.getData()) : null;
43
+ }
44
+
45
+ public <%= root_name %> getData() {
46
+ return data;
47
+ }
48
+
49
+ public List<Error> getErrors() {
50
+ return response.getErrors();
51
+ }
52
+
53
+ public String toJson() {
54
+ return new Gson().toJson(response);
55
+ }
56
+
57
+ public String prettyPrintJson() {
58
+ final Gson gson = new GsonBuilder().setPrettyPrinting().create();
59
+ return gson.toJson(response);
60
+ }
61
+
62
+ public static <%= operation_type.capitalize %>Response fromJson(String json) throws SchemaViolationError {
63
+ final TopLevelResponse response = new Gson().fromJson(json, TopLevelResponse.class);
64
+ return new <%= operation_type.capitalize %>Response(response);
65
+ }
66
+ }
67
+ <% end %>
68
+
69
+ <% schema.types.reject{ |type| type.name.start_with?('__') || type.scalar? }.each do |type| %>
70
+ <% case type.kind when 'OBJECT', 'INTERFACE', 'UNION' %>
71
+ <% fields = type.fields(include_deprecated: include_deprecated) || [] %>
72
+ public interface <%= type.name %>QueryDefinition {
73
+ void define(<%= type.name %>Query _queryBuilder);
74
+ }
75
+
76
+ <%= java_doc(type) %>
77
+ public static class <%= type.name %>Query extends AbstractQuery<<%= type.name %>Query> {
78
+ <%= type.name %>Query(StringBuilder _queryBuilder) {
79
+ super(_queryBuilder);
80
+ <% if type.object? && type.implement?("Node") %>
81
+ startField("id");
82
+ <% end %>
83
+ <% unless type.object? %>
84
+ startField("__typename");
85
+ <% end %>
86
+ }
87
+
88
+ <% fields.each do |field| %>
89
+ <% next if field.name == "id" && type.object? && type.implement?("Node") %>
90
+ <% unless field.optional_args.empty? %>
91
+ public class <%= field.classify_name %>Arguments extends Arguments {
92
+ <%= field.classify_name %>Arguments(StringBuilder _queryBuilder) {
93
+ super(_queryBuilder, <%= !!field.required_args.empty? %>);
94
+ }
95
+
96
+ <% field.optional_args.each do |arg| %>
97
+ <%= java_doc(arg) %>
98
+ public <%= field.classify_name %>Arguments <%= escape_reserved_word(arg.camelize_name) %>(<%= java_input_type(arg.type) %> value) {
99
+ if (value != null) {
100
+ startArgument("<%= arg.name %>");
101
+ <%= generate_build_input_code('value', arg.type) %>
102
+ }
103
+ return this;
104
+ }
105
+ <% end %>
106
+ }
107
+
108
+ public interface <%= field.classify_name %>ArgumentsDefinition {
109
+ void define(<%= field.classify_name %>Arguments args);
110
+ }
111
+
112
+ <%= java_doc(field) %>
113
+ public <%= type.name %>Query <%= escape_reserved_word(field.camelize_name) %>(<%= java_arg_defs(field, skip_optional: true) %>) {
114
+ return <%= escape_reserved_word(field.camelize_name) %>(<%= java_arg_expresions_with_empty_optional_args(field) %>);
115
+ }
116
+ <% end %>
117
+
118
+ <%= java_doc(field) %>
119
+ <%= field.deprecated? ? "@Deprecated\n" : '' -%>
120
+ public <%= type.name %>Query <%= escape_reserved_word(field.camelize_name) %>(<%= java_arg_defs(field) %>) {
121
+ startField("<%= field.name %>");
122
+ <% unless field.args.empty? %>
123
+ <% if field.required_args.empty? %>
124
+ <%= field.classify_name %>Arguments args = new <%= field.classify_name %>Arguments(_queryBuilder);
125
+ argsDef.define(args);
126
+ <%= field.classify_name %>Arguments.end(args);
127
+ <% else %>
128
+ <% field.required_args.each_with_index do |arg, i| %>
129
+ _queryBuilder.append("<%= i == 0 ? "(" : "," %><%= arg.name %>:");
130
+ <%= generate_build_input_code(escape_reserved_word(arg.camelize_name), arg.type) %>
131
+ <% end %>
132
+ <% unless field.optional_args.empty? %>
133
+ argsDef.define(new <%= field.classify_name %>Arguments(_queryBuilder));
134
+ <% end %>
135
+ _queryBuilder.append(')');
136
+ <% end %>
137
+ <% end %>
138
+ <% if field.subfields? %>
139
+ _queryBuilder.append('{');
140
+ queryDef.define(new <%= field.type.unwrap.name %>Query(_queryBuilder));
141
+ _queryBuilder.append('}');
142
+ <% end %>
143
+ return this;
144
+ }
145
+ <% end %>
146
+ <% unless type.object? %>
147
+ <% type.possible_types.each do |possible_type| %>
148
+ public <%= type.name %>Query on<%= possible_type.name %>(<%= possible_type.name %>QueryDefinition queryDef) {
149
+ startInlineFragment("<%= possible_type.name %>");
150
+ queryDef.define(new <%= possible_type.name %>Query(_queryBuilder));
151
+ _queryBuilder.append('}');
152
+ return this;
153
+ }
154
+ <% end %>
155
+ <% end %>
156
+
157
+ <% if schema.root_name?(type.name) %>
158
+ public String toString() {
159
+ return _queryBuilder.toString();
160
+ }
161
+ <% end %>
162
+ }
163
+
164
+ <% unless type.object? %>
165
+ <% if type.name == 'Node' %>
166
+ public interface <%= type.name %> extends com.shopify.graphql.support.Node {
167
+ <% else %>
168
+ public interface <%= type.name %> {
169
+ <% end %>
170
+ String getGraphQlTypeName();
171
+ <% fields.each do |field| %>
172
+ <%= java_output_type(field.type) %> get<%= field.classify_name %>();
173
+ <% end %>
174
+ }
175
+ <% end %>
176
+
177
+ <% class_name = type.object? ? type.name : "Unknown#{type.name}" %>
178
+ <%= java_doc(type) %>
179
+ public static class <%= class_name %> extends AbstractResponse<<%= class_name %>> <%= java_implements(type) %>{
180
+ public <%= class_name %>() {
181
+ }
182
+
183
+ public <%= class_name %>(JsonObject fields) throws SchemaViolationError {
184
+ for (Map.Entry<String, JsonElement> field : fields.entrySet()) {
185
+ String key = field.getKey();
186
+ String fieldName = getFieldName(key);
187
+ switch (fieldName) {
188
+ <% fields.each do |field| %>
189
+ case "<%= field.name %>": {
190
+ <% generate_build_output_code("field.getValue()", field.type) do |statements, expr| %>
191
+ <%= statements %>
192
+ responseData.put(key, <%= expr %>);
193
+ <% end %>
194
+ break;
195
+ }
196
+ <% end %>
197
+ case "__typename": {
198
+ responseData.put(key, jsonAsString(field.getValue(), key));
199
+ break;
200
+ }
201
+ default: {
202
+ throw new SchemaViolationError(this, key, field.getValue());
203
+ }
204
+ }
205
+ }
206
+ }
207
+
208
+ <% if type.object? && type.implement?("Node") %>
209
+ public <%= class_name %>(<%= scalars['ID'].non_nullable_type %> id) {
210
+ this();
211
+ optimisticData.put("id", id);
212
+ }
213
+ <% end %>
214
+
215
+ <% if type.object? %>
216
+ public String getGraphQlTypeName() {
217
+ return "<%= type.name %>";
218
+ }
219
+ <% else %>
220
+ public static <%= type.name %> create(JsonObject fields) throws SchemaViolationError {
221
+ String typeName = fields.getAsJsonPrimitive("__typename").getAsString();
222
+ switch (typeName) {
223
+ <% type.possible_types.each do |possible_type| %>
224
+ case "<%= possible_type.name %>": {
225
+ return new <%= possible_type.name %>(fields);
226
+ }
227
+ <% end %>
228
+ default: {
229
+ return new <%= class_name %>(fields);
230
+ }
231
+ }
232
+ }
233
+
234
+ public String getGraphQlTypeName() {
235
+ return (String) get("__typename");
236
+ }
237
+ <% end %>
238
+
239
+ <% fields.each do |field| %>
240
+ <%= java_doc(field) %>
241
+ <%= java_annotations(field) %>
242
+ public <%= java_output_type(field.type) %> get<%= field.classify_name %>() {
243
+ return (<%= java_output_type(field.type) %>) get("<%= field.name %>");
244
+ }
245
+
246
+ <% next if field.name == "id" && type.object? && type.implement?("Node") %>
247
+ public <%= class_name %> set<%= field.classify_name %>(<%= java_output_type(field.type) %> arg) {
248
+ optimisticData.put(getKey("<%= field.name %>"), arg);
249
+ return this;
250
+ }
251
+ <% end %>
252
+
253
+ public boolean unwrapsToObject(String key) {
254
+ switch (getFieldName(key)) {
255
+ <% fields.each do |field| %>
256
+ case "<%= field.name %>": return <%= field.type.unwrap.object? %>;
257
+ <% end %>
258
+ default: return false;
259
+ }
260
+ }
261
+ }
262
+ <% when 'INPUT_OBJECT' %>
263
+ public static class <%= type.name %> implements Serializable {
264
+ <% type.required_input_fields.each do |field| %>
265
+ private <%= java_input_type(field.type) %> <%= escape_reserved_word(field.camelize_name) %>;
266
+ <% end %>
267
+ <% type.optional_input_fields.each do |field| %>
268
+ private Input<<%= java_input_type(field.type) %>> <%= escape_reserved_word(field.camelize_name) %> = Input.undefined();
269
+ <% end %>
270
+
271
+ <% unless type.required_input_fields.empty? %>
272
+ public <%= type.name %>(<%= type.required_input_fields.map{ |field| "#{java_input_type(field.type)} #{escape_reserved_word(field.camelize_name)}" }.join(', ') %>) {
273
+ <% type.required_input_fields.each do |field| %>
274
+ this.<%= escape_reserved_word(field.camelize_name) %> = <%= escape_reserved_word(field.camelize_name) %>;
275
+ <% end %>
276
+ }
277
+ <% end %>
278
+
279
+ <% type.required_input_fields.each do |field| %>
280
+ <%= java_annotations(field) %>
281
+ public <%= java_input_type(field.type) %> get<%= field.classify_name %>() {
282
+ return <%= escape_reserved_word(field.camelize_name) %>;
283
+ }
284
+
285
+ public <%= type.name %> set<%= field.classify_name %>(<%= java_annotations(field, in_argument: true) %><%= java_input_type(field.type) %> <%= escape_reserved_word(field.camelize_name) %>) {
286
+ this.<%= escape_reserved_word(field.camelize_name) %> = <%= escape_reserved_word(field.camelize_name) %>;
287
+ return this;
288
+ }
289
+
290
+ <% end %>
291
+ <% type.optional_input_fields.each do |field| %>
292
+ <%= java_annotations(field) %>
293
+ public <%= java_input_type(field.type) %> get<%= field.classify_name %>() {
294
+ return <%= escape_reserved_word(field.camelize_name) %>.getValue();
295
+ }
296
+
297
+ public Input<<%= java_input_type(field.type) %>> get<%= field.classify_name %>Input() {
298
+ return <%= escape_reserved_word(field.camelize_name) %>;
299
+ }
300
+
301
+ public <%= type.name %> set<%= field.classify_name %>(<%= java_annotations(field, in_argument: true) %><%= java_input_type(field.type) %> <%= escape_reserved_word(field.camelize_name) %>) {
302
+ this.<%= escape_reserved_word(field.camelize_name) %> = Input.optional(<%= escape_reserved_word(field.camelize_name) %>);
303
+ return this;
304
+ }
305
+
306
+ public <%= type.name %> set<%= field.classify_name %>Input(Input<<%= java_input_type(field.type) %>> <%= escape_reserved_word(field.camelize_name) %>) {
307
+ if (<%= escape_reserved_word(field.camelize_name) %> == null) {
308
+ throw new IllegalArgumentException("Input can not be null");
309
+ }
310
+ this.<%= escape_reserved_word(field.camelize_name) %> = <%= escape_reserved_word(field.camelize_name) %>;
311
+ return this;
312
+ }
313
+
314
+ <% end %>
315
+
316
+ public void appendTo(StringBuilder _queryBuilder) {
317
+ String separator = "";
318
+ _queryBuilder.append('{');
319
+ <% type.required_input_fields.each do |field| %>
320
+ _queryBuilder.append(separator);
321
+ separator = ",";
322
+ _queryBuilder.append("<%= field.name %>:");
323
+ <%= generate_build_input_code(escape_reserved_word(field.camelize_name), field.type) %>
324
+ <% end %>
325
+ <% type.optional_input_fields.each do |field| %>
326
+ if (this.<%= escape_reserved_word(field.camelize_name) %>.isDefined()) {
327
+ _queryBuilder.append(separator);
328
+ separator = ",";
329
+ _queryBuilder.append("<%= field.name %>:");
330
+ if (<%= escape_reserved_word(field.camelize_name) %>.getValue() != null) {
331
+ <%= generate_build_input_code(escape_reserved_word(field.camelize_name).concat(".getValue()"), field.type) %>
332
+ } else {
333
+ _queryBuilder.append("null");
334
+ }
335
+ }
336
+ <% end %>
337
+ _queryBuilder.append('}');
338
+ }
339
+ }
340
+ <% when 'ENUM' %>
341
+ <%= java_doc(type) %>
342
+ public enum <%= type.name %> {
343
+ <% type.enum_values(include_deprecated: include_deprecated).each do |value| %>
344
+ <%= java_doc(value) %>
345
+ <%= value.deprecated? ? "@Deprecated\n" : '' -%>
346
+ <%= value.upcase_name %>,
347
+ <% end %>
348
+
349
+ UNKNOWN_VALUE;
350
+
351
+ public static <%= type.name %> fromGraphQl(String value) {
352
+ if (value == null) {
353
+ return null;
354
+ }
355
+
356
+ switch (value) {
357
+ <% type.enum_values.each do |value| %>
358
+ case "<%= value.name %>": {
359
+ return <%= value.upcase_name %>;
360
+ }
361
+ <% end %>
362
+ default: {
363
+ return UNKNOWN_VALUE;
364
+ }
365
+ }
366
+ }
367
+ public String toString() {
368
+ switch (this) {
369
+ <% type.enum_values.each do |value| %>
370
+ case <%= value.upcase_name %>: {
371
+ return "<%= value.name %>";
372
+ }
373
+ <% end %>
374
+ default: {
375
+ return "";
376
+ }
377
+ }
378
+ }
379
+ }
380
+ <% else %>
381
+ <% raise NotImplementedError, "unhandled #{type.kind} type #{type.name}" %>
382
+ <% end %>
383
+ <% end %>
384
+ }