graphql_rails 0.7.0 → 1.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (82) hide show
  1. checksums.yaml +4 -4
  2. data/.hound.yml +1 -0
  3. data/.rubocop.yml +3 -3
  4. data/.ruby-version +1 -1
  5. data/.travis.yml +2 -2
  6. data/CHANGELOG.md +35 -0
  7. data/Gemfile +3 -2
  8. data/Gemfile.lock +181 -71
  9. data/docs/README.md +40 -8
  10. data/docs/_sidebar.md +5 -0
  11. data/docs/components/controller.md +295 -9
  12. data/docs/components/decorator.md +69 -0
  13. data/docs/components/model.md +267 -6
  14. data/docs/components/routes.md +28 -0
  15. data/docs/getting_started/quick_start.md +10 -3
  16. data/docs/index.html +1 -1
  17. data/docs/logging_and_monitoring/logging_and_monitoring.md +35 -0
  18. data/docs/other_tools/query_runner.md +49 -0
  19. data/docs/other_tools/schema_dump.md +29 -0
  20. data/docs/testing/testing.md +3 -1
  21. data/graphql_rails.gemspec +5 -4
  22. data/lib/generators/graphql_rails/install_generator.rb +50 -0
  23. data/lib/generators/graphql_rails/templates/example_users_controller.erb +19 -0
  24. data/lib/generators/graphql_rails/templates/graphql_application_controller.erb +8 -0
  25. data/lib/generators/graphql_rails/templates/graphql_controller.erb +20 -0
  26. data/lib/generators/graphql_rails/templates/graphql_router.erb +19 -0
  27. data/lib/generators/graphql_rails/templates/graphql_router_spec.erb +21 -0
  28. data/lib/graphql_rails.rb +6 -0
  29. data/lib/graphql_rails/attributes/attributable.rb +22 -17
  30. data/lib/graphql_rails/attributes/attribute.rb +67 -3
  31. data/lib/graphql_rails/attributes/attribute_name_parser.rb +4 -4
  32. data/lib/graphql_rails/attributes/input_attribute.rb +33 -15
  33. data/lib/graphql_rails/attributes/input_type_parser.rb +62 -0
  34. data/lib/graphql_rails/attributes/type_name_info.rb +38 -0
  35. data/lib/graphql_rails/attributes/type_parseable.rb +132 -0
  36. data/lib/graphql_rails/attributes/type_parser.rb +59 -53
  37. data/lib/graphql_rails/concerns/service.rb +19 -0
  38. data/lib/graphql_rails/controller.rb +42 -21
  39. data/lib/graphql_rails/controller/action.rb +12 -67
  40. data/lib/graphql_rails/controller/action_configuration.rb +70 -28
  41. data/lib/graphql_rails/controller/build_controller_action_resolver.rb +52 -0
  42. data/lib/graphql_rails/controller/build_controller_action_resolver/controller_action_resolver.rb +28 -0
  43. data/lib/graphql_rails/controller/configuration.rb +56 -3
  44. data/lib/graphql_rails/controller/log_controller_action.rb +71 -0
  45. data/lib/graphql_rails/controller/request.rb +29 -8
  46. data/lib/graphql_rails/controller/request/format_errors.rb +58 -0
  47. data/lib/graphql_rails/decorator.rb +41 -0
  48. data/lib/graphql_rails/decorator/relation_decorator.rb +75 -0
  49. data/lib/graphql_rails/errors/custom_execution_error.rb +22 -0
  50. data/lib/graphql_rails/errors/execution_error.rb +6 -7
  51. data/lib/graphql_rails/errors/system_error.rb +14 -0
  52. data/lib/graphql_rails/errors/validation_error.rb +1 -5
  53. data/lib/graphql_rails/input_configurable.rb +47 -0
  54. data/lib/graphql_rails/integrations.rb +19 -0
  55. data/lib/graphql_rails/integrations/lograge.rb +39 -0
  56. data/lib/graphql_rails/integrations/sentry.rb +34 -0
  57. data/lib/graphql_rails/model.rb +26 -4
  58. data/lib/graphql_rails/model/add_fields_to_graphql_type.rb +45 -0
  59. data/lib/graphql_rails/model/build_connection_type.rb +52 -0
  60. data/lib/graphql_rails/model/{configuration → build_connection_type}/count_items.rb +5 -5
  61. data/lib/graphql_rails/model/build_enum_type.rb +39 -10
  62. data/lib/graphql_rails/model/build_graphql_input_type.rb +8 -4
  63. data/lib/graphql_rails/model/call_graphql_model_method.rb +72 -0
  64. data/lib/graphql_rails/model/configurable.rb +6 -2
  65. data/lib/graphql_rails/model/configuration.rb +30 -16
  66. data/lib/graphql_rails/model/find_or_build_graphql_type.rb +64 -0
  67. data/lib/graphql_rails/model/find_or_build_graphql_type_class.rb +46 -0
  68. data/lib/graphql_rails/model/input.rb +11 -7
  69. data/lib/graphql_rails/query_runner.rb +68 -0
  70. data/lib/graphql_rails/railtie.rb +10 -0
  71. data/lib/graphql_rails/router.rb +40 -13
  72. data/lib/graphql_rails/router/resource_routes_builder.rb +10 -9
  73. data/lib/graphql_rails/router/route.rb +21 -6
  74. data/lib/graphql_rails/router/schema_builder.rb +30 -11
  75. data/lib/graphql_rails/rspec_controller_helpers.rb +6 -4
  76. data/lib/graphql_rails/tasks/dump_graphql_schema.rb +57 -0
  77. data/lib/graphql_rails/tasks/schema.rake +14 -0
  78. data/lib/graphql_rails/version.rb +1 -1
  79. metadata +70 -19
  80. data/lib/graphql_rails/controller/controller_function.rb +0 -50
  81. data/lib/graphql_rails/controller/format_results.rb +0 -36
  82. data/lib/graphql_rails/model/build_graphql_type.rb +0 -37
