graphql_java_gen 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: '09c51e3a3cd35736ed828139d47f92bb6386fbe1'
4
- data.tar.gz: 5388a33adb2994b597b8fa9d380c519115363b93
2
+ SHA256:
3
+ metadata.gz: dd77a40622987096e4ef455cd48168eb2e8dd81f00c85c16388e524ce7829ad8
4
+ data.tar.gz: cbe9ed3fd060f37f80f58b9434a59ccef65c24b5c016af2dfea8ce4a7ccd087e
5
5
  SHA512:
6
- metadata.gz: 858c440553a22269df660fdd3560e7d3940bf02357984d4bad1543e7913b1f5e1c3a192834e6dc2cd8b37471b102ae1baa6d15e95fc7aadfa1bbeac7e9c57fc9
7
- data.tar.gz: 2dc257fbbdae8e1d84d0202605c279fbaea372129cb42cbce7cf6f783baa808bb0791ee5ff5bf532821a3f7c178ddfa9c02434279a3a8e3e6cd89bac6f21a829
6
+ metadata.gz: c172a7e0dae1cf363805570ec97f73a76dd23b5d66904f0850b3caa18962641644aeb94242b573f1608aa8e4c2cde14a4059af64384e2a646e074fab4aabc6b8
7
+ data.tar.gz: 5df07b68b93f7c510b4d45ff7756c99c6fcba009d92dacce3c31d341926a0a1ad7b9319892268187d2197db40a30ee0c7e1c30815a74ca481d842d3ebbee504b
data/README.md CHANGED
@@ -25,10 +25,12 @@ The generated code depends on the com.shopify.graphql.support java
25
25
  package. This can be added to a gradle project by adding the following
26
26
  jCenter dependancy to you `build.gradle` file:
27
27
 
28
- compile 'com.shopify.graphql.support:support:0.1.0'
28
+ compile 'com.shopify.graphql.support:support:0.2.0'
29
29
 
30
30
  ## Usage
31
31
 
32
+ ### Monolith (single file) schema generation
33
+
32
34
  Create a script that generates the code for a GraphQL API
33
35
 
34
36
  ```ruby
@@ -42,6 +44,7 @@ schema = GraphQLSchema.new(JSON.parse(introspection_result))
42
44
  GraphQLJavaGen.new(schema,
43
45
  package_name: "com.example.MyApp",
44
46
  nest_under: 'ExampleSchema',
47
+ version: '2020-01',
45
48
  custom_scalars: [
46
49
  GraphQLJavaGen::Scalar.new(
47
50
  type_name: 'Decimal',
@@ -53,6 +56,48 @@ GraphQLJavaGen.new(schema,
53
56
  ).save("#{Dir.pwd}/../MyApp/src/main/java/com/example/MyApp/ExampleSchema.java")
54
57
  ```
55
58
 
59
+ The generated `ExampleSchema.java` will include a single class, `ExampleSchema`, which contains
60
+ all defined GraphQL schema entities as nested subclasses.
61
+
62
+
63
+ ### Granular (multiple file) schema generation
64
+
65
+ You may also generate separate class files for each schema entity using the `save_granular` command.
66
+ In this case, you should provide the path to a directory, not a single class.
67
+
68
+ e.g.
69
+ ```ruby
70
+ require 'graphql_java_gen'
71
+ require 'graphql_schema'
72
+ require 'json'
73
+
74
+ introspection_result = File.read("graphql_schema.json")
75
+ schema = GraphQLSchema.new(JSON.parse(introspection_result))
76
+
77
+ GraphQLJavaGen.new(schema,
78
+ package_name: "com.example.MyApp",
79
+ nest_under: 'ExampleSchema',
80
+ version: '2020-01',
81
+ custom_scalars: [
82
+ GraphQLJavaGen::Scalar.new(
83
+ type_name: 'Decimal',
84
+ java_type: 'BigDecimal',
85
+ deserialize_expr: ->(expr) { "new BigDecimal(jsonAsString(#{expr}, key))" },
86
+ imports: ['java.math.BigDecimal'],
87
+ ),
88
+ ]
89
+ ).save_granular("#{Dir.pwd}/../MyApp/src/main/java/com/example/MyApp/")
90
+ ```
91
+
92
+ When using the granular option, the `com.example.MyApp` package wil contain many small
93
+ class files each containing a single GraphQL schema entity.
94
+
95
+ ### Generated code
96
+
97
+ Regarless of whether you use the monolith or granular schema generator, the usage is largely the same.
98
+ The only difference depends on whether you access the schema entities directly from the provided package
99
+ or as nested, static classes on the `ExampleSchema` class.
100
+
56
101
  That generated code depends on the com.shopify.graphql.support package.
57
102
 
58
103
  The generated code includes a query builder that can be used to create a GraphQL
@@ -7,11 +7,11 @@ require 'erb'
7
7
  require 'set'
8
8
 
9
9
  class GraphQLJavaGen
10
- attr_reader :schema, :package_name, :scalars, :imports, :script_name, :schema_name
10
+ attr_reader :schema, :package_name, :scalars, :imports, :script_name, :schema_name, :include_deprecated, :version
11
11
 
