-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathinstall_generator.rb
246 lines (218 loc) · 8.06 KB
/
install_generator.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# frozen_string_literal: true
require 'rails/generators'
require 'rails/generators/base'
require_relative 'core'
require_relative 'relay'
module Graphql
module Generators
# Add GraphQL to a Rails app with `rails g graphql:install`.
#
# Setup a folder structure for GraphQL:
#
# ```
# - app/
# - graphql/
# - resolvers/
# - types/
# - base_argument.rb
# - base_field.rb
# - base_enum.rb
# - base_input_object.rb
# - base_interface.rb
# - base_object.rb
# - base_scalar.rb
# - base_union.rb
# - query_type.rb
# - loaders/
# - mutations/
# - base_mutation.rb
# - {app_name}_schema.rb
# ```
#
# (Add `.gitkeep`s by default, support `--skip-keeps`)
#
# Add a controller for serving GraphQL queries:
#
# ```
# app/controllers/graphql_controller.rb
# ```
#
# Add a route for that controller:
#
# ```ruby
# # config/routes.rb
# post "/graphql", to: "graphql#execute"
# ```
#
# Add ActiveRecord::QueryLogs metadata:
# ```ruby
# current_graphql_operation: -> { GraphQL::Current.operation_name },
# current_graphql_field: -> { GraphQL::Current.field&.path },
# current_dataloader_source: -> { GraphQL::Current.dataloader_source_class },
# ```
#
# Accept a `--batch` option which adds `GraphQL::Batch` setup.
#
# Use `--skip-graphiql` to skip `graphiql-rails` installation.
#
# TODO: also add base classes
class InstallGenerator < Rails::Generators::Base
include Core
include Relay
desc "Install GraphQL folder structure and boilerplate code"
source_root File.expand_path('../templates', __FILE__)
class_option :schema,
type: :string,
default: nil,
desc: "Name for the schema constant (default: {app_name}Schema)"
class_option :skip_keeps,
type: :boolean,
default: false,
desc: "Skip .keep files for source control"
class_option :skip_graphiql,
type: :boolean,
default: false,
desc: "Skip graphiql-rails installation"
class_option :skip_mutation_root_type,
type: :boolean,
default: false,
desc: "Skip creation of the mutation root type"
class_option :relay,
type: :boolean,
default: true,
desc: "Include installation of Relay conventions (nodes, connections, edges)"
class_option :batch,
type: :boolean,
default: false,
desc: "Include GraphQL::Batch installation"
class_option :playground,
type: :boolean,
default: false,
desc: "Use GraphQL Playground over Graphiql as IDE"
class_option :skip_query_logs,
type: :boolean,
default: false,
desc: "Skip ActiveRecord::QueryLogs hooks in config/application.rb"
# These two options are taken from Rails' own generators'
class_option :api,
type: :boolean,
desc: "Preconfigure smaller stack for API only apps"
def create_folder_structure
create_dir("#{options[:directory]}/types")
template("schema.erb", schema_file_path)
["base_object", "base_argument", "base_field", "base_enum", "base_input_object", "base_interface", "base_scalar", "base_union"].each do |base_type|
template("#{base_type}.erb", "#{options[:directory]}/types/#{base_type}.rb")
end
# All resolvers are defined as living in their own module, including this class.
template("base_resolver.erb", "#{options[:directory]}/resolvers/base_resolver.rb")
# Note: You can't have a schema without the query type, otherwise introspection breaks
template("query_type.erb", "#{options[:directory]}/types/query_type.rb")
insert_root_type('query', 'QueryType')
invoke "graphql:install:mutation_root" unless options.skip_mutation_root_type?
template("graphql_controller.erb", "app/controllers/graphql_controller.rb")
route('post "/graphql", to: "graphql#execute"')
if options[:batch]
gem("graphql-batch")
create_dir("#{options[:directory]}/loaders")
end
if options.api?
say("Skipped graphiql, as this rails project is API only")
say(" You may wish to use GraphiQL.app for development: https://github.com/skevy/graphiql-app")
elsif !options[:skip_graphiql]
# `gem(...)` uses `gsub_file(...)` under the hood, which is a no-op for `rails destroy...` (when `behavior == :revoke`).
# So handle that case by calling `gsub_file` with `force: true`.
if behavior == :invoke && !File.read(Rails.root.join("Gemfile")).include?("graphiql-rails")
gem("graphiql-rails", group: :development)
elsif behavior == :revoke
gemfile_pattern = /\n\s*gem ('|")graphiql-rails('|"), :?group(:| =>) :development/
gsub_file Rails.root.join("Gemfile"), gemfile_pattern, "", { force: true }
end
# This is a little cheat just to get cleaner shell output:
log :route, 'graphiql-rails'
shell.mute do
# Rails 5.2 has better support for `route`?
if Rails::VERSION::STRING > "5.2"
route <<-RUBY
if Rails.env.development?
mount GraphiQL::Rails::Engine, at: "/graphiql", graphql_path: "/graphql"
end
RUBY
else
route <<-RUBY
if Rails.env.development?
mount GraphiQL::Rails::Engine, at: "/graphiql", graphql_path: "/graphql"
end
RUBY
end
end
end
if options[:playground]
gem("graphql_playground-rails", group: :development)
log :route, 'graphql_playground-rails'
shell.mute do
if Rails::VERSION::STRING > "5.2"
route <<-RUBY
if Rails.env.development?
mount GraphqlPlayground::Rails::Engine, at: "/playground", graphql_path: "/graphql"
end
RUBY
else
route <<-RUBY
if Rails.env.development?
mount GraphqlPlayground::Rails::Engine, at: "/playground", graphql_path: "/graphql"
end
RUBY
end
end
end
if options[:relay]
install_relay
end
if !options[:skip_query_logs]
config_file = "config/application.rb"
current_app_rb = File.read(Rails.root.join(config_file))
existing_log_tags_pattern = /config.active_record.query_log_tags = \[\n?(\s*:[a-z_]+,?\s*\n?|\s*#[^\]]*\n)*/m
existing_log_tags = existing_log_tags_pattern.match(current_app_rb)
if existing_log_tags && behavior == :invoke
code = <<-RUBY
# GraphQL-Ruby query log tags:
current_graphql_operation: -> { GraphQL::Current.operation_name },
current_graphql_field: -> { GraphQL::Current.field&.path },
current_dataloader_source: -> { GraphQL::Current.dataloader_source_class },
RUBY
if !existing_log_tags.to_s.end_with?(",")
code = ",\n#{code} "
end
# Try to insert this code _after_ any plain symbol entries in the array of query log tags:
after_code = existing_log_tags_pattern
else
code = <<-RUBY
config.active_record.query_log_tags_enabled = true
config.active_record.query_log_tags = [
# Rails query log tags:
:application, :controller, :action, :job,
# GraphQL-Ruby query log tags:
current_graphql_operation: -> { GraphQL::Current.operation_name },
current_graphql_field: -> { GraphQL::Current.field&.path },
current_dataloader_source: -> { GraphQL::Current.dataloader_source_class },
]
RUBY
after_code = "class Application < Rails::Application\n"
end
insert_into_file(config_file, code, after: after_code)
end
if gemfile_modified?
say "Gemfile has been modified, make sure you `bundle install`"
end
end
private
def gemfile_modified?
@gemfile_modified
end
def gem(*args)
@gemfile_modified = true
super(*args)
end
end
end
end