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,70 @@
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
+ <% case type.kind when 'OBJECT', 'INTERFACE', 'UNION' %>
26
+ <% when 'INPUT_OBJECT' %>
27
+ <% when 'ENUM' %>
28
+ <%= java_doc(type) %>
29
+ public enum <%= type.name %> {
30
+ <% enum_values = type.enum_values(include_deprecated: include_deprecated).sort_by{ |value| value.name } %>
31
+ <% enum_values.each do |value| %>
32
+ <%= java_doc(value) %>
33
+ <%= value.deprecated? ? "@Deprecated\n" : '' -%>
34
+ <%= value.upcase_name %>,
35
+ <% end %>
36
+
37
+ UNKNOWN_VALUE;
38
+
39
+ public static <%= type.name %> fromGraphQl(String value) {
40
+ if (value == null) {
41
+ return null;
42
+ }
43
+
44
+ switch (value) {
45
+ <% enum_values.each do |value| %>
46
+ case "<%= value.name %>": {
47
+ return <%= value.upcase_name %>;
48
+ }
49
+ <% end %>
50
+ default: {
51
+ return UNKNOWN_VALUE;
52
+ }
53
+ }
54
+ }
55
+ public String toString() {
56
+ switch (this) {
57
+ <% enum_values.each do |value| %>
58
+ case <%= value.upcase_name %>: {
59
+ return "<%= value.name %>";
60
+ }
61
+ <% end %>
62
+ default: {
63
+ return "";
64
+ }
65
+ }
66
+ }
67
+ }
68
+ <% else %>
69
+ <% raise NotImplementedError, "unhandled #{type.kind} type #{type.name}" %>
70
+ <% end %>
@@ -0,0 +1,66 @@
1
+ <%= render_license %>
2
+
3
+ package <%= package_name %>;
4
+
5
+ import poc.graphql.client.support.SchemaViolationError;
6
+ import poc.graphql.client.GraphQLRequest;
7
+ import poc.graphql.client.impl.GraphQLClientImpl;
8
+ import poc.graphql.client.impl.GraphqlClientConfiguration;
9
+ import org.apache.http.HttpEntity;
10
+ import org.apache.http.HttpResponse;
11
+ import org.apache.http.HttpStatus;
12
+ import org.apache.http.StatusLine;
13
+ import org.apache.http.util.EntityUtils;
14
+ import org.slf4j.Logger;
15
+ import org.slf4j.LoggerFactory;
16
+
17
+ import java.nio.charset.StandardCharsets;
18
+
19
+ public class GQLClient extends GraphQLClientImpl {
20
+ private static final Logger LOGGER = LoggerFactory.getLogger(GQLClient.class);
21
+
22
+ public GQLClient(GraphqlClientConfiguration configuration) throws Exception {
23
+ super(configuration);
24
+ }
25
+
26
+ /**
27
+ * Execute() with link to generated DTO.
28
+ * @param query
29
+ * @return
30
+ * @throws SchemaViolationError
31
+ */
32
+ public QueryResponse execute(QueryQuery query) throws SchemaViolationError {
33
+ LOGGER.debug("Executing GraphQL query: " + query.toString());
34
+
35
+ GraphQLRequest request = new GraphQLRequest(query.toString());
36
+ HttpResponse httpResponse;
37
+ try {
38
+ httpResponse = client.execute(buildRequest(request, null));
39
+ } catch (Exception e) {
40
+ throw new RuntimeException("Failed to send GraphQL request", e);
41
+ }
42
+
43
+ StatusLine statusLine = httpResponse.getStatusLine();
44
+ if (HttpStatus.SC_OK == statusLine.getStatusCode()) {
45
+ HttpEntity entity = httpResponse.getEntity();
46
+ String json;
47
+ try {
48
+ json = EntityUtils.toString(entity, StandardCharsets.UTF_8);
49
+ } catch (Exception e) {
50
+ throw new RuntimeException("Failed to read HTTP response content", e);
51
+ }
52
+
53
+ QueryResponse response = QueryResponse.fromJson(json);
54
+
55
+ // We log GraphQL errors because they might otherwise get "silently" unnoticed
56
+ if (response.getErrors() != null) {
57
+ LOGGER.warn("GraphQL request {} returned some errors {}", request.getQuery(), response.getErrors());
58
+ }
59
+
60
+ return response;
61
+ } else {
62
+ EntityUtils.consumeQuietly(httpResponse.getEntity());
63
+ throw new RuntimeException("GraphQL query failed with response code " + statusLine.getStatusCode());
64
+ }
65
+ }
66
+ }
@@ -0,0 +1,103 @@
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
+ <% required_input_fields = type.required_input_fields.sort_by{ |field| field.name } || [] %>
26
+ <% optional_input_fields = type.optional_input_fields.sort_by{ |field| field.name } || [] %>
27
+ public class <%= type.name %> implements Serializable {
28
+ <% required_input_fields.each do |field| %>
29
+ private <%= java_input_type(field.type) %> <%= escape_reserved_word(field.camelize_name) %>;
30
+ <% end %>
31
+ <% optional_input_fields.each do |field| %>
32
+ private Input<<%= java_input_type(field.type) %>> <%= escape_reserved_word(field.camelize_name) %> = Input.undefined();
33
+ <% end %>
34
+
35
+ <% unless required_input_fields.empty? %>
36
+ public <%= type.name %>(<%= required_input_fields.map{ |field| "#{java_input_type(field.type)} #{escape_reserved_word(field.camelize_name)}" }.join(', ') %>) {
37
+ <% required_input_fields.each do |field| %>
38
+ this.<%= escape_reserved_word(field.camelize_name) %> = <%= escape_reserved_word(field.camelize_name) %>;
39
+ <% end %>
40
+ }
41
+ <% end %>
42
+
43
+ <% required_input_fields.each do |field| %>
44
+ <%= java_annotations(field) %>
45
+ public <%= java_input_type(field.type) %> get<%= field.classify_name %>() {
46
+ return <%= escape_reserved_word(field.camelize_name) %>;
47
+ }
48
+
49
+ public <%= type.name %> set<%= field.classify_name %>(<%= java_annotations(field, in_argument: true) %><%= java_input_type(field.type) %> <%= escape_reserved_word(field.camelize_name) %>) {
50
+ this.<%= escape_reserved_word(field.camelize_name) %> = <%= escape_reserved_word(field.camelize_name) %>;
51
+ return this;
52
+ }
53
+
54
+ <% end %>
55
+ <% optional_input_fields.each do |field| %>
56
+ <%= java_annotations(field) %>
57
+ public <%= java_input_type(field.type) %> get<%= field.classify_name %>() {
58
+ return <%= escape_reserved_word(field.camelize_name) %>.getValue();
59
+ }
60
+
61
+ public Input<<%= java_input_type(field.type) %>> get<%= field.classify_name %>Input() {
62
+ return <%= escape_reserved_word(field.camelize_name) %>;
63
+ }
64
+
65
+ public <%= type.name %> set<%= field.classify_name %>(<%= java_annotations(field, in_argument: true) %><%= java_input_type(field.type) %> <%= escape_reserved_word(field.camelize_name) %>) {
66
+ this.<%= escape_reserved_word(field.camelize_name) %> = Input.optional(<%= escape_reserved_word(field.camelize_name) %>);
67
+ return this;
68
+ }
69
+
70
+ public <%= type.name %> set<%= field.classify_name %>Input(Input<<%= java_input_type(field.type) %>> <%= escape_reserved_word(field.camelize_name) %>) {
71
+ if (<%= escape_reserved_word(field.camelize_name) %> == null) {
72
+ throw new IllegalArgumentException("Input can not be null");
73
+ }
74
+ this.<%= escape_reserved_word(field.camelize_name) %> = <%= escape_reserved_word(field.camelize_name) %>;
75
+ return this;
76
+ }
77
+
78
+ <% end %>
79
+
80
+ public void appendTo(StringBuilder _queryBuilder) {
81
+ String separator = "";
82
+ _queryBuilder.append('{');
83
+ <% required_input_fields.each do |field| %>
84
+ _queryBuilder.append(separator);
85
+ separator = ",";
86
+ _queryBuilder.append("<%= field.name %>:");
87
+ <%= generate_build_input_code(escape_reserved_word(field.camelize_name), field.type) %>
88
+ <% end %>
89
+ <% optional_input_fields.each do |field| %>
90
+ if (this.<%= escape_reserved_word(field.camelize_name) %>.isDefined()) {
91
+ _queryBuilder.append(separator);
92
+ separator = ",";
93
+ _queryBuilder.append("<%= field.name %>:");
94
+ if (<%= escape_reserved_word(field.camelize_name) %>.getValue() != null) {
95
+ <%= generate_build_input_code(escape_reserved_word(field.camelize_name).concat(".getValue()"), field.type) %>
96
+ } else {
97
+ _queryBuilder.append("null");
98
+ }
99
+ }
100
+ <% end %>
101
+ _queryBuilder.append('}');
102
+ }
103
+ }
@@ -0,0 +1,39 @@
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.CustomFieldInterface;
12
+ import poc.graphql.client.support.Error;
13
+ import poc.graphql.client.support.AbstractQuery;
14
+ import poc.graphql.client.support.SchemaViolationError;
15
+ import poc.graphql.client.support.TopLevelResponse;
16
+ import poc.graphql.client.support.Input;
17
+ <% imports.each do |import| %>
18
+ import <%= import %>;
19
+ <% end %>
20
+
21
+ import java.io.Serializable;
22
+ import java.util.ArrayList;
23
+ import java.util.List;
24
+ import java.util.Map;
25
+
26
+ <% fields = type.fields(include_deprecated: include_deprecated).sort_by{ |field| field.name } || [] %>
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 %> extends CustomFieldInterface {
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,113 @@
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
+ <% fields = type.fields(include_deprecated: include_deprecated) || [] %>
26
+ <% fields.sort_by{ |field| field.name } %>
27
+ <% class_name = type.object? ? type.name : "Unknown#{type.name}" %>
28
+ <%= java_doc(type) %>
29
+ public class <%= class_name %> extends AbstractResponse<<%= class_name %>> <%= java_implements(type) %>{
30
+ public <%= class_name %>() {
31
+ }
32
+
33
+ public <%= class_name %>(JsonObject fields) throws SchemaViolationError {
34
+ for (Map.Entry<String, JsonElement> field : fields.entrySet()) {
35
+ String key = field.getKey();
36
+ String fieldName = getFieldName(key);
37
+ switch (fieldName) {
38
+ <% fields.each do |field| %>
39
+ case "<%= field.name %>": {
40
+ <% generate_build_output_code("field.getValue()", field.type) do |statements, expr| %>
41
+ <%= statements %>
42
+ responseData.put(key, <%= expr %>);
43
+ <% end %>
44
+ break;
45
+ }
46
+ <% end %>
47
+ case "__typename": {
48
+ responseData.put(key, jsonAsString(field.getValue(), key));
49
+ break;
50
+ }
51
+
52
+ default: {
53
+ readCustomField(fieldName, field.getValue());
54
+ }
55
+ }
56
+ }
57
+ }
58
+
59
+ <% if type.object? && type.implement?("Node") %>
60
+ public <%= class_name %>(<%= scalars['ID'].non_nullable_type %> id) {
61
+ this();
62
+ optimisticData.put("id", id);
63
+ }
64
+ <% end %>
65
+
66
+ <% if type.object? %>
67
+ public String getGraphQlTypeName() {
68
+ return "<%= type.name %>";
69
+ }
70
+ <% else %>
71
+ public static <%= type.name %> create(JsonObject fields) throws SchemaViolationError {
72
+ String typeName = fields.getAsJsonPrimitive("__typename").getAsString();
73
+ switch (typeName) {
74
+ <% type.possible_types.each do |possible_type| %>
75
+ case "<%= possible_type.name %>": {
76
+ return new <%= possible_type.name %>(fields);
77
+ }
78
+ <% end %>
79
+ default: {
80
+ return new <%= class_name %>(fields);
81
+ }
82
+ }
83
+ }
84
+
85
+ public String getGraphQlTypeName() {
86
+ return (String) get("__typename");
87
+ }
88
+ <% end %>
89
+
90
+ <% fields.each do |field| %>
91
+ <%= java_doc(field) %>
92
+ <%= java_annotations(field) %>
93
+ public <%= java_output_type(field.type) %> get<%= field.classify_name %>() {
94
+ return (<%= java_output_type(field.type) %>) get("<%= field.name %>");
95
+ }
96
+
97
+ <% next if field.name == "id" && type.object? && type.implement?("Node") %>
98
+ public <%= class_name %> set<%= field.classify_name %>(<%= java_output_type(field.type) %> arg) {
99
+ optimisticData.put(getKey("<%= field.name %>"), arg);
100
+ return this;
101
+ }
102
+ <% end %>
103
+
104
+ public boolean unwrapsToObject(String key) {
105
+ switch (getFieldName(key)) {
106
+ <% fields.each do |field| %>
107
+ case "<%= field.name %>": return <%= field.type.unwrap.object? %>;
108
+ <% end %>
109
+ default: return false;
110
+ }
111
+ }
112
+ }
113
+
@@ -0,0 +1,36 @@
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 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
+ <%= 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
+
26
+ <% fields = type.fields(include_deprecated: include_deprecated).sort_by{ |field| field.name } || [] %>
27
+ <%= java_doc(type) %>
28
+ public class <%= type.name %>Query extends AbstractQuery<<%= 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
+ }