12
12
  def initialize(schema,
13
13
  package_name:, nest_under:, script_name: 'graphql_java_gen gem',
14
- custom_scalars: [], custom_annotations: []
14
+ custom_scalars: [], custom_annotations: [], include_deprecated: false, version: ''
15
15
  )
16
16
  @schema = schema
17
17
  @schema_name = nest_under
@@ -21,12 +21,22 @@ class GraphQLJavaGen
21
21
  @scalars.default_proc = ->(hash, key) { DEFAULT_SCALAR }
22
22
  @annotations = custom_annotations
23
23
  @imports = (@scalars.values.map(&:imports) + @annotations.map(&:imports)).flatten.sort.uniq
24
+ @include_deprecated = include_deprecated
25
+ @version = version
24
26
  end
25
27
 
26
28
  def save(path)
27
29
  File.write(path, generate)
28
30
  end
29
31
 
32
+ def save_granular(path)
33
+ write_schema(path)
34
+ write_static_methods(path)
35
+ write_response(path, :query, schema.query_root_name)
36
+ write_response(path, :mutation, schema.mutation_root_name)
37
+ write_entities(path)
38
+ end
39
+
30
40
  def generate
31
41
  reformat(TEMPLATE_ERB.result(binding))
32
42
  end
@@ -37,7 +47,7 @@ class GraphQLJavaGen
37
47
  private
38
48
 
39
49
  def erb_for(template_filename)
40
- erb = ERB.new(File.read(template_filename))
50
+ erb = ERB.new(File.read(template_filename), nil, '-')
41
51
  erb.filename = template_filename
42
52
  erb
43
53
  end
@@ -46,6 +56,51 @@ class GraphQLJavaGen
46
56
  TEMPLATE_ERB = erb_for(File.expand_path("../graphql_java_gen/templates/APISchema.java.erb", __FILE__))
47
57
  private_constant :TEMPLATE_ERB
48
58
 