@@ -20,12 +20,12 @@ module GraphqlRails
20
20
  @routes ||= initial_routes
21
21
  end
22
22
 
23
- def query(*args)
24
- routes << build_query(*args)
23
+ def query(*args, **kwargs)
24
+ routes << build_query(*args, **kwargs)
25
25
  end
26
26
 
27
- def mutation(*args)
28
- routes << build_mutation(*args)
27
+ def mutation(*args, **kwargs)
28
+ routes << build_mutation(*args, **kwargs)
29
29
  end
30
30
 
31
31
  private
@@ -54,12 +54,12 @@ module GraphqlRails
54
54
  routes
55
55
  end
56
56
 
57
- def build_mutation(*args)
58
- build_route(MutationRoute, *args)
57
+ def build_mutation(*args, **kwargs)
58
+ build_route(MutationRoute, *args, **kwargs)
59
59
  end
60
60
 
61
- def build_query(*args)
62
- build_route(QueryRoute, *args)
61
+ def build_query(*args, **kwargs)
62
+ build_route(QueryRoute, *args, **kwargs)
63
63
  end
64
64
 
65
65
  # rubocop:disable Metrics/ParameterLists
@@ -70,8 +70,9 @@ module GraphqlRails
70
70
  end
71
71
 
72
72
  action_options = options.merge(custom_options).merge(on: on)
73
+ controller_method_name = action.to_s.underscore
73
74
  action_name = [prefix, resource_name(on), suffix_name].map(&:to_s).reject(&:empty?).join('_')
74
- builder.new(action_name, to: "#{name}##{action}", **action_options)
75
+ builder.new(action_name, to: "#{name}##{controller_method_name}", **action_options)
75
76
  end
76
77
  # rubocop:enable Metrics/ParameterLists
77
78
 
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative '../controller/controller_function'
3
+ require_relative '../controller/build_controller_action_resolver'
4
4
 
