Skip to content

Commit cb786c4

Browse files
authored
Merge pull request #29 from Shopify/clear-nullable-input
Add a way to unset an input property
2 parents 1f84b1c + 016fb32 commit cb786c4

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,7 @@ public class <%= schema_name %> {
286286
this.<%= escape_reserved_word(field.camelize_name) %> = <%= escape_reserved_word(field.camelize_name) %>;
287287
return this;
288288
}
289+
289290
<% end %>
290291
<% type.optional_input_fields.each do |field| %>
291292
<%= java_annotations(field) %>
@@ -298,6 +299,14 @@ public class <%= schema_name %> {
298299
this.<%= field.camelize_name %>Seen = true;
299300
return this;
300301
}
302+
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;
307+
return this;
308+
}
309+
301310
<% end %>
302311

303312
public void appendTo(StringBuilder _queryBuilder) {

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1120,6 +1120,13 @@ public SetIntegerInput setTtl(@Nullable LocalDateTime ttl) {
11201120
return this;
11211121
}
11221122

1123+
// Unsets the ttl property so that it is not serialized.
1124+
public SetIntegerInput unsetTtl() {
1125+
this.ttl = null;
1126+
this.ttlSeen = false;
1127+
return this;
1128+
}
1129+
11231130
@Nullable
11241131
public Boolean getNegate() {
11251132
return negate;
@@ -1131,6 +1138,13 @@ public SetIntegerInput setNegate(@Nullable Boolean negate) {
11311138
return this;
11321139
}
11331140

1141+
// Unsets the negate property so that it is not serialized.
1142+
public SetIntegerInput unsetNegate() {
1143+
this.negate = null;
1144+
this.negateSeen = false;
1145+
return this;
1146+
}
1147+
11341148
@Nullable
11351149
public String getApiClient() {
11361150
return apiClient;
@@ -1142,6 +1156,13 @@ public SetIntegerInput setApiClient(@Nullable String apiClient) {
11421156
return this;
11431157
}
11441158

1159+
// Unsets the apiClient property so that it is not serialized.
1160+
public SetIntegerInput unsetApiClient() {
1161+
this.apiClient = null;
1162+
this.apiClientSeen = false;
1163+
return this;
1164+
}
1165+
11451166
public void appendTo(StringBuilder _queryBuilder) {
11461167
String separator = "";
11471168
_queryBuilder.append('{');

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,4 +164,12 @@ public void testOptionalFieldOnInput() throws Exception {
164164
).toString();
165165
assertEquals("mutation{set_integer(input:{key:\"answer\",value:42,ttl:null})}", queryString);
166166
}
167+
168+
@Test
169+
public void testUnsetOptionalFieldOnInput() throws Exception {
170+
String queryString = Generated.mutation(mutation -> mutation
171+
.setInteger(new Generated.SetIntegerInput("answer", 42).setTtl(null).unsetTtl())
172+
).toString();
173+
assertEquals("mutation{set_integer(input:{key:\"answer\",value:42})}", queryString);
174+
}
167175
}

0 commit comments

Comments
 (0)