59
+ def erb_for_entity(template)
60
+ template_filename = File.expand_path("../graphql_java_gen/templates/#{template}.erb", __FILE__)
61
+ erb = ERB.new(File.read(template_filename), nil, '-')
62
+ erb.filename = template_filename
63
+ erb
64
+ end
65
+
66
+ def generate_entity(template, type)
67
+ erb_template = erb_for_entity(template)
68
+ reformat(erb_template.result(binding))
69
+ end
70
+
71
+ def write_schema(path)
72
+ File.write(path + "/Schema.java", reformat(erb_for_entity("Schema.java").result(binding)))
73
+ end
74
+
75
+ def write_static_methods(path)
76
+ File.write(path + "/Operations.java", reformat(erb_for_entity("Operations.java").result(binding)))
77
+ end
78
+
79
+ def write_response(path, query, root_name)
80
+ response_type = query.to_s.capitalize
81
+ response = reformat(erb_for_entity("Responses.java").result(binding))
82
+ File.write(path + "/#{response_type}Response.java", response)
83
+ end
84
+
85
+ def write_entities(path)
86
+ schema.types.reject{ |type| type.name.start_with?('__') || type.scalar? }.each do |type|
87
+ case type.kind when 'OBJECT', 'INTERFACE', 'UNION'
88
+ File.write(path + "/#{type.name}QueryDefinition.java", generate_entity("QueryDefinition.java", type))
89
+ File.write(path + "/#{type.name}Query.java", generate_entity("Query.java", type))
90
+ File.write(path + "/#{type.name}.java", generate_entity("Interface.java", type))
91
+
92
+ class_name = type.object? ? type.name : "Unknown#{type.name}"
93
+ File.write(path + "/#{class_name}.java", generate_entity("Object.java", type))
94
+ when 'INPUT_OBJECT'
95
+ File.write(path + "/#{type.name}.java", generate_entity("Input.java", type))
96
+ when 'ENUM'
97
+ File.write(path + "/#{type.name}.java", generate_entity("Enum.java", type))
98
+ else
99
+ raise NotImplementedError, "unhandled #{type.kind} type #{type.name}"
100
+ end
101
+ end
102
+ end
103
+
49
104
  DEFAULT_SCALAR = Scalar.new(
50
105
  type_name: nil,
51
106
  java_type: 'String',
@@ -144,12 +199,13 @@ class GraphQLJavaGen
144
199
  item_type = type.of_type
145
200
  <<-JAVA
146
201
  _queryBuilder.append('[');
147
-
148
- String listSeperator#{depth} = "";
149
- for (#{java_input_type(item_type)} item#{depth} : #{expr}) {
150
- _queryBuilder.append(listSeperator#{depth});
151
- listSeperator#{depth} = ",";
152
- #{generate_build_input_code("item#{depth}", item_type, depth: depth + 1)}
202
+ {
203
+ String listSeperator#{depth} = "";
204
+ for (#{java_input_type(item_type)} item#{depth} : #{expr}) {
205
+ _queryBuilder.append(listSeperator#{depth});
206
+ listSeperator#{depth} = ",";
207
+ #{generate_build_input_code("item#{depth}", item_type, depth: depth + 1)}
208
+ }
153
209
  }
154
210
  _queryBuilder.append(']');
155
211
  JAVA
@@ -252,10 +308,17 @@ class GraphQLJavaGen
252
308
  "implements #{interfaces.to_a.join(', ')} "
253
309
  end
254
310
 
255
- def java_annotations(field)
256
- @annotations.map do |annotation|
311
+ def java_annotations(field, in_argument: false)
312
+ annotations = @annotations.map do |annotation|
257
313
  "@#{annotation.name}" if annotation.annotate?(field)
258
- end.compact.join("\n")
314
+ end.compact
315
+ return "" unless annotations.any?
316
+
317
+ if in_argument
318
+ annotations.join(" ") + " "
319
+ else
320
+ annotations.join("\n")
321
+ end
259
322
  end
260
323
 
261
324
  def type_names_set
@@ -280,12 +343,22 @@ class GraphQLJavaGen
280
343
  unless element.description.nil?
281
344
  description = wrap_text(element.description, 100)
282
345
  description = description.chomp("\n").gsub("\n", "\n* ")
283
- doc << "/**\n"
284
346
  doc << '* '
285
347
  doc << description
286
- doc << "\n*/"
287
348
  end
288
- doc
349
+
350
+ if element.respond_to?(:deprecated?) && element.deprecated?
351
+ unless doc.empty?
352
+ doc << "\n*"
353
+ doc << "\n*"
354
+ else
355
+ doc << '*'
356
+ end
357
+ doc << ' @deprecated '
358
+ doc << element.deprecation_reason
359
+ end
360
+
361
+ doc.empty? ? doc : "/**\n" + doc + "\n*/"
289
362
  end
290
363
 
291
364
  def wrap_text(text, col_width=80)
@@ -12,6 +12,7 @@ import com.shopify.graphql.support.Error;
12
12
  import com.shopify.graphql.support.Query;
13
13
  import com.shopify.graphql.support.SchemaViolationError;
14
14
  import com.shopify.graphql.support.TopLevelResponse;
15
+ import com.shopify.graphql.support.Input;
15
16
  <% imports.each do |import| %>
16
17
  import <%= import %>;
17
18
  <% end %>
@@ -22,6 +23,8 @@ import java.util.List;
22
23
  import java.util.Map;
23
24
 
24
25
  public class <%= schema_name %> {
26
+ public static final String API_VERSION = "<%= version %>";
27
+
25
28
  <% [[:query, schema.query_root_name], [:mutation, schema.mutation_root_name]].each do |operation_type, root_name| %>
26
29
  <% next unless root_name %>
27
30
  public static <%= root_name %>Query <%= operation_type %>(<%= root_name %>QueryDefinition queryDef) {
@@ -67,7 +70,7 @@ public class <%= schema_name %> {
67
70
 
68
71
  <% schema.types.reject{ |type| type.name.start_with?('__') || type.scalar? }.each do |type| %>
69
72
  <% case type.kind when 'OBJECT', 'INTERFACE', 'UNION' %>
70
- <% fields = type.fields || [] %>
73
+ <% fields = type.fields(include_deprecated: include_deprecated) || [] %>
71
74
  public interface <%= type.name %>QueryDefinition {
72
75
  void define(<%= type.name %>Query _queryBuilder);
73
76
  }
@@ -115,6 +118,7 @@ public class <%= schema_name %> {
115
118
  <% end %>
116
119
 
117
120
  <%= java_doc(field) %>
121
+ <%= field.deprecated? ? "@Deprecated\n" : '' -%>
118
122
  public <%= type.name %>Query <%= escape_reserved_word(field.camelize_name) %>(<%= java_arg_defs(field) %>) {
119
123
  startField("<%= field.name %>");
120
124
  <% unless field.args.empty? %>
@@ -263,7 +267,7 @@ public class <%= schema_name %> {
263
267
  private <%= java_input_type(field.type) %> <%= escape_reserved_word(field.camelize_name) %>;
264
268
  <% end %>
265
269
  <% type.optional_input_fields.each do |field| %>
266
- private <%= java_input_type(field.type) %> <%= escape_reserved_word(field.camelize_name) %>;
270
+ private Input<<%= java_input_type(field.type) %>> <%= escape_reserved_word(field.camelize_name) %> = Input.undefined();
267
271
  <% end %>
268
272
 
269
273
  <% unless type.required_input_fields.empty? %>
@@ -275,24 +279,40 @@ public class <%= schema_name %> {
275
279
  <% end %>
276
280
 
277
281
  <% type.required_input_fields.each do |field| %>
282
+ <%= java_annotations(field) %>
278
283
  public <%= java_input_type(field.type) %> get<%= field.classify_name %>() {
279
284
  return <%= escape_reserved_word(field.camelize_name) %>;
280
285
  }
281
286
 
282
- public <%= type.name %> set<%= field.classify_name %>(<%= java_input_type(field.type) %> <%= escape_reserved_word(field.camelize_name) %>) {
287
+ public <%= type.name %> set<%= field.classify_name %>(<%= java_annotations(field, in_argument: true) %><%= java_input_type(field.type) %> <%= escape_reserved_word(field.camelize_name) %>) {
283
288
  this.<%= escape_reserved_word(field.camelize_name) %> = <%= escape_reserved_word(field.camelize_name) %>;
284
289
  return this;
285
290
  }
291
+
286
292
  <% end %>
287
293
  <% type.optional_input_fields.each do |field| %>
294
+ <%= java_annotations(field) %>
288
295
  public <%= java_input_type(field.type) %> get<%= field.classify_name %>() {
296
+ return <%= escape_reserved_word(field.camelize_name) %>.getValue();
297
+ }
298
+
299
+ public Input<<%= java_input_type(field.type) %>> get<%= field.classify_name %>Input() {
289
300
  return <%= escape_reserved_word(field.camelize_name) %>;
290
301
  }
291
302
 
292
- public <%= type.name %> set<%= field.classify_name %>(<%= java_input_type(field.type) %> <%= escape_reserved_word(field.camelize_name) %>) {
303
+ public <%= type.name %> set<%= field.classify_name %>(<%= java_annotations(field, in_argument: true) %><%= java_input_type(field.type) %> <%= escape_reserved_word(field.camelize_name) %>) {
304
+ this.<%= escape_reserved_word(field.camelize_name) %> = Input.optional(<%= escape_reserved_word(field.camelize_name) %>);
305
+ return this;
306
+ }
307
+
308
+ public <%= type.name %> set<%= field.classify_name %>Input(Input<<%= java_input_type(field.type) %>> <%= escape_reserved_word(field.camelize_name) %>) {
309
+ if (<%= escape_reserved_word(field.camelize_name) %> == null) {
310
+ throw new IllegalArgumentException("Input can not be null");
311
+ }
293
312
  this.<%= escape_reserved_word(field.camelize_name) %> = <%= escape_reserved_word(field.camelize_name) %>;
294
313
  return this;
295
314
  }
315
+
296
316
  <% end %>
297
317
 
298
318
  public void appendTo(StringBuilder _queryBuilder) {
@@ -305,11 +325,15 @@ public class <%= schema_name %> {
305
325
  <%= generate_build_input_code(escape_reserved_word(field.camelize_name), field.type) %>
306
326
  <% end %>
307
327
  <% type.optional_input_fields.each do |field| %>
308
- if (<%= escape_reserved_word(field.camelize_name) %> != null) {
328
+ if (this.<%= escape_reserved_word(field.camelize_name) %>.isDefined()) {
309
329
  _queryBuilder.append(separator);
310
330
  separator = ",";
311
331
  _queryBuilder.append("<%= field.name %>:");
312
- <%= generate_build_input_code(escape_reserved_word(field.camelize_name), field.type) %>
332
+ if (<%= escape_reserved_word(field.camelize_name) %>.getValue() != null) {
333
+ <%= generate_build_input_code(escape_reserved_word(field.camelize_name).concat(".getValue()"), field.type) %>
334
+ } else {
335
+ _queryBuilder.append("null");
336
+ }
313
337
  }
314
338
  <% end %>
315
339
  _queryBuilder.append('}');
@@ -318,9 +342,12 @@ public class <%= schema_name %> {
318
342
  <% when 'ENUM' %>
319
343
  <%= java_doc(type) %>
320
344
  public enum <%= type.name %> {
321
- <% type.enum_values.each do |value| %>
345
+ <% type.enum_values(include_deprecated: include_deprecated).each do |value| %>
346
+ <%= java_doc(value) %>
347
+ <%= value.deprecated? ? "@Deprecated\n" : '' -%>
322
348
  <%= value.upcase_name %>,
323
349
  <% end %>
350
+
324
351
  UNKNOWN_VALUE;
325
352
 
326
353
  public static <%= type.name %> fromGraphQl(String value) {
@@ -0,0 +1,69 @@
1
+ // Generated from <%= script_name %>
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 com.shopify.graphql.support.AbstractResponse;
10
+ import com.shopify.graphql.support.Arguments;
11
+ import com.shopify.graphql.support.Error;
12
+ import com.shopify.graphql.support.Query;
13
+ import com.shopify.graphql.support.SchemaViolationError;
14
+ import com.shopify.graphql.support.TopLevelResponse;
15
+ import com.shopify.graphql.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
+ <% case type.kind when 'OBJECT', 'INTERFACE', 'UNION' %>
26
+ <% when 'INPUT_OBJECT' %>
27
+ <% when 'ENUM' %>
28
+ <%= java_doc(type) %>
29
+ public enum <%= type.name %> {
30
+ <% type.enum_values(include_deprecated: include_deprecated).each do |value| %>
31
+ <%= java_doc(value) %>
32
+ <%= value.deprecated? ? "@Deprecated\n" : '' -%>
33
+ <%= value.upcase_name %>,
34
+ <% end %>
35
+
36
+ UNKNOWN_VALUE;
37
+
38
+ public static <%= type.name %> fromGraphQl(String value) {
39
+ if (value == null) {
40
+ return null;
41
+ }
42
+
43
+ switch (value) {
44
+ <% type.enum_values(include_deprecated: include_deprecated).each do |value| %>
45
+ case "<%= value.name %>": {
46
+ return <%= value.upcase_name %>;
47
+ }
48
+ <% end %>
49
+ default: {
50
+ return UNKNOWN_VALUE;
51
+ }
52
+ }
53
+ }
54
+ public String toString() {
55
+ switch (this) {
56
+ <% type.enum_values(include_deprecated: include_deprecated).each do |value| %>
57
+ case <%= value.upcase_name %>: {
58
+ return "<%= value.name %>";
59
+ }
60
+ <% end %>
61
+ default: {
62
+ return "";
63
+ }
64
+ }
65
+ }
66
+ }
67
+ <% else %>
68
+ <% raise NotImplementedError, "unhandled #{type.kind} type #{type.name}" %>
69
+ <% end %>
@@ -0,0 +1,101 @@
1
+ // Generated from <%= script_name %>
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 com.shopify.graphql.support.AbstractResponse;
10
+ import com.shopify.graphql.support.Arguments;
11
+ import com.shopify.graphql.support.Error;
12
+ import com.shopify.graphql.support.Query;
13
+ import com.shopify.graphql.support.SchemaViolationError;
14
+ import com.shopify.graphql.support.TopLevelResponse;
15
+ import com.shopify.graphql.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 <%= type.name %> implements Serializable {
26
+ <% type.required_input_fields.each do |field| %>
27
+ private <%= java_input_type(field.type) %> <%= escape_reserved_word(field.camelize_name) %>;
28
+ <% end %>
29
+ <% type.optional_input_fields.each do |field| %>
30
+ private Input<<%= java_input_type(field.type) %>> <%= escape_reserved_word(field.camelize_name) %> = Input.undefined();
31
+ <% end %>
32
+
33
+ <% unless type.required_input_fields.empty? %>
34
+ public <%= type.name %>(<%= type.required_input_fields.map{ |field| "#{java_input_type(field.type)} #{escape_reserved_word(field.camelize_name)}" }.join(', ') %>) {
35
+ <% type.required_input_fields.each do |field| %>
36
+ this.<%= escape_reserved_word(field.camelize_name) %> = <%= escape_reserved_word(field.camelize_name) %>;
37
+ <% end %>
38
+ }
39
+ <% end %>
40
+
41
+ <% type.required_input_fields.each do |field| %>
42
+ <%= java_annotations(field) %>
43
+ public <%= java_input_type(field.type) %> get<%= field.classify_name %>() {
44
+ return <%= escape_reserved_word(field.camelize_name) %>;
45
+ }
46
+
47
+ public <%= type.name %> set<%= field.classify_name %>(<%= java_annotations(field, in_argument: true) %><%= java_input_type(field.type) %> <%= escape_reserved_word(field.camelize_name) %>) {
48
+ this.<%= escape_reserved_word(field.camelize_name) %> = <%= escape_reserved_word(field.camelize_name) %>;
49
+ return this;
50
+ }
51
+
52
+ <% end %>
53
+ <% type.optional_input_fields.each do |field| %>
54
+ <%= java_annotations(field) %>
55
+ public <%= java_input_type(field.type) %> get<%= field.classify_name %>() {
56
+ return <%= escape_reserved_word(field.camelize_name) %>.getValue();
57
+ }
58
+
59
+ public Input<<%= java_input_type(field.type) %>> get<%= field.classify_name %>Input() {
60
+ return <%= escape_reserved_word(field.camelize_name) %>;
61
+ }
62
+
63
+ public <%= type.name %> set<%= field.classify_name %>(<%= java_annotations(field, in_argument: true) %><%= java_input_type(field.type) %> <%= escape_reserved_word(field.camelize_name) %>) {
64
+ this.<%= escape_reserved_word(field.camelize_name) %> = Input.optional(<%= escape_reserved_word(field.camelize_name) %>);
65
+ return this;
66
+ }
67
+
68
+ public <%= type.name %> set<%= field.classify_name %>Input(Input<<%= java_input_type(field.type) %>> <%= escape_reserved_word(field.camelize_name) %>) {
69
+ if (<%= escape_reserved_word(field.camelize_name) %> == null) {
70
+ throw new IllegalArgumentException("Input can not be null");
71
+ }
72
+ this.<%= escape_reserved_word(field.camelize_name) %> = <%= escape_reserved_word(field.camelize_name) %>;
73
+ return this;
74
+ }
75
+
76
+ <% end %>
77
+
78
+ public void appendTo(StringBuilder _queryBuilder) {
79
+ String separator = "";
80
+ _queryBuilder.append('{');
81
+ <% type.required_input_fields.each do |field| %>
82
+ _queryBuilder.append(separator);
83
+ separator = ",";
84
+ _queryBuilder.append("<%= field.name %>:");
85
+ <%= generate_build_input_code(escape_reserved_word(field.camelize_name), field.type) %>
86
+ <% end %>
87
+ <% type.optional_input_fields.each do |field| %>
88
+ if (this.<%= escape_reserved_word(field.camelize_name) %>.isDefined()) {
89
+ _queryBuilder.append(separator);
90
+ separator = ",";
91
+ _queryBuilder.append("<%= field.name %>:");
92
+ if (<%= escape_reserved_word(field.camelize_name) %>.getValue() != null) {
93
+ <%= generate_build_input_code(escape_reserved_word(field.camelize_name).concat(".getValue()"), field.type) %>
94
+ } else {
95
+ _queryBuilder.append("null");
96
+ }
97
+ }
98
+ <% end %>
99
+ _queryBuilder.append('}');
100
+ }
101
+ }
@@ -0,0 +1,39 @@
1
+ // Generated from <%= script_name %>
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 com.shopify.graphql.support.AbstractResponse;
10
+ import com.shopify.graphql.support.Arguments;
11
+ import com.shopify.graphql.support.Error;
12
+ import com.shopify.graphql.support.Query;
13
+ import com.shopify.graphql.support.SchemaViolationError;
14
+ import com.shopify.graphql.support.TopLevelResponse;
15
+ import com.shopify.graphql.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
+
26
+ <% fields = type.fields(include_deprecated: include_deprecated) || [] %>
27
+ <%= java_doc(type) %>
28
+ <% unless type.object? %>
29
+ <% if type.name == 'Node' %>
30
+ public interface <%= type.name %> extends com.shopify.graphql.support.Node {
31
+ <% else %>
32
+ public interface <%= type.name %> {
33
+ <% end %>
34
+ String getGraphQlTypeName();
35
+ <% fields.each do |field| %>
36
+ <%= java_output_type(field.type) %> get<%= field.classify_name %>();
37
+ <% end %>
38
+ }
39
+ <% end %>
@@ -0,0 +1,111 @@
1
+ // Generated from <%= script_name %>
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 com.shopify.graphql.support.AbstractResponse;
10
+ import com.shopify.graphql.support.Arguments;
11
+ import com.shopify.graphql.support.Error;
12
+ import com.shopify.graphql.support.Query;
13
+ import com.shopify.graphql.support.SchemaViolationError;
14
+ import com.shopify.graphql.support.TopLevelResponse;
15
+ import com.shopify.graphql.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
+ <% fields = type.fields(include_deprecated: include_deprecated) || [] %>
26
+ <% class_name = type.object? ? type.name : "Unknown#{type.name}" %>
27
+ <%= java_doc(type) %>
28
+ public class <%= class_name %> extends AbstractResponse<<%= class_name %>> <%= java_implements(type) %>{
29
+ public <%= class_name %>() {
30
+ }
31
+
32
+ public <%= class_name %>(JsonObject fields) throws SchemaViolationError {
33
+ for (Map.Entry<String, JsonElement> field : fields.entrySet()) {
34
+ String key = field.getKey();
35
+ String fieldName = getFieldName(key);
36
+ switch (fieldName) {
37
+ <% fields.each do |field| %>
38
+ case "<%= field.name %>": {
39
+ <% generate_build_output_code("field.getValue()", field.type) do |statements, expr| %>
40
+ <%= statements %>
41
+ responseData.put(key, <%= expr %>);
42
+ <% end %>
43
+ break;
44
+ }
45
+ <% end %>
46
+ case "__typename": {
47
+ responseData.put(key, jsonAsString(field.getValue(), key));
48
+ break;
49
+ }
50
+ default: {
51
+ throw new SchemaViolationError(this, key, field.getValue());
52
+ }
53
+ }
54
+ }
55
+ }
56
+
57
+ <% if type.object? && type.implement?("Node") %>
58
+ public <%= class_name %>(<%= scalars['ID'].non_nullable_type %> id) {
59
+ this();
60
+ optimisticData.put("id", id);
61
+ }
62
+ <% end %>
63
+
64
+ <% if type.object? %>
65
+ public String getGraphQlTypeName() {
66
+ return "<%= type.name %>";
67
+ }
68
+ <% else %>
69
+ public static <%= type.name %> create(JsonObject fields) throws SchemaViolationError {
70
+ String typeName = fields.getAsJsonPrimitive("__typename").getAsString();
71
+ switch (typeName) {
72
+ <% type.possible_types.each do |possible_type| %>
73
+ case "<%= possible_type.name %>": {
74
+ return new <%= possible_type.name %>(fields);
75
+ }
76
+ <% end %>
77
+ default: {
78
+ return new <%= class_name %>(fields);
79
+ }
80
+ }
81
+ }
82
+
83
+ public String getGraphQlTypeName() {
84
+ return (String) get("__typename");
85
+ }
86
+ <% end %>
87
+
88
+ <% fields.each do |field| %>
89
+ <%= java_doc(field) %>
90
+ <%= java_annotations(field) %>
91
+ public <%= java_output_type(field.type) %> get<%= field.classify_name %>() {
92
+ return (<%= java_output_type(field.type) %>) get("<%= field.name %>");
93
+ }
94
+
95
+ <% next if field.name == "id" && type.object? && type.implement?("Node") %>
96
+ public <%= class_name %> set<%= field.classify_name %>(<%= java_output_type(field.type) %> arg) {
97
+ optimisticData.put(getKey("<%= field.name %>"), arg);
98
+ return this;
99
+ }
100
+ <% end %>
101
+
102
+ public boolean unwrapsToObject(String key) {
103
+ switch (getFieldName(key)) {
104
+ <% fields.each do |field| %>
105
+ case "<%= field.name %>": return <%= field.type.unwrap.object? %>;
106
+ <% end %>
107
+ default: return false;
108
+ }
109
+ }
110
+ }
111
+
@@ -0,0 +1,36 @@
1
+ // Generated from <%= script_name %>
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 com.shopify.graphql.support.AbstractResponse;
10
+ import com.shopify.graphql.support.Arguments;
11
+ import com.shopify.graphql.support.Error;
12
+ import com.shopify.graphql.support.Query;
13
+ import com.shopify.graphql.support.SchemaViolationError;
14
+ import com.shopify.graphql.support.TopLevelResponse;
15
+ import com.shopify.graphql.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 Operations {
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
+ <% end %>
36
+ }
@@ -0,0 +1,113 @@
1
+ // Generated from <%= script_name %>
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 com.shopify.graphql.support.AbstractResponse;
10
+ import com.shopify.graphql.support.Arguments;
11
+ import com.shopify.graphql.support.Error;
12
+ import com.shopify.graphql.support.Query;
13
+ import com.shopify.graphql.support.SchemaViolationError;
14
+ import com.shopify.graphql.support.TopLevelResponse;
15
+ import com.shopify.graphql.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
+
26
+ <% fields = type.fields(include_deprecated: include_deprecated) || [] %>
27
+ <%= java_doc(type) %>
28
+ public class <%= type.name %>Query extends Query<<%= type.name %>Query> {
29
+ <%= type.name %>Query(StringBuilder _queryBuilder) {
30
+ super(_queryBuilder);
31
+ <% if type.object? && type.implement?("Node") %>
32
+ startField("id");
33
+ <% end %>
34
+ <% unless type.object? %>
35
+ startField("__typename");
36
+ <% end %>
37
+ }
38
+
39
+ <% fields.each do |field| %>
40
+ <% next if field.name == "id" && type.object? && type.implement?("Node") %>
41
+ <% unless field.optional_args.empty? %>
42
+ public class <%= field.classify_name %>Arguments extends Arguments {
43
+ <%= field.classify_name %>Arguments(StringBuilder _queryBuilder) {
44
+ super(_queryBuilder, <%= !!field.required_args.empty? %>);
45
+ }
46
+
47
+ <% field.optional_args.each do |arg| %>
48
+ <%= java_doc(arg) %>
49
+ public <%= field.classify_name %>Arguments <%= escape_reserved_word(arg.camelize_name) %>(<%= java_input_type(arg.type) %> value) {
50
+ if (value != null) {
51
+ startArgument("<%= arg.name %>");
52
+ <%= generate_build_input_code('value', arg.type) %>
53
+ }
54
+ return this;
55
+ }
56
+ <% end %>
57
+ }
58
+
59
+ public interface <%= field.classify_name %>ArgumentsDefinition {
60
+ void define(<%= field.classify_name %>Arguments args);
61
+ }
62
+
63
+ <%= java_doc(field) %>
64
+ public <%= type.name %>Query <%= escape_reserved_word(field.camelize_name) %>(<%= java_arg_defs(field, skip_optional: true) %>) {
65
+ return <%= escape_reserved_word(field.camelize_name) %>(<%= java_arg_expresions_with_empty_optional_args(field) %>);
66
+ }
67
+ <% end %>
68
+
69
+ <%= java_doc(field) %>
70
+ <%= field.deprecated? ? "@Deprecated\n" : '' -%>
71
+ public <%= type.name %>Query <%= escape_reserved_word(field.camelize_name) %>(<%= java_arg_defs(field) %>) {
72
+ startField("<%= field.name %>");
73
+ <% unless field.args.empty? %>
74
+ <% if field.required_args.empty? %>
75
+ <%= field.classify_name %>Arguments args = new <%= field.classify_name %>Arguments(_queryBuilder);
76
+ argsDef.define(args);
77
+ <%= field.classify_name %>Arguments.end(args);
78
+ <% else %>
79
+ <% field.required_args.each_with_index do |arg, i| %>
80
+ _queryBuilder.append("<%= i == 0 ? "(" : "," %><%= arg.name %>:");
81
+ <%= generate_build_input_code(escape_reserved_word(arg.camelize_name), arg.type) %>
82
+ <% end %>
83
+ <% unless field.optional_args.empty? %>
84
+ argsDef.define(new <%= field.classify_name %>Arguments(_queryBuilder));
85
+ <% end %>
86
+ _queryBuilder.append(')');
87
+ <% end %>
88
+ <% end %>
89
+ <% if field.subfields? %>
90
+ _queryBuilder.append('{');
91
+ queryDef.define(new <%= field.type.unwrap.name %>Query(_queryBuilder));
92
+ _queryBuilder.append('}');
93
+ <% end %>
94
+ return this;
95
+ }
96
+ <% end %>
97
+ <% unless type.object? %>
98
+ <% type.possible_types.each do |possible_type| %>
99
+ public <%= type.name %>Query on<%= possible_type.name %>(<%= possible_type.name %>QueryDefinition queryDef) {
100
+ startInlineFragment("<%= possible_type.name %>");
101
+ queryDef.define(new <%= possible_type.name %>Query(_queryBuilder));
102
+ _queryBuilder.append('}');
103
+ return this;
104
+ }
105
+ <% end %>
106
+ <% end %>
107
+
108
+ <% if schema.root_name?(type.name) %>
109
+ public String toString() {
110
+ return _queryBuilder.toString();
111
+ }
112
+ <% end %>
113
+ }
@@ -0,0 +1,29 @@
1
+ // Generated from <%= script_name %>
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 com.shopify.graphql.support.AbstractResponse;
10
+ import com.shopify.graphql.support.Arguments;
11
+ import com.shopify.graphql.support.Error;
12
+ import com.shopify.graphql.support.Query;
13
+ import com.shopify.graphql.support.SchemaViolationError;
14
+ import com.shopify.graphql.support.TopLevelResponse;
15
+ import com.shopify.graphql.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
+
26
+ <% fields = type.fields(include_deprecated: include_deprecated) || [] %>
27
+ public interface <%= type.name %>QueryDefinition {
28
+ void define(<%= type.name %>Query _queryBuilder);
29
+ }
@@ -0,0 +1,55 @@
1
+ // Generated from <%= script_name %>
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 com.shopify.graphql.support.AbstractResponse;
10
+ import com.shopify.graphql.support.Arguments;
11
+ import com.shopify.graphql.support.Error;
12
+ import com.shopify.graphql.support.Query;
13
+ import com.shopify.graphql.support.SchemaViolationError;
14
+ import com.shopify.graphql.support.TopLevelResponse;
15
+ import com.shopify.graphql.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 <%= response_type %>Response {
26
+ private TopLevelResponse response;
27
+ private <%= root_name %> data;
28
+
29
+ public <%= response_type %>Response(TopLevelResponse response) throws SchemaViolationError {
30
+ this.response = response;
31
+ this.data = response.getData() != null ? new <%= root_name %>(response.getData()) : null;
32
+ }
33
+
34
+ public <%= root_name %> getData() {
35
+ return data;
36
+ }
37
+
38
+ public List<Error> getErrors() {
39
+ return response.getErrors();
40
+ }
41
+
42
+ public String toJson() {
43
+ return new Gson().toJson(response);
44
+ }
45
+
46
+ public String prettyPrintJson() {
47
+ final Gson gson = new GsonBuilder().setPrettyPrinting().create();
48
+ return gson.toJson(response);
49
+ }
50
+
51
+ public static <%= response_type %>Response fromJson(String json) throws SchemaViolationError {
52
+ final TopLevelResponse response = new Gson().fromJson(json, TopLevelResponse.class);
53
+ return new <%= response_type %>Response(response);
54
+ }
55
+ }
@@ -0,0 +1,7 @@
1
+ // Generated from <%= script_name %>
2
+
3
+ package <%= package_name %>;
4
+
5
+ public class Schema {
6
+ public static String VERSION = "<%= version %>";
7
+ }
@@ -1,3 +1,3 @@
1
1
  class GraphQLJavaGen
2
- VERSION = "0.1.1"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: graphql_java_gen
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dylan Thacker-Smith
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-10-19 00:00:00.000000000 Z
11
+ date: 2020-04-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: graphql_schema
@@ -30,14 +30,14 @@ dependencies:
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '1.14'
33
+ version: '2.1'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '1.14'
40
+ version: '2.1'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rake
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -95,6 +95,15 @@ files:
95
95
  - codegen/lib/graphql_java_gen/reformatter.rb
96
96
  - codegen/lib/graphql_java_gen/scalar.rb
97
97
  - codegen/lib/graphql_java_gen/templates/APISchema.java.erb
98
+ - codegen/lib/graphql_java_gen/templates/Enum.java.erb
99
+ - codegen/lib/graphql_java_gen/templates/Input.java.erb
100
+ - codegen/lib/graphql_java_gen/templates/Interface.java.erb
101
+ - codegen/lib/graphql_java_gen/templates/Object.java.erb
102
+ - codegen/lib/graphql_java_gen/templates/Operations.java.erb
103
+ - codegen/lib/graphql_java_gen/templates/Query.java.erb
104
+ - codegen/lib/graphql_java_gen/templates/QueryDefinition.java.erb
105
+ - codegen/lib/graphql_java_gen/templates/Responses.java.erb
106
+ - codegen/lib/graphql_java_gen/templates/Schema.java.erb
98
107
  - codegen/lib/graphql_java_gen/version.rb
99
108
  homepage: https://github.com/Shopify/graphql_java_gen
100
109
  licenses:
@@ -115,8 +124,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
115
124
  - !ruby/object:Gem::Version
116
125
  version: '0'
117
126
  requirements: []
118
- rubyforge_project:
119
- rubygems_version: 2.6.14
127
+ rubygems_version: 3.1.2
120
128
  signing_key:
121
129
  specification_version: 4
122
130
  summary: GraphQL java client code generator