5
5
  module GraphqlRails
6
6
  class Router
@@ -8,10 +8,11 @@ module GraphqlRails
8
8
  class Route
9
9
  attr_reader :name, :module_name, :on, :relative_path
10
10
 
11
- def initialize(name, to: '', on:, **options)
11
+ def initialize(name, to: '', on:, groups: nil, **options)
12
12
  @name = name.to_s.camelize(:lower)
13
13
  @module_name = options[:module].to_s
14
14
  @function = options[:function]
15
+ @groups = groups
15
16
  @relative_path = to
16
17
  @on = on.to_sym
17
18
  end
@@ -26,12 +27,26 @@ module GraphqlRails
26
27
  on == :collection
27
28
  end
28
29
 
29
- def member?
30
- on == :member
30
+ def show_in_group?(group_name)
31
+ return true if groups.nil? || groups.empty?
32
+
33
+ groups.include?(group_name&.to_sym)
34
+ end
35
+
36
+ def field_options
37
+ if function
38
+ { function: function }
39
+ else
40
+ { resolver: resolver }
41
+ end
31
42
  end
32
43
 
33
- def function
34
- @function ||= Controller::ControllerFunction.from_route(self)
44
+ private
45
+
46
+ attr_reader :function, :groups
47
+
48
+ def resolver
49
+ @resolver ||= Controller::BuildControllerActionResolver.call(route: self)
35
50
  end
36
51
  end
37
52
  end
@@ -8,34 +8,53 @@ module GraphqlRails
8
8
 
9
9
  attr_reader :queries, :mutations, :raw_actions
10
10
 
11
- def initialize(queries:, mutations:, raw_actions:)
11
+ def initialize(queries:, mutations:, raw_actions:, group: nil)
12
12
  @queries = queries
13
13
  @mutations = mutations
14
14
  @raw_actions = raw_actions
15
+ @group = group
15
16
  end
16
17
 
17
18
  def call
18
- query_type = build_type('Query', queries)
19
- mutation_type = build_type('Mutation', mutations)
19
+ query_type = build_group_type('Query', queries)
20
+ mutation_type = build_group_type('Mutation', mutations)
20
21
  raw = raw_actions
21
22
 
22
- GraphQL::Schema.define do
23
+ Class.new(GraphQL::Schema) do
24
+ connections.add(
25
+ GraphqlRails::Decorator::RelationDecorator,
26
+ GraphQL::Pagination::ActiveRecordRelationConnection
27
+ )
23
28
  cursor_encoder(Router::PlainCursorEncoder)
24
29
  raw.each { |action| send(action[:name], *action[:args], &action[:block]) }
25
30
 
26
- query(query_type)
27
- mutation(mutation_type)
31
+ query(query_type) if query_type
32
+ mutation(mutation_type) if mutation_type
28
33
  end
29
34
  end
30
35
 
31
36
  private
32
37
 
33
- def build_type(type_name, routes)
34
- GraphQL::ObjectType.define do
35
- name type_name
38
+ attr_reader :group
36
39
 
37
- routes.each do |route|
38
- field route.name, function: route.function
40
+ def build_group_type(type_name, routes)
41
+ group_name = group
42
+ group_routes = routes.select { |route| route.show_in_group?(group_name) }
43
+ return if group_routes.empty?
44
+
45
+ build_type(type_name, group_routes)
46
+ end
47
+
48
+ def build_type(type_name, group_routes)
49
+ Class.new(GraphQL::Schema::Object) do
50
+ graphql_name(type_name)
51
+
52
+ group_routes.each do |route|
53
+ field(*route.name, **route.field_options)
54
+ end
55
+
56
+ def self.inspect
57
+ "#{GraphQL::Schema::Object}(#{graphql_name})"
39
58
  end
40
59
  end
41
60
  end
@@ -82,11 +82,13 @@ module GraphqlRails
82
82
  action_by_name = config.action_by_name
