Skip to content

Allow setting optional fields on input objects to explicit null #28

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 7 commits into from
Aug 8, 2017
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 6 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ task :generate do
deserialize_expr: ->(expr) { "LocalDateTime.parse(jsonAsString(#{expr}, key))" },
imports: ['java.time.LocalDateTime'],
)
],
custom_annotations: [
GraphQLJavaGen::Annotation.new(
'Nullable',
imports: ['com.shopify.graphql.support.Nullable']
) { |field| !field.type.non_null? },
]
).save('support/src/test/java/com/shopify/graphql/support/Generated.java')

Expand Down
13 changes: 10 additions & 3 deletions codegen/lib/graphql_java_gen.rb
Original file line number Diff line number Diff line change
Expand Up @@ -253,10 +253,17 @@ def java_implements(type)
"implements #{interfaces.to_a.join(', ')} "
end

def java_annotations(field)
@annotations.map do |annotation|
def java_annotations(field, in_argument: false)
annotations = @annotations.map do |annotation|
"@#{annotation.name}" if annotation.annotate?(field)
end.compact.join("\n")
end.compact
return "" unless annotations.any?

if in_argument
annotations.join(" ") + " "
else
annotations.join("\n")
end
end

def type_names_set
Expand Down
18 changes: 13 additions & 5 deletions codegen/lib/graphql_java_gen/templates/APISchema.java.erb
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ public class <%= schema_name %> {

<% fields.each do |field| %>
<%= java_doc(field) %>
<%= java_annotations(field) -%>
<%= java_annotations(field) %>
public <%= java_output_type(field.type) %> get<%= field.classify_name %>() {
return (<%= java_output_type(field.type) %>) get("<%= field.name %>");
}
Expand Down Expand Up @@ -265,6 +265,7 @@ public class <%= schema_name %> {
<% end %>
<% type.optional_input_fields.each do |field| %>
private <%= java_input_type(field.type) %> <%= escape_reserved_word(field.camelize_name) %>;
private boolean <%= field.camelize_name %>Seen = false;
<% end %>

<% unless type.required_input_fields.empty? %>
Expand All @@ -276,22 +277,25 @@ public class <%= schema_name %> {
<% end %>

<% type.required_input_fields.each do |field| %>
<%= java_annotations(field) %>
public <%= java_input_type(field.type) %> get<%= field.classify_name %>() {
return <%= escape_reserved_word(field.camelize_name) %>;
}

public <%= type.name %> set<%= field.classify_name %>(<%= java_input_type(field.type) %> <%= escape_reserved_word(field.camelize_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) %> = <%= escape_reserved_word(field.camelize_name) %>;
return this;
}
<% end %>
<% type.optional_input_fields.each do |field| %>
<%= java_annotations(field) %>
public <%= java_input_type(field.type) %> get<%= field.classify_name %>() {
return <%= escape_reserved_word(field.camelize_name) %>;
}

public <%= type.name %> set<%= field.classify_name %>(<%= java_input_type(field.type) %> <%= escape_reserved_word(field.camelize_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) %> = <%= escape_reserved_word(field.camelize_name) %>;
this.<%= field.camelize_name %>Seen = true;
return this;
}
<% end %>
Expand All @@ -306,11 +310,15 @@ public class <%= schema_name %> {
<%= generate_build_input_code(escape_reserved_word(field.camelize_name), field.type) %>
<% end %>
<% type.optional_input_fields.each do |field| %>
if (<%= escape_reserved_word(field.camelize_name) %> != null) {
if (this.<%= field.camelize_name %>Seen) {
_queryBuilder.append(separator);
separator = ",";
_queryBuilder.append("<%= field.name %>:");
<%= generate_build_input_code(escape_reserved_word(field.camelize_name), field.type) %>
if (<%= escape_reserved_word(field.camelize_name) %> != null) {
<%= generate_build_input_code(escape_reserved_word(field.camelize_name), field.type) %>
} else {
_queryBuilder.append("null");
}
}
<% end %>
_queryBuilder.append('}');
Expand Down
1 change: 1 addition & 0 deletions codegen/test/support/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ module Schema
argument :value, !types.Int
argument :ttl, TimeType
argument :negate, types.Boolean, default_value: false
argument :api_client, types.String
end

MutationType = GraphQL::ObjectType.define do
Expand Down
46 changes: 46 additions & 0 deletions support/src/main/java/com/shopify/graphql/support/Nullable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright (C) 2013 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.shopify.graphql.support;

import static java.lang.annotation.ElementType.ANNOTATION_TYPE;
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.PACKAGE;
import static java.lang.annotation.ElementType.PARAMETER;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

/**
* Denotes that a parameter, field or method return value can be null.
* <p>
* When decorating a method call parameter, this denotes that the parameter can
* legitimately be null and the method will gracefully deal with it. Typically
* used on optional parameters.
* <p>
* When decorating a method, this denotes the method might legitimately return
* null.
* <p>
* This is a marker annotation and it has no specific attributes.
*/
@Documented
@Retention(RUNTIME)
@Target({METHOD, PARAMETER, FIELD, ANNOTATION_TYPE, PACKAGE})
public @interface Nullable {
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.shopify.graphql.support;

import org.junit.Test;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

import static junit.framework.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import com.shopify.graphql.support.Generated;

public class AnnotationTest {
@Test
public void testAppliesAnnotation() throws Exception {
Class<Generated> obj = Generated.class;
boolean foundNullable = false;
for (Class klass: obj.getDeclaredClasses()) {
for (Method method : klass.getDeclaredMethods()) {
if (method.isAnnotationPresent(Nullable.class)) {
foundNullable = true;
break;
}
}
}
assertTrue("Should have found a class with @Nullable annotation in Generated.java", foundNullable);
}
}
Loading