graphql_java_gen 0.1.0 → 0.1.1

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
2
  SHA1:
3
- metadata.gz: a3088c3fd787e2d815792f8bb8f6a0b3fe414829
4
- data.tar.gz: 1058513015d8fe05d201f7e72ef5f3a007a94b8a
3
+ metadata.gz: '09c51e3a3cd35736ed828139d47f92bb6386fbe1'
4
+ data.tar.gz: 5388a33adb2994b597b8fa9d380c519115363b93
5
5
  SHA512:
6
- metadata.gz: a3d82e27f2ca1cfbf3a7b7c082a5a75d631607f32f1d6bc2e7fdb54840ed023814ce81c8fe4d6e65bf4576a0c28bb1eadbb988aecb8593362935fa2603648e18
7
- data.tar.gz: 1c040d36778c54054772af31b651ac30db8aef05f77812c158e51cbcf33ab98b171d942d1aaf1837439a84de2dcbeefab0063a6e43eb2455e40b666d5ff86d79
6
+ metadata.gz: 858c440553a22269df660fdd3560e7d3940bf02357984d4bad1543e7913b1f5e1c3a192834e6dc2cd8b37471b102ae1baa6d15e95fc7aadfa1bbeac7e9c57fc9
7
+ data.tar.gz: 2dc257fbbdae8e1d84d0202605c279fbaea372129cb42cbce7cf6f783baa808bb0791ee5ff5bf532821a3f7c178ddfa9c02434279a3a8e3e6cd89bac6f21a829
data/README.md CHANGED
@@ -1,4 +1,5 @@
1
1
  # GraphQLJavaGen