83
83
  controller_path = controller.name.underscore.sub(/_controller\Z/, '')
84
84
 
85
- Router.draw do
85
+ router = Router.draw do
86
86
  action_by_name.keys.each do |action_name|
87
- query("#{action_name}_test", to: "#{controller_path}##{action_name}")
87
+ query("#{action_name}_test", to: "#{controller_path}##{action_name}", group: :graphql_rspec_helpers)
88
88
  end
89
89
  end
90
+
91
+ router.graphql_schema(:graphql_rspec_helpers)
90
92
  end
91
93
  end
92
94
 
@@ -107,8 +109,8 @@ module GraphqlRails
107
109
  @response
108
110
  end
109
111
 
110
- def mutation(*args)
111
- query(*args)
112
+ def mutation(*args, **kwargs)
113
+ query(*args, **kwargs)
112
114
  end
113
115
 
114
116
  def response
@@ -0,0 +1,57 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GraphqlRails
4
+ # Generates graphql schema dump files
5
+ class DumpGraphqlSchema
6
+ require 'graphql_rails/errors/error'
7
+
8
+ class MissingGraphqlRouterError < GraphqlRails::Error; end
9
+
10
+ attr_reader :name
11
+
12
+ def self.call(*args)
13
+ new(*args).call
14
+ end
15
+
16
+ def initialize(name:)
17
+ @name = name
18
+ end
19
+
20
+ def call
21
+ validate
22
+ File.write(schema_path, schema.to_definition)
23
+ end
24
+
25
+ private
26
+
27
+ def validate
28
+ return if router
29
+
30
+ error_message = \
31
+ 'GraphqlRouter is missing. ' \
32
+ 'Run `rails g graphql_rails:install` to build it'
33
+ raise MissingGraphqlRouterError, error_message
34
+ end
35
+
36
+ def router
37
+ @router ||= '::GraphqlRouter'.safe_constantize
38
+ end
39
+
40
+ def schema
41
+ @schema ||= ::GraphqlRouter.graphql_schema(name.presence)
42
+ end
43
+
44
+ def schema_path
45
+ ENV['GRAPHQL_SCHEMA_DUMP_PATH'] || default_schema_path
46
+ end
47
+
48
+ def default_schema_path
49
+ schema_folder_path = Rails.root.join('spec', 'fixtures')
50
+
51
+ FileUtils.mkdir_p(schema_folder_path)
52
+ file_name = name.present? ? "graphql_#{name}_schema.graphql" : 'graphql_schema.graphql'
53
+
54
+ schema_folder_path.join(file_name)
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'graphql_rails/tasks/dump_graphql_schema'
4
+
5
+ namespace :graphql_rails do
6
+ namespace :schema do
7
+ desc 'Dump GraphQL schema'
8
+ task(:dump, %i[name] => :environment) do |_, args|
9
+ default_name = ENV.fetch('SCHEMA_GROUP_NAME', '')
10
+ args.with_defaults(name: default_name)
11
+ GraphqlRails::DumpGraphqlSchema.call(name: args.name)
12
+ end
13
+ end
14
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module GraphqlRails
4
- VERSION = '0.7.0'
4
+ VERSION = '1.2.1'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: graphql_rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.0
4
+ version: 1.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Povilas Jurčys
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-05-15 00:00:00.000000000 Z
11
+ date: 2021-02-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: graphql
@@ -16,14 +16,20 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '1'
19
+ version: '1.12'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 1.12.4
20
23
  type: :runtime
21
24
  prerelease: false
22
25
  version_requirements: !ruby/object:Gem::Requirement
23
26
  requirements:
24
27
  - - "~>"
25
28
  - !ruby/object:Gem::Version
26
- version: '1'
29
+ version: '1.12'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 1.12.4
27
33
  - !ruby/object:Gem::Dependency
28
34
  name: activesupport
29
35
  requirement: !ruby/object:Gem::Requirement
