Skip to content

Commit 07aac56

Browse files
authored
Merge pull request #8 from sshaw/master
Add name, include-columns, and superclass options to type generators
2 parents 5f3adc9 + 6c18968 commit 07aac56

File tree

5 files changed

+101
-56
lines changed

5 files changed

+101
-56
lines changed

lib/generators/gql/gql_generator_base.rb

Lines changed: 62 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,71 @@
22

33
module Gql
44
module GqlGeneratorBase
5-
extend ActiveSupport::Concern
5+
protected
66

7-
included do
8-
protected
7+
# Generate a namedspaced class name with the mutation prefix
8+
def prefixed_class_name(prefix)
9+
(class_path + ["#{prefix}_#{file_name}"]).map!(&:camelize).join("::")
10+
end
911

10-
# Generate a namedspaced class name with the mutation prefix
11-
def prefixed_class_name(prefix)
12-
(class_path + ["#{prefix}_#{file_name}"]).map!(&:camelize).join("::")
13-
end
12+
def type_map
13+
{
14+
integer: 'Int',
15+
string: 'String',
16+
boolean: 'Boolean',
17+
decimal: 'Float',
18+
datetime: 'GraphQL::Types::ISO8601DateTime',
19+
date: 'GraphQL::Types::ISO8601Date',
20+
hstore: 'GraphQL::Types::JSON',
21+
text: 'String',
22+
json: 'GraphQL::Types::JSON',
23+
jsonb: 'GraphQL::Types::JSON'
24+
}
25+
end
1426

15-
def type_map
16-
{
17-
integer: 'Int',
18-
string: 'String',
19-
boolean: 'Boolean',
20-
decimal: 'Float',
21-
datetime: 'GraphQL::Types::ISO8601DateTime',
22-
date: 'GraphQL::Types::ISO8601Date',
23-
hstore: 'GraphQL::Types::JSON',
24-
text: 'String',
25-
json: 'GraphQL::Types::JSON'
26-
}
27-
end
28-
29-
def map_model_types(model_name)
30-
klass = model_name.constantize
31-
associations = klass.reflect_on_all_associations(:belongs_to)
32-
bt_columns = associations.map(&:foreign_key)
33-
34-
klass.columns
35-
.reject { |col| bt_columns.include?(col.name) }
36-
.reject { |col| type_map[col.type].nil? }
37-
.map { |col| {name: col.name, gql_type: type_map[col.type]} }
27+
def map_model_types(model_name)
28+
klass = model_name.constantize
29+
associations = klass.reflect_on_all_associations(:belongs_to)
30+
bt_columns = associations.map(&:foreign_key)
31+
32+
klass.columns
33+
.reject { |col| bt_columns.include?(col.name) }
34+
.reject { |col| type_map[col.type].nil? }
35+
.map do |col|
36+
{
37+
name: col.name,
38+
null: col.null,
39+
gql_type: klass.primary_key == col.name ? 'GraphQL::Types::ID' : type_map[col.type]
40+
}
41+
end
42+
end
43+
44+
def root_directory(namespace)
45+
"app/graphql/#{namespace.underscore}"
46+
end
47+
48+
def wrap_in_namespace(namespace)
49+
namespace = namespace.split('::')
50+
namespace.shift if namespace[0].empty?
51+
52+
code = namespace.each_with_index.map { |name, i| " " * i + "module #{name}" }.join("\n")
53+
code << "\n" << yield(namespace.size) << "\n"
54+
code << (namespace.size - 1).downto(0).map { |i| " " * i + "end" }.join("\n")
55+
code
56+
end
57+
58+
def class_with_fields(namespace, name, superclass, fields)
59+
wrap_in_namespace(namespace) do |indent|
60+
klass = []
61+
klass << sprintf("%sclass %s < %s", " " * indent, name, superclass)
62+
63+
fields.each do |field|
64+
klass << sprintf("%sfield :%s, %s, null: %s", " " * (indent + 1), field[:name], field[:gql_type], field[:null])
65+
end
66+
67+
klass << sprintf("%send", " " * indent)
68+
klass.join("\n")
3869
end
3970
end
4071
end
41-
end
72+
end

lib/generators/gql/input_generator.rb

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,29 @@ module Gql
33
class InputGenerator < Rails::Generators::Base
44
include GqlGeneratorBase
55
source_root File.expand_path('../templates', __FILE__)
6+
67
argument :model_name, type: :string
7-
8+
9+
class_option :name, type: :string
10+
class_option :include_columns, type: :array, default: []
11+
class_option :superclass, type: :string, default: 'Types::BaseInputObject'
12+
class_option :namespace, type: :string, default: 'Types::Input'
13+
814
def generate_input_type
9-
file_name = model_name
15+
name = options['name'] || model_name
16+
superclass = options['superclass']
1017

1118
ignore = ['id', 'created_at', 'updated_at']
12-
@fields = map_model_types(model_name).reject { |field| ignore.include?(field[:name]) }
19+
fields = map_model_types(model_name)
20+
fields.reject! { |field| ignore.include?(field[:name]) }
21+
if options['include_columns'].any?
22+
fields.reject! { |field| !options['include_columns'].include?(field[:name]) }
23+
end
24+
25+
code = class_with_fields(options['namespace'], name, superclass, fields)
26+
file_name = File.join(root_directory(options['namespace']), "#{name.underscore}_input.rb")
1327

14-
template('input_type.rb', "app/graphql/types/input/#{file_name.underscore}_input.rb")
28+
create_file file_name, code
1529
end
1630
end
17-
end
31+
end

lib/generators/gql/model_type_generator.rb

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,28 @@ module Gql
33
class ModelTypeGenerator < Rails::Generators::Base
44
include GqlGeneratorBase
55
source_root File.expand_path('../templates', __FILE__)
6+
67
argument :model_name, type: :string
78

9+
class_option :name, type: :string
10+
class_option :include_columns, type: :array, default: []
11+
class_option :superclass, type: :string, default: 'Types::BaseObject'
12+
class_option :namespace, type: :string, default: 'Types'
13+
814
def type
9-
file_name = "#{model_name.underscore}_type"
10-
@fields = map_model_types(model_name)
11-
template('model_type.rb', "app/graphql/types/#{file_name}.rb")
15+
name = options['name'] || model_name
16+
17+
superclass = options['superclass']
18+
19+
fields = map_model_types(model_name)
20+
if options['include_columns'].any?
21+
fields.reject! { |field| !options['include_columns'].include?(field[:name]) }
22+
end
23+
24+
code = class_with_fields(options['namespace'], name, superclass, fields)
25+
file_name = File.join(root_directory(options['namespace']), "#{name.underscore}_type.rb")
26+
27+
create_file file_name, code
1228
end
1329
end
14-
end
30+
end

lib/generators/gql/templates/input_type.rb

Lines changed: 0 additions & 9 deletions
This file was deleted.

lib/generators/gql/templates/model_type.rb

Lines changed: 0 additions & 7 deletions
This file was deleted.

0 commit comments

Comments
 (0)