Skip to content

Add a way to unset an input property #29

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Sep 7, 2017
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions codegen/lib/graphql_java_gen/templates/APISchema.java.erb
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,12 @@ public class <%= schema_name %> {
this.<%= escape_reserved_word(field.camelize_name) %> = <%= escape_reserved_word(field.camelize_name) %>;
return this;
}

// Unsets the <%= escape_reserved_word(field.camelize_name) %> property so that it is not serialized
public void unset<%= field.classify_name %>() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't make sense to have this method for required fields, as is the case here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense so I fixed this.
This comment was on the optional field generator ☝️

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

originally the comment was for required fields, but github moved it after that got removed.

this.<%= escape_reserved_word(field.camelize_name) %> = null;
}

<% end %>
<% type.optional_input_fields.each do |field| %>
<%= java_annotations(field) %>
Expand All @@ -298,6 +304,13 @@ public class <%= schema_name %> {
this.<%= field.camelize_name %>Seen = true;
return this;
}

// Unsets the <%= escape_reserved_word(field.camelize_name) %> property so that it is not serialized
public void unset<%= field.classify_name %>() {
this.<%= escape_reserved_word(field.camelize_name) %> = null;
this.<%= field.camelize_name %>Seen = false;
}

<% end %>

public void appendTo(StringBuilder _queryBuilder) {
Expand Down
28 changes: 28 additions & 0 deletions support/src/test/java/com/shopify/graphql/support/Generated.java
Original file line number Diff line number Diff line change
Expand Up @@ -1100,6 +1100,11 @@ public SetIntegerInput setKey(String key) {
return this;
}

// Unsets the key property so that it is not serialized
public void unsetKey() {
this.key = null;
}

public int getValue() {
return value;
}
Expand All @@ -1109,6 +1114,11 @@ public SetIntegerInput setValue(int value) {
return this;
}

// Unsets the value property so that it is not serialized
public void unsetValue() {
this.value = null;
}

@Nullable
public LocalDateTime getTtl() {
return ttl;
Expand All @@ -1120,6 +1130,12 @@ public SetIntegerInput setTtl(@Nullable LocalDateTime ttl) {
return this;
}

// Unsets the ttl property so that it is not serialized
public void unsetTtl() {
this.ttl = null;
this.ttlSeen = false;
}

@Nullable
public Boolean getNegate() {
return negate;
Expand All @@ -1131,6 +1147,12 @@ public SetIntegerInput setNegate(@Nullable Boolean negate) {
return this;
}

// Unsets the negate property so that it is not serialized
public void unsetNegate() {
this.negate = null;
this.negateSeen = false;
}

@Nullable
public String getApiClient() {
return apiClient;
Expand All @@ -1142,6 +1164,12 @@ public SetIntegerInput setApiClient(@Nullable String apiClient) {
return this;
}

// Unsets the apiClient property so that it is not serialized
public void unsetApiClient() {
this.apiClient = null;
this.apiClientSeen = false;
}

public void appendTo(StringBuilder _queryBuilder) {
String separator = "";
_queryBuilder.append('{');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,4 +164,20 @@ public void testOptionalFieldOnInput() throws Exception {
).toString();
assertEquals("mutation{set_integer(input:{key:\"answer\",value:42,ttl:null})}", queryString);
}

@Test
public void testUnsetRequiredlFieldOnInput() throws Exception {
String queryString = Generated.mutation(mutation -> mutation
.setInteger(new Generated.SetIntegerInput("answer", 42).setTtl(null).unsetValue())
).toString();
assertEquals("mutation{set_integer(input:{key:\"answer\",ttl:null})}", queryString);
}

@Test
public void testUnsetOptionalFieldOnInput() throws Exception {
String queryString = Generated.mutation(mutation -> mutation
.setInteger(new Generated.SetIntegerInput("answer", 42).setTtl(null).unsetTtl())
).toString();
assertEquals("mutation{set_integer(input:{key:\"answer\",value:42})}", queryString);
}
}