diff --git a/codegen/lib/graphql_java_gen/templates/APISchema.java.erb b/codegen/lib/graphql_java_gen/templates/APISchema.java.erb index 213fb22..ced892e 100644 --- a/codegen/lib/graphql_java_gen/templates/APISchema.java.erb +++ b/codegen/lib/graphql_java_gen/templates/APISchema.java.erb @@ -299,7 +299,7 @@ public class <%= schema_name %> { } public <%= type.name %> set<%= field.classify_name %>(<%= java_annotations(field, in_argument: true) %><%= java_input_type(field.type) %> <%= escape_reserved_word(field.camelize_name) %>) { - this.<%= escape_reserved_word(field.camelize_name) %> = Input.value(<%= escape_reserved_word(field.camelize_name) %>); + this.<%= escape_reserved_word(field.camelize_name) %> = Input.optional(<%= escape_reserved_word(field.camelize_name) %>); return this; } diff --git a/support/src/main/java/com/shopify/graphql/support/Input.java b/support/src/main/java/com/shopify/graphql/support/Input.java index 4471047..8440254 100644 --- a/support/src/main/java/com/shopify/graphql/support/Input.java +++ b/support/src/main/java/com/shopify/graphql/support/Input.java @@ -15,6 +15,10 @@ public static Input value(@Nullable T value) { return new Input<>(value, true); } + public static Input optional(@Nullable T value) { + return value != null ? value(value) : Input.undefined(); + } + public static Input undefined() { return new Input<>(null, false); } diff --git a/support/src/test/java/com/shopify/graphql/support/Generated.java b/support/src/test/java/com/shopify/graphql/support/Generated.java index e9258b6..c556d5b 100644 --- a/support/src/test/java/com/shopify/graphql/support/Generated.java +++ b/support/src/test/java/com/shopify/graphql/support/Generated.java @@ -1117,7 +1117,7 @@ public Input getTtlInput() { } public SetIntegerInput setTtl(@Nullable LocalDateTime ttl) { - this.ttl = Input.value(ttl); + this.ttl = Input.optional(ttl); return this; } @@ -1139,7 +1139,7 @@ public Input getNegateInput() { } public SetIntegerInput setNegate(@Nullable Boolean negate) { - this.negate = Input.value(negate); + this.negate = Input.optional(negate); return this; } @@ -1161,7 +1161,7 @@ public Input getApiClientInput() { } public SetIntegerInput setApiClient(@Nullable String apiClient) { - this.apiClient = Input.value(apiClient); + this.apiClient = Input.optional(apiClient); return this; } diff --git a/support/src/test/java/com/shopify/graphql/support/IntegrationTest.java b/support/src/test/java/com/shopify/graphql/support/IntegrationTest.java index 551df9b..046e078 100644 --- a/support/src/test/java/com/shopify/graphql/support/IntegrationTest.java +++ b/support/src/test/java/com/shopify/graphql/support/IntegrationTest.java @@ -163,7 +163,7 @@ public void testOptionalFieldOnInput() throws Exception { String queryString = Generated.mutation(mutation -> mutation .setInteger(new Generated.SetIntegerInput("answer", 42).setTtl(null)) ).toString(); - assertEquals("mutation{set_integer(input:{key:\"answer\",value:42,ttl:null})}", queryString); + assertEquals("mutation{set_integer(input:{key:\"answer\",value:42})}", queryString); } @Test