Skip to content

Commit 743b016

Browse files
committed
Add Input class for supporting nulifying input object
1 parent cb786c4 commit 743b016

File tree

5 files changed

+112
-46
lines changed

5 files changed

+112
-46
lines changed

codegen/lib/graphql_java_gen/templates/APISchema.java.erb

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import com.shopify.graphql.support.Error;
1212
import com.shopify.graphql.support.Query;
1313
import com.shopify.graphql.support.SchemaViolationError;
1414
import com.shopify.graphql.support.TopLevelResponse;
15+
import com.shopify.graphql.support.Input;
1516
<% imports.each do |import| %>
1617
import <%= import %>;
1718
<% end %>
@@ -264,8 +265,7 @@ public class <%= schema_name %> {
264265
private <%= java_input_type(field.type) %> <%= escape_reserved_word(field.camelize_name) %>;
265266
<% end %>
266267
<% type.optional_input_fields.each do |field| %>
267-
private <%= java_input_type(field.type) %> <%= escape_reserved_word(field.camelize_name) %>;
268-
private boolean <%= field.camelize_name %>Seen = false;
268+
private Input<<%= java_input_type(field.type) %>> <%= escape_reserved_word(field.camelize_name) %> = Input.undefined();
269269
<% end %>
270270

271271
<% unless type.required_input_fields.empty? %>
@@ -291,19 +291,23 @@ public class <%= schema_name %> {
291291
<% type.optional_input_fields.each do |field| %>
292292
<%= java_annotations(field) %>
293293
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() {
294298
return <%= escape_reserved_word(field.camelize_name) %>;
295299
}
296300

297301
public <%= type.name %> set<%= field.classify_name %>(<%= java_annotations(field, in_argument: true) %><%= java_input_type(field.type) %> <%= escape_reserved_word(field.camelize_name) %>) {
298-
this.<%= escape_reserved_word(field.camelize_name) %> = <%= escape_reserved_word(field.camelize_name) %>;
299-
this.<%= field.camelize_name %>Seen = true;
302+
this.<%= escape_reserved_word(field.camelize_name) %> = Input.value(<%= escape_reserved_word(field.camelize_name) %>);
300303
return this;
301304
}
302305

303-
// Unsets the <%= escape_reserved_word(field.camelize_name) %> property so that it is not serialized.
304-
public <%= type.name %> unset<%= field.classify_name %>() {
305-
this.<%= escape_reserved_word(field.camelize_name) %> = null;
306-
this.<%= field.camelize_name %>Seen = false;
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) %>;
307311
return this;
308312
}
309313

@@ -319,12 +323,12 @@ public class <%= schema_name %> {
319323
<%= generate_build_input_code(escape_reserved_word(field.camelize_name), field.type) %>
320324
<% end %>
321325
<% type.optional_input_fields.each do |field| %>
322-
if (this.<%= field.camelize_name %>Seen) {
326+
if (this.<%= escape_reserved_word(field.camelize_name) %>.isDefined()) {
323327
_queryBuilder.append(separator);
324328
separator = ",";
325329
_queryBuilder.append("<%= field.name %>:");
326-
if (<%= escape_reserved_word(field.camelize_name) %> != null) {
327-
<%= generate_build_input_code(escape_reserved_word(field.camelize_name), field.type) %>
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) %>
328332
} else {
329333
_queryBuilder.append("null");
330334
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.shopify.graphql.support;
2+
3+
import java.io.Serializable;
4+
5+
/**
6+
* Created by henrytao on 9/7/17.
7+
*/
8+
9+
public final class Input<T> implements Serializable {
10+
11+
private final T value;
12+
private final boolean defined;
13+
14+
public static <T> Input<T> value(@Nullable T value) {
15+
return new Input<>(value, true);
16+
}
17+
18+
public static <T> Input<T> undefined() {
19+
return new Input<>(null, false);
20+
}
21+
22+
private Input(T value, boolean defined) {
23+
this.value = value;
24+
this.defined = defined;
25+
}
26+
27+
public T getValue() {
28+
return value;
29+
}
30+
31+
public boolean isDefined() {
32+
return defined;
33+
}
34+
}

support/src/test/java/com/shopify/graphql/support/Generated.java

Lines changed: 43 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import com.shopify.graphql.support.Query;
1313
import com.shopify.graphql.support.SchemaViolationError;
1414
import com.shopify.graphql.support.TopLevelResponse;
15+
import com.shopify.graphql.support.Input;
1516

1617
import com.shopify.graphql.support.ID;
1718

@@ -1076,14 +1077,11 @@ public static class SetIntegerInput implements Serializable {
10761077

10771078
private int value;
10781079

1079-
private LocalDateTime ttl;
1080-
private boolean ttlSeen = false;
1080+
private Input<LocalDateTime> ttl = Input.undefined();
10811081

1082-
private Boolean negate;
1083-
private boolean negateSeen = false;
1082+
private Input<Boolean> negate = Input.undefined();
10841083

1085-
private String apiClient;
1086-
private boolean apiClientSeen = false;
1084+
private Input<String> apiClient = Input.undefined();
10871085

10881086
public SetIntegerInput(String key, int value) {
10891087
this.key = key;
@@ -1111,55 +1109,67 @@ public SetIntegerInput setValue(int value) {
11111109

11121110
@Nullable
11131111
public LocalDateTime getTtl() {
1112+
return ttl.getValue();
1113+
}
1114+
1115+
public Input<LocalDateTime> getTtlInput() {
11141116
return ttl;
11151117
}
11161118

11171119
public SetIntegerInput setTtl(@Nullable LocalDateTime ttl) {
1118-
this.ttl = ttl;
1119-
this.ttlSeen = true;
1120+
this.ttl = Input.value(ttl);
11201121
return this;
11211122
}
11221123

1123-
// Unsets the ttl property so that it is not serialized.
1124-
public SetIntegerInput unsetTtl() {
1125-
this.ttl = null;
1126-
this.ttlSeen = false;
1124+
public SetIntegerInput setTtlInput(Input<LocalDateTime> ttl) {
1125+
if (ttl == null) {
1126+
throw new IllegalArgumentException("Input can not be null");
1127+
}
1128+
this.ttl = ttl;
11271129
return this;
11281130
}
11291131

11301132
@Nullable
11311133
public Boolean getNegate() {
1134+
return negate.getValue();
1135+
}
1136+
1137+
public Input<Boolean> getNegateInput() {
11321138
return negate;
11331139
}
11341140

11351141
public SetIntegerInput setNegate(@Nullable Boolean negate) {
1136-
this.negate = negate;
1137-
this.negateSeen = true;
1142+
this.negate = Input.value(negate);
11381143
return this;
11391144
}
11401145

1141-
// Unsets the negate property so that it is not serialized.
1142-
public SetIntegerInput unsetNegate() {
1143-
this.negate = null;
1144-
this.negateSeen = false;
1146+
public SetIntegerInput setNegateInput(Input<Boolean> negate) {
1147+
if (negate == null) {
1148+
throw new IllegalArgumentException("Input can not be null");
1149+
}
1150+
this.negate = negate;
11451151
return this;
11461152
}
11471153

11481154
@Nullable
11491155
public String getApiClient() {
1156+
return apiClient.getValue();
1157+
}
1158+
1159+
public Input<String> getApiClientInput() {
11501160
return apiClient;
11511161
}
11521162

11531163
public SetIntegerInput setApiClient(@Nullable String apiClient) {
1154-
this.apiClient = apiClient;
1155-
this.apiClientSeen = true;
1164+
this.apiClient = Input.value(apiClient);
11561165
return this;
11571166
}
11581167

1159-
// Unsets the apiClient property so that it is not serialized.
1160-
public SetIntegerInput unsetApiClient() {
1161-
this.apiClient = null;
1162-
this.apiClientSeen = false;
1168+
public SetIntegerInput setApiClientInput(Input<String> apiClient) {
1169+
if (apiClient == null) {
1170+
throw new IllegalArgumentException("Input can not be null");
1171+
}
1172+
this.apiClient = apiClient;
11631173
return this;
11641174
}
11651175

@@ -1177,34 +1187,34 @@ public void appendTo(StringBuilder _queryBuilder) {
11771187
_queryBuilder.append("value:");
11781188
_queryBuilder.append(value);
11791189

1180-
if (this.ttlSeen) {
1190+
if (this.ttl.isDefined()) {
11811191
_queryBuilder.append(separator);
11821192
separator = ",";
11831193
_queryBuilder.append("ttl:");
1184-
if (ttl != null) {
1185-
Query.appendQuotedString(_queryBuilder, ttl.toString());
1194+
if (ttl.getValue() != null) {
1195+
Query.appendQuotedString(_queryBuilder, ttl.getValue().toString());
11861196
} else {
11871197
_queryBuilder.append("null");
11881198
}
11891199
}
11901200

1191-
if (this.negateSeen) {
1201+
if (this.negate.isDefined()) {
11921202
_queryBuilder.append(separator);
11931203
separator = ",";
11941204
_queryBuilder.append("negate:");
1195-
if (negate != null) {
1196-
_queryBuilder.append(negate);
1205+
if (negate.getValue() != null) {
1206+
_queryBuilder.append(negate.getValue());
11971207
} else {
11981208
_queryBuilder.append("null");
11991209
}
12001210
}
12011211

1202-
if (this.apiClientSeen) {
1212+
if (this.apiClient.isDefined()) {
12031213
_queryBuilder.append(separator);
12041214
separator = ",";
12051215
_queryBuilder.append("api_client:");
1206-
if (apiClient != null) {
1207-
Query.appendQuotedString(_queryBuilder, apiClient.toString());
1216+
if (apiClient.getValue() != null) {
1217+
Query.appendQuotedString(_queryBuilder, apiClient.getValue().toString());
12081218
} else {
12091219
_queryBuilder.append("null");
12101220
}

support/src/test/java/com/shopify/graphql/support/GeneratedMinimal.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import com.shopify.graphql.support.Query;
1313
import com.shopify.graphql.support.SchemaViolationError;
1414
import com.shopify.graphql.support.TopLevelResponse;
15+
import com.shopify.graphql.support.Input;
1516

1617
import com.shopify.graphql.support.ID;
1718

support/src/test/java/com/shopify/graphql/support/IntegrationTest.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.shopify.graphql.support;
22

3+
import java.time.LocalDate;
34
import java.time.LocalDateTime;
45
import java.util.Arrays;
56
import java.util.List;
@@ -166,10 +167,26 @@ public void testOptionalFieldOnInput() throws Exception {
166167
}
167168

168169
@Test
169-
public void testUnsetOptionalFieldOnInput() throws Exception {
170+
public void testOptionalFieldOnInputAsUndefined() throws Exception {
170171
String queryString = Generated.mutation(mutation -> mutation
171-
.setInteger(new Generated.SetIntegerInput("answer", 42).setTtl(null).unsetTtl())
172+
.setInteger(new Generated.SetIntegerInput("answer", 42).setTtlInput(Input.<LocalDateTime>undefined()))
172173
).toString();
173174
assertEquals("mutation{set_integer(input:{key:\"answer\",value:42})}", queryString);
174175
}
176+
177+
@Test
178+
public void testOptionalFieldOnInputAsExplicitNull() throws Exception {
179+
String queryString = Generated.mutation(mutation -> mutation
180+
.setInteger(new Generated.SetIntegerInput("answer", 42).setTtlInput(Input.<LocalDateTime>value(null)))
181+
).toString();
182+
assertEquals("mutation{set_integer(input:{key:\"answer\",value:42,ttl:null})}", queryString);
183+
}
184+
185+
@Test
186+
public void testOptionalFieldOnInputAsInputValue() throws Exception {
187+
String queryString = Generated.mutation(mutation -> mutation
188+
.setInteger(new Generated.SetIntegerInput("answer", 42).setTtlInput(Input.<LocalDateTime>value(LocalDateTime.of(2017, 1, 31, 10, 9, 48))))
189+
).toString();
190+
assertEquals("mutation{set_integer(input:{key:\"answer\",value:42,ttl:\"2017-01-31T10:09:48\"})}", queryString);
191+
}
175192
}

0 commit comments

Comments
 (0)