2
+ [![Build Status](https://travis-ci.org/Shopify/graphql_java_gen.svg?branch=master)](https://travis-ci.org/Shopify/graphql_java_gen)
2
3
 
3
4
  Generate code for a specific GraphQL schema that provides query
4
5
  builders and response classes.
@@ -10,27 +11,21 @@ The code generator requires ruby version 2.1 or later.
10
11
  It is recommended to use [bundler](http://bundler.io/) to install
11
12
  the code generators ruby package.
12
13
 
13
- Until this project is released, it is recommended to include it into
14
- a project as a git submodule.
15
-
16
- $ git submodule https://github.com/Shopify/graphql_java_gen.git
17
-
18
14
  Add this line to your application's Gemfile:
19
15
 
20
16
  ```ruby
21
- gem 'graphql_java_gen', path: 'graphql_java_gen'
17
+ gem 'graphql_java_gen'
22
18
  ```
23
19
 
24
20
  And then execute:
25
21
 
26
22
  $ bundle
27
23
 
28
- The generated code depends on the com.shopify.graphql.support package
29
- which is in the support directory of this repo. Create a symlink to
30
- include that project in a [gradle multi-project build
31
- ](https://docs.gradle.org/current/userguide/multi_project_builds.html)
24
+ The generated code depends on the com.shopify.graphql.support java
25
+ package. This can be added to a gradle project by adding the following
26
+ jCenter dependancy to you `build.gradle` file:
32
27
 
33
- $ ln -s graphql_java_gen/support GraphQLSupport
28
+ compile 'com.shopify.graphql.support:support:0.1.0'
34
29
 
35
30
  ## Usage
36
31
 
@@ -247,8 +247,9 @@ class GraphQLJavaGen
247
247
 
248
248
  def java_implements(type)
249
249
  return "implements #{type.name} " unless type.object?
250
- return "" if type.interfaces.empty?
251
- "implements #{type.interfaces.map{ |interface| interface.name }.join(', ')} "
250
+ interfaces = abstract_types.fetch(type.name)
251
+ return "" if interfaces.empty?
252
+ "implements #{interfaces.to_a.join(', ')} "
252
253
  end
253
254
 
254
255
  def java_annotations(field)
@@ -260,4 +261,36 @@ class GraphQLJavaGen
260
261
  def type_names_set
261
262
  @type_names_set ||= schema.types.map(&:name).to_set
262
263
  end
264
+
265
+ def abstract_types
266
+ @abstract_types ||= schema.types.each_with_object({}) do |type, result|
267
+ case type.kind
268
+ when 'OBJECT'
269
+ result[type.name] ||= Set.new
270
+ when 'INTERFACE', 'UNION'
271
+ type.possible_types.each do |possible_type|
272
+ (result[possible_type.name] ||= Set.new).add(type.name)
273
+ end
274
+ end
275
+ end
276
+ end
277
+
278
+ def java_doc(element)
279
+ doc = ''
280
+ unless element.description.nil?
281
+ description = wrap_text(element.description, 100)
282
+ description = description.chomp("\n").gsub("\n", "\n* ")
283
+ doc << "/**\n"
284
+ doc << '* '
285
+ doc << description
286
+ doc << "\n*/"
287
+ end
288
+ doc
289
+ end
290
+
291
+ def wrap_text(text, col_width=80)
292
+ text.gsub!( /(\S{#{col_width}})(?=\S)/, '\1 ' )
293
+ text.gsub!( /(.{1,#{col_width}})(?:\s+|$)/, "\\1\n" )
294
+ text
295
+ end
263
296
  end
@@ -23,7 +23,7 @@ import java.util.Map;
23
23
 
24
24
  public class <%= schema_name %> {
25
25
  <% [[:query, schema.query_root_name], [:mutation, schema.mutation_root_name]].each do |operation_type, root_name| %>
26
- <% next unless schema.mutation_root_name %>
26
+ <% next unless root_name %>
27
27
  public static <%= root_name %>Query <%= operation_type %>(<%= root_name %>QueryDefinition queryDef) {
28
28
  StringBuilder queryString = new StringBuilder("<%= operation_type unless operation_type == :query %>{");
29
29
  <%= root_name %>Query query = new <%= root_name %>Query(queryString);
@@ -67,10 +67,12 @@ public class <%= schema_name %> {
67
67
 
68
68
  <% schema.types.reject{ |type| type.name.start_with?('__') || type.scalar? }.each do |type| %>
69
69
  <% case type.kind when 'OBJECT', 'INTERFACE', 'UNION' %>
70
+ <% fields = type.fields || [] %>
70
71
  public interface <%= type.name %>QueryDefinition {
71
72
  void define(<%= type.name %>Query _queryBuilder);
72
73
  }
73
74
 
75
+ <%= java_doc(type) %>
74
76
  public static class <%= type.name %>Query extends Query<<%= type.name %>Query> {
75
77
  <%= type.name %>Query(StringBuilder _queryBuilder) {
76
78
  super(_queryBuilder);
@@ -82,7 +84,7 @@ public class <%= schema_name %> {
82
84
  <% end %>
83
85
  }
84
86
 
85
- <% type.fields.each do |field| %>
87
+ <% fields.each do |field| %>
86
88
  <% next if field.name == "id" && type.object? && type.implement?("Node") %>
87
89
  <% unless field.optional_args.empty? %>
88
90
  public class <%= field.classify_name %>Arguments extends Arguments {
@@ -91,6 +93,7 @@ public class <%= schema_name %> {
91
93
  }
92
94
 
93
95
  <% field.optional_args.each do |arg| %>
96
+ <%= java_doc(arg) %>
94
97
  public <%= field.classify_name %>Arguments <%= escape_reserved_word(arg.camelize_name) %>(<%= java_input_type(arg.type) %> value) {
95
98
  if (value != null) {
96
99
  startArgument("<%= arg.name %>");
@@ -105,11 +108,13 @@ public class <%= schema_name %> {
105
108
  void define(<%= field.classify_name %>Arguments args);
106
109
  }
107
110
 
111
+ <%= java_doc(field) %>
108
112
  public <%= type.name %>Query <%= escape_reserved_word(field.camelize_name) %>(<%= java_arg_defs(field, skip_optional: true) %>) {
109
113
  return <%= escape_reserved_word(field.camelize_name) %>(<%= java_arg_expresions_with_empty_optional_args(field) %>);
110
114
  }
111
115
  <% end %>
112
116
 
117
+ <%= java_doc(field) %>
113
118
  public <%= type.name %>Query <%= escape_reserved_word(field.camelize_name) %>(<%= java_arg_defs(field) %>) {
114
119
  startField("<%= field.name %>");
115
120
  <% unless field.args.empty? %>
@@ -155,15 +160,20 @@ public class <%= schema_name %> {
155
160
  }
156
161
 
157
162
  <% unless type.object? %>
158
- public interface <%= type.name %> {
163
+ <% if type.name == 'Node' %>
164
+ public interface <%= type.name %> extends com.shopify.graphql.support.Node {
165
+ <% else %>
166
+ public interface <%= type.name %> {
167
+ <% end %>
159
168
  String getGraphQlTypeName();
160
- <% type.fields.each do |field| %>
169
+ <% fields.each do |field| %>
161
170
  <%= java_output_type(field.type) %> get<%= field.classify_name %>();
162
171
  <% end %>
163
172
  }
164
173
  <% end %>
165
174
 
166
175
  <% class_name = type.object? ? type.name : "Unknown#{type.name}" %>
176
+ <%= java_doc(type) %>
167
177
  public static class <%= class_name %> extends AbstractResponse<<%= class_name %>> <%= java_implements(type) %>{
168
178
  public <%= class_name %>() {
169
179
  }
@@ -173,7 +183,7 @@ public class <%= schema_name %> {
173
183
  String key = field.getKey();
174
184
  String fieldName = getFieldName(key);
175
185
  switch (fieldName) {
176
- <% type.fields.each do |field| %>
186
+ <% fields.each do |field| %>
177
187
  case "<%= field.name %>": {
178
188
  <% generate_build_output_code("field.getValue()", field.type) do |statements, expr| %>
179
189
  <%= statements %>
@@ -200,28 +210,6 @@ public class <%= schema_name %> {
200
210
  }
201
211
  <% end %>
202
212
 
203
- <% if type_names_set.include?('Node') %>
204
- public List<Node> getNodes() {
205
- List<Node> children = new ArrayList<>();
206
- <% if type.object? && type.implement?("Node") %>
207
- children.add(this);
208
- <% end %>
209
- <% type.fields.each do |field| %>
210
- <% next unless field.type.unwrap.object? %>
211
- if (get<%= field.classify_name %>() != null) {
212
- <% if field.type.unwrap_non_null.kind == 'LIST' %>
213
- for (<%= java_output_type(field.type.unwrap) %> elem: get<%= field.classify_name %>()) {
214
- children.addAll(elem.getNodes());
215
- }
216
- <% else %>
217
- children.addAll(get<%= field.classify_name %>().getNodes());
218
- <% end %>
219
- }
220
- <% end %>
221
- return children;
222
- }
223
- <% end %>
224
-
225
213
  <% if type.object? %>
226
214
  public String getGraphQlTypeName() {
227
215
  return "<%= type.name %>";
@@ -246,7 +234,8 @@ public class <%= schema_name %> {
246
234
  }
247
235
  <% end %>
248
236
 
249
- <% type.fields.each do |field| %>
237
+ <% fields.each do |field| %>
238
+ <%= java_doc(field) %>
250
239
  <%= java_annotations(field) %>
251
240
  public <%= java_output_type(field.type) %> get<%= field.classify_name %>() {
252
241
  return (<%= java_output_type(field.type) %>) get("<%= field.name %>");
@@ -254,14 +243,14 @@ public class <%= schema_name %> {
254
243
 
255
244
  <% next if field.name == "id" && type.object? && type.implement?("Node") %>
256
245
  public <%= class_name %> set<%= field.classify_name %>(<%= java_output_type(field.type) %> arg) {
257
- optimisticData.put("<%= field.name %>", arg);
246
+ optimisticData.put(getKey("<%= field.name %>"), arg);
258
247
  return this;
259
248
  }
260
249
  <% end %>
261
250
 
262
251
  public boolean unwrapsToObject(String key) {
263
- switch (key) {
264
- <% type.fields.each do |field| %>
252
+ switch (getFieldName(key)) {
253
+ <% fields.each do |field| %>
265
254
  case "<%= field.name %>": return <%= field.type.unwrap.object? %>;
266
255
  <% end %>
267
256
  default: return false;
@@ -327,6 +316,7 @@ public class <%= schema_name %> {
327
316
  }
328
317
  }
329
318
  <% when 'ENUM' %>
319
+ <%= java_doc(type) %>
330
320
  public enum <%= type.name %> {
331
321
  <% type.enum_values.each do |value| %>
332
322
  <%= value.upcase_name %>,
@@ -1,3 +1,3 @@
1
1
  class GraphQLJavaGen
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
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.0
4
+ version: 0.1.1
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-02-01 00:00:00.000000000 Z
11
+ date: 2017-10-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: graphql_schema
@@ -116,7 +116,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
116
116
  version: '0'
117
117
  requirements: []
118
118
  rubyforge_project:
119
- rubygems_version: 2.6.10
119
+ rubygems_version: 2.6.14
120
120
  signing_key:
121
121
  specification_version: 4
122
122
  summary: GraphQL java client code generator