@@ -44,28 +50,28 @@ dependencies:
44
50
  requirements:
45
51
  - - "~>"
46
52
  - !ruby/object:Gem::Version
47
- version: '1.16'
53
+ version: '2'
48
54
  type: :development
49
55
  prerelease: false
50
56
  version_requirements: !ruby/object:Gem::Requirement
51
57
  requirements:
52
58
  - - "~>"
53
59
  - !ruby/object:Gem::Version
54
- version: '1.16'
60
+ version: '2'
55
61
  - !ruby/object:Gem::Dependency
56
62
  name: rake
57
63
  requirement: !ruby/object:Gem::Requirement
58
64
  requirements:
59
65
  - - "~>"
60
66
  - !ruby/object:Gem::Version
61
- version: '10.0'
67
+ version: '13.0'
62
68
  type: :development
63
69
  prerelease: false
64
70
  version_requirements: !ruby/object:Gem::Requirement
65
71
  requirements:
66
72
  - - "~>"
67
73
  - !ruby/object:Gem::Version
68
- version: '10.0'
74
+ version: '13.0'
69
75
  - !ruby/object:Gem::Dependency
70
76
  name: rspec
71
77
  requirement: !ruby/object:Gem::Requirement
@@ -108,7 +114,21 @@ dependencies:
108
114
  - - ">="
109
115
  - !ruby/object:Gem::Version
110
116
  version: '0'
111
- description:
117
+ - !ruby/object:Gem::Dependency
118
+ name: rails
119
+ requirement: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - "~>"
122
+ - !ruby/object:Gem::Version
123
+ version: '6'
124
+ type: :development
125
+ prerelease: false
126
+ version_requirements: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - "~>"
129
+ - !ruby/object:Gem::Version
130
+ version: '6'
131
+ description:
112
132
  email:
113
133
  - po.jurcys@gmail.com
114
134
  executables: []
@@ -133,40 +153,70 @@ files:
133
153
  - docs/README.md
134
154
  - docs/_sidebar.md
135
155
  - docs/components/controller.md
156
+ - docs/components/decorator.md
136
157
  - docs/components/model.md
137
158
  - docs/components/routes.md
138
159
  - docs/getting_started/quick_start.md
139
160
  - docs/getting_started/setup.md
140
161
  - docs/index.html
162
+ - docs/logging_and_monitoring/logging_and_monitoring.md
163
+ - docs/other_tools/query_runner.md
164
+ - docs/other_tools/schema_dump.md
141
165
  - docs/testing/testing.md
142
166
  - graphql_rails.gemspec
167
+ - lib/generators/graphql_rails/install_generator.rb
168
+ - lib/generators/graphql_rails/templates/example_users_controller.erb
169
+ - lib/generators/graphql_rails/templates/graphql_application_controller.erb
170
+ - lib/generators/graphql_rails/templates/graphql_controller.erb
171
+ - lib/generators/graphql_rails/templates/graphql_router.erb
172
+ - lib/generators/graphql_rails/templates/graphql_router_spec.erb
143
173
  - lib/graphql_rails.rb
144
174
  - lib/graphql_rails/attributes.rb
145
175
  - lib/graphql_rails/attributes/attributable.rb
146
176
  - lib/graphql_rails/attributes/attribute.rb
147
177
  - lib/graphql_rails/attributes/attribute_name_parser.rb
148
178
  - lib/graphql_rails/attributes/input_attribute.rb
179
+ - lib/graphql_rails/attributes/input_type_parser.rb
180
+ - lib/graphql_rails/attributes/type_name_info.rb
181
+ - lib/graphql_rails/attributes/type_parseable.rb
149
182
  - lib/graphql_rails/attributes/type_parser.rb
183
+ - lib/graphql_rails/concerns/service.rb
150
184
  - lib/graphql_rails/controller.rb
151
185
  - lib/graphql_rails/controller/action.rb
152
186
  - lib/graphql_rails/controller/action_configuration.rb
