这是indexloc提供的服务,不要输入任何密码
Skip to content
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
23 changes: 17 additions & 6 deletions codegen/lib/graphql_java_gen.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
require 'set'

class GraphQLJavaGen
attr_reader :schema, :package_name, :scalars, :imports, :script_name, :schema_name
attr_reader :schema, :package_name, :scalars, :imports, :script_name, :schema_name, :include_deprecated

def initialize(schema,
package_name:, nest_under:, script_name: 'graphql_java_gen gem',
custom_scalars: [], custom_annotations: []
custom_scalars: [], custom_annotations: [], include_deprecated: false
)
@schema = schema
@schema_name = nest_under
Expand All @@ -21,6 +21,7 @@ def initialize(schema,
@scalars.default_proc = ->(hash, key) { DEFAULT_SCALAR }
@annotations = custom_annotations
@imports = (@scalars.values.map(&:imports) + @annotations.map(&:imports)).flatten.sort.uniq
@include_deprecated = include_deprecated
end

def save(path)
Expand All @@ -37,7 +38,7 @@ class << self
private

def erb_for(template_filename)
erb = ERB.new(File.read(template_filename))
erb = ERB.new(File.read(template_filename), nil, '-')
erb.filename = template_filename
erb
end
Expand Down Expand Up @@ -280,12 +281,22 @@ def java_doc(element)
unless element.description.nil?
description = wrap_text(element.description, 100)
description = description.chomp("\n").gsub("\n", "\n* ")
doc << "/**\n"
doc << '* '
doc << description
doc << "\n*/"
end
doc

if element.respond_to?(:deprecated?) && element.deprecated?
unless doc.empty?
doc << "\n*"
doc << "\n*"
else
doc << '*'
end
doc << ' @deprecated '
Copy link
Contributor

Choose a reason for hiding this comment

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

GraphQL also has deprecated enum values; is the @deprecated annotation valid for them in java?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

this PR addresses deprecation for fields not for types for now, but maybe you are right we should do the same for types

Copy link
Contributor Author

Choose a reason for hiding this comment

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

but if you are asking if this annotation applicable for enum values the answer yes.

doc << element.deprecation_reason
end

doc.empty? ? doc : "/**\n" + doc + "\n*/"
end

def wrap_text(text, col_width=80)
Expand Down
10 changes: 7 additions & 3 deletions codegen/lib/graphql_java_gen/templates/APISchema.java.erb
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public class <%= schema_name %> {

<% schema.types.reject{ |type| type.name.start_with?('__') || type.scalar? }.each do |type| %>
<% case type.kind when 'OBJECT', 'INTERFACE', 'UNION' %>
<% fields = type.fields || [] %>
<% fields = type.fields(include_deprecated: include_deprecated) || [] %>
public interface <%= type.name %>QueryDefinition {
void define(<%= type.name %>Query _queryBuilder);
}
Expand Down Expand Up @@ -115,6 +115,7 @@ public class <%= schema_name %> {
<% end %>

<%= java_doc(field) %>
<%= field.deprecated? ? "@Deprecated\n" : '' -%>
public <%= type.name %>Query <%= escape_reserved_word(field.camelize_name) %>(<%= java_arg_defs(field) %>) {
startField("<%= field.name %>");
<% unless field.args.empty? %>
Expand Down Expand Up @@ -236,7 +237,7 @@ public class <%= schema_name %> {

<% fields.each do |field| %>
<%= java_doc(field) %>
<%= java_annotations(field) %>
<%= java_annotations(field) -%>
Copy link
Contributor

Choose a reason for hiding this comment

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

what does this - do?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

do not generate new line in case if java_annotations(field) resolved to blank line

public <%= java_output_type(field.type) %> get<%= field.classify_name %>() {
return (<%= java_output_type(field.type) %>) get("<%= field.name %>");
}
Expand Down Expand Up @@ -318,9 +319,12 @@ public class <%= schema_name %> {
<% when 'ENUM' %>
<%= java_doc(type) %>
public enum <%= type.name %> {
<% type.enum_values.each do |value| %>
<% type.enum_values(include_deprecated: include_deprecated).each do |value| %>
<%= java_doc(value) %>
<%= value.deprecated? ? "@Deprecated\n" : '' -%>
<%= value.upcase_name %>,
<% end %>

UNKNOWN_VALUE;

public static <%= type.name %> fromGraphQl(String value) {
Expand Down