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
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
sudo: false
language: ruby
before_install: gem install bundler -v 1.14.3
before_install: gem install bundler -v 2.1.2
rvm:
- 2.3.3
jdk:
- oraclejdk8
- oraclejdk9

# https://docs.travis-ci.com/user/languages/java/#Caching
# recommends adding this for the gradle cache
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ schema = GraphQLSchema.new(JSON.parse(introspection_result))
GraphQLJavaGen.new(schema,
package_name: "com.example.MyApp",
nest_under: 'ExampleSchema',
version: '2020-01',
custom_scalars: [
GraphQLJavaGen::Scalar.new(
type_name: 'Decimal',
Expand Down Expand Up @@ -76,6 +77,7 @@ schema = GraphQLSchema.new(JSON.parse(introspection_result))
GraphQLJavaGen.new(schema,
package_name: "com.example.MyApp",
nest_under: 'ExampleSchema',
version: '2020-01',
custom_scalars: [
GraphQLJavaGen::Scalar.new(
type_name: 'Decimal',
Expand Down
2 changes: 2 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ task :generate do
GraphQLSchema.new(Support::Schema.introspection_result),
package_name: 'com.shopify.graphql.support',
nest_under: 'Generated',
version: '2020-01',
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Should this be hardcoded?

custom_scalars: [
GraphQLJavaGen::Scalar.new(
type_name: 'Time',
Expand All @@ -38,6 +39,7 @@ task :generate do
GraphQLSchema.new(Support::Schema.introspection_result(Support::Schema::MinimalSchema)),
package_name: 'com.shopify.graphql.support',
nest_under: 'GeneratedMinimal',
version: '2020-01'
).save('support/src/test/java/com/shopify/graphql/support/GeneratedMinimal.java')
end

Expand Down
10 changes: 8 additions & 2 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, :include_deprecated
attr_reader :schema, :package_name, :scalars, :imports, :script_name, :schema_name, :include_deprecated, :version

def initialize(schema,
package_name:, nest_under:, script_name: 'graphql_java_gen gem',
custom_scalars: [], custom_annotations: [], include_deprecated: false
custom_scalars: [], custom_annotations: [], include_deprecated: false, version: ''
)
@schema = schema
@schema_name = nest_under
Expand All @@ -22,13 +22,15 @@ def initialize(schema,
@annotations = custom_annotations
@imports = (@scalars.values.map(&:imports) + @annotations.map(&:imports)).flatten.sort.uniq
@include_deprecated = include_deprecated
@version = version
end

def save(path)
File.write(path, generate)
end

def save_granular(path)
write_schema(path)
write_static_methods(path)
write_response(path, :query, schema.query_root_name)
write_response(path, :mutation, schema.mutation_root_name)
Expand Down Expand Up @@ -66,6 +68,10 @@ def generate_entity(template, type)
reformat(erb_template.result(binding))
end

def write_schema(path)
File.write(path + "/Schema.java", reformat(erb_for_entity("Schema.java").result(binding)))
end

def write_static_methods(path)
File.write(path + "/Operations.java", reformat(erb_for_entity("Operations.java").result(binding)))
end
Expand Down
2 changes: 2 additions & 0 deletions codegen/lib/graphql_java_gen/templates/APISchema.java.erb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import java.util.List;
import java.util.Map;

public class <%= schema_name %> {
public static final String API_VERSION = "<%= version %>";

<% [[:query, schema.query_root_name], [:mutation, schema.mutation_root_name]].each do |operation_type, root_name| %>
<% next unless root_name %>
public static <%= root_name %>Query <%= operation_type %>(<%= root_name %>QueryDefinition queryDef) {
Expand Down
7 changes: 7 additions & 0 deletions codegen/lib/graphql_java_gen/templates/Schema.java.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Generated from <%= script_name %>

package <%= package_name %>;

public class Schema {
public static String VERSION = "<%= version %>";
}
6 changes: 3 additions & 3 deletions codegen/test/graphql_java_gen_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ def test_that_it_has_a_version_number
end

def test_default_script_name
output = GraphQLJavaGen.new(MINIMAL_SCHEMA, **required_args).generate
output = GraphQLJavaGen.new(MINIMAL_SCHEMA, version: '2020-01', **required_args).generate
assert_match %r{\A// Generated from graphql_java_gen gem$}, output
end

def test_script_name_option
output = GraphQLJavaGen.new(MINIMAL_SCHEMA, script_name: 'script/update_schema', **required_args).generate
output = GraphQLJavaGen.new(MINIMAL_SCHEMA, script_name: 'script/update_schema', version: '2020-01', **required_args).generate
assert_match %r{\A// Generated from script/update_schema$}, output
end

def test_generate
refute_empty GraphQLJavaGen.new(LARGER_SCHEMA, **required_args).generate
refute_empty GraphQLJavaGen.new(LARGER_SCHEMA, version: '2020-01', **required_args).generate
end

private
Expand Down
2 changes: 1 addition & 1 deletion graphql_java_gen.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Gem::Specification.new do |spec|

spec.add_dependency "graphql_schema", "~> 0.1.1"

spec.add_development_dependency "bundler", "~> 1.14"
spec.add_development_dependency "bundler", "~> 2.1"
spec.add_development_dependency "rake", "~> 12.0"
spec.add_development_dependency "minitest", "~> 5.10"
spec.add_development_dependency "graphql", "~> 1.3"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import java.util.Map;

public class Generated {
public static final String API_VERSION = "2020-01";

public static QueryRootQuery query(QueryRootQueryDefinition queryDef) {
StringBuilder queryString = new StringBuilder("{");
QueryRootQuery query = new QueryRootQuery(queryString);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import java.util.Map;

public class GeneratedMinimal {
public static final String API_VERSION = "2020-01";

public static QueryRootQuery query(QueryRootQueryDefinition queryDef) {
StringBuilder queryString = new StringBuilder("{");
QueryRootQuery query = new QueryRootQuery(queryString);
Expand Down