153
187
  - lib/graphql_rails/controller/action_hook.rb
154
188
  - lib/graphql_rails/controller/action_hooks_runner.rb
189
+ - lib/graphql_rails/controller/build_controller_action_resolver.rb
190
+ - lib/graphql_rails/controller/build_controller_action_resolver/controller_action_resolver.rb
155
191
  - lib/graphql_rails/controller/configuration.rb
156
- - lib/graphql_rails/controller/controller_function.rb
157
- - lib/graphql_rails/controller/format_results.rb
192
+ - lib/graphql_rails/controller/log_controller_action.rb
158
193
  - lib/graphql_rails/controller/request.rb
194
+ - lib/graphql_rails/controller/request/format_errors.rb
195
+ - lib/graphql_rails/decorator.rb
196
+ - lib/graphql_rails/decorator/relation_decorator.rb
197
+ - lib/graphql_rails/errors/custom_execution_error.rb
159
198
  - lib/graphql_rails/errors/error.rb
160
199
  - lib/graphql_rails/errors/execution_error.rb
200
+ - lib/graphql_rails/errors/system_error.rb
161
201
  - lib/graphql_rails/errors/validation_error.rb
202
+ - lib/graphql_rails/input_configurable.rb
203
+ - lib/graphql_rails/integrations.rb
204
+ - lib/graphql_rails/integrations/lograge.rb
205
+ - lib/graphql_rails/integrations/sentry.rb
162
206
  - lib/graphql_rails/model.rb
207
+ - lib/graphql_rails/model/add_fields_to_graphql_type.rb
208
+ - lib/graphql_rails/model/build_connection_type.rb
209
+ - lib/graphql_rails/model/build_connection_type/count_items.rb
163
210
  - lib/graphql_rails/model/build_enum_type.rb
164
211
  - lib/graphql_rails/model/build_graphql_input_type.rb
165
- - lib/graphql_rails/model/build_graphql_type.rb
212
+ - lib/graphql_rails/model/call_graphql_model_method.rb
166
213
  - lib/graphql_rails/model/configurable.rb
167
214
  - lib/graphql_rails/model/configuration.rb
168
- - lib/graphql_rails/model/configuration/count_items.rb
215
+ - lib/graphql_rails/model/find_or_build_graphql_type.rb
216
+ - lib/graphql_rails/model/find_or_build_graphql_type_class.rb
169
217
  - lib/graphql_rails/model/input.rb
218
+ - lib/graphql_rails/query_runner.rb
219
+ - lib/graphql_rails/railtie.rb
170
220
  - lib/graphql_rails/router.rb
171
221
  - lib/graphql_rails/router/mutation_route.rb
172
222
  - lib/graphql_rails/router/plain_cursor_encoder.rb
@@ -175,12 +225,14 @@ files:
175
225
  - lib/graphql_rails/router/route.rb
176
226
  - lib/graphql_rails/router/schema_builder.rb
177
227
  - lib/graphql_rails/rspec_controller_helpers.rb
228
+ - lib/graphql_rails/tasks/dump_graphql_schema.rb
229
+ - lib/graphql_rails/tasks/schema.rake
178
230
  - lib/graphql_rails/version.rb
179
231
  homepage: https://github.com/samesystem/graphql_rails
180
232
  licenses:
181
233
  - MIT
182
234
  metadata: {}
183
- post_install_message:
235
+ post_install_message:
184
236
  rdoc_options: []
185
237
  require_paths:
186
238
  - lib
@@ -195,9 +247,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
195
247
  - !ruby/object:Gem::Version
196
248
  version: '0'
197
249
  requirements: []
198
- rubyforge_project:
199
- rubygems_version: 2.7.7
200
- signing_key:
250
+ rubygems_version: 3.1.2
251
+ signing_key:
201
252
  specification_version: 4
202
- summary: GraphQL server and client for rails
253
+ summary: Rails style structure for GraphQL API.
203
254
  test_files: []