groovestack-base 0.1.7 → 0.1.9

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 (38) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +1 -1
  3. data/CHANGELOG.md +5 -1
  4. data/Gemfile +5 -1
  5. data/config/initializers/graphql.rb +2 -0
  6. data/groovestack-base.gemspec +1 -1
  7. data/lib/groovestack/base/graphql/authorization/authorized_field.rb +21 -0
  8. data/lib/groovestack/base/graphql/authorization/authorized_object.rb +13 -0
  9. data/lib/groovestack/base/graphql/authorization/visible_field.rb +23 -0
  10. data/lib/groovestack/base/graphql/base/argument.rb +17 -0
  11. data/lib/groovestack/base/graphql/base/field.rb +32 -0
  12. data/lib/groovestack/base/graphql/base/input_object.rb +13 -0
  13. data/lib/groovestack/base/graphql/base/mutation.rb +25 -0
  14. data/lib/groovestack/base/graphql/base/object.rb +13 -0
  15. data/lib/groovestack/base/graphql/base/subscription.rb +15 -0
  16. data/lib/groovestack/base/graphql/controllers/graphql_controller.rb +57 -0
  17. data/lib/groovestack/base/graphql/{documentation.rb → documentation/arguments.rb} +0 -7
  18. data/lib/groovestack/base/graphql/documentation/fields.rb +20 -0
  19. data/lib/groovestack/base/graphql/mutations/aasm_event_trigger.rb +42 -0
  20. data/lib/groovestack/base/graphql/mutations/method_trigger.rb +36 -0
  21. data/lib/groovestack/base/graphql/providers/react_admin.rb +180 -0
  22. data/lib/groovestack/base/graphql/subscriptions/{event_handler.rb → trigger.rb} +1 -1
  23. data/lib/groovestack/base/graphql/{tracers/atomic_multiplex_transaction.rb → tracers.rb} +22 -12
  24. data/lib/groovestack/base/graphql/types/aasm_event_attributes.rb +28 -0
  25. data/lib/groovestack/base/graphql/types/currency.rb +16 -0
  26. data/lib/groovestack/base/graphql/types/money.rb +23 -0
  27. data/lib/groovestack/base/graphql/types/subscription_payload.rb +15 -0
  28. data/lib/groovestack/base/graphql/types/type_resolver.rb +21 -0
  29. data/lib/groovestack/base/version.rb +1 -1
  30. data/lib/groovestack/base.rb +77 -22
  31. metadata +26 -14
  32. data/lib/groovestack/base/graphql/base_input_object.rb +0 -11
  33. data/lib/groovestack/base/graphql/base_mutation.rb +0 -23
  34. data/lib/groovestack/base/graphql/base_subscription.rb +0 -13
  35. data/lib/groovestack/base/graphql/helpers.rb +0 -152
  36. data/lib/groovestack/base/graphql/providers/react_admin/resource.rb +0 -146
  37. data/lib/groovestack/base/graphql/providers/react_admin/types.rb +0 -17
  38. data/lib/groovestack/base/graphql/types.rb +0 -103
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ # TODO: add specs
4
+
3
5
  module Groovestack
4
6
  module Base
5
7
  module GraphQL
@@ -19,23 +21,26 @@ module Groovestack
19
21
  # responds to `mutation?`.
20
22
 
21
23
  module AtomicMultiplexTransaction
22
- # rubocop:disable Metrics/AbcSize, Metrics/MethodLength, Metrics/PerceivedComplexity, Metrics/CyclomaticComplexity
23
- def execute_multiplex(multiplex:)
24
- is_mutation = multiplex.queries.any?(&:mutation?)
24
+ def execute_multiplex(multiplex:, &block)
25
+ return yield unless contains_mutation?(multiplex)
26
+
27
+ execute_with_transaction(&block)
28
+ end
25
29
 
26
- return yield unless is_mutation
30
+ private
31
+
32
+ def contains_mutation?(multiplex)
33
+ multiplex.queries.any?(&:mutation?)
34
+ end
27
35
 
36
+ def execute_with_transaction
28
37
  results = nil
29
38
  rollback = false
30
39
 
31
40
  begin
32
41
  ::ActiveRecord::Base.transaction do
33
42
  results = yield
34
-
35
- rollback = results.any? do |result|
36
- result.is_a?(::GraphQL::Query::Result) && result.to_h['errors'].present?
37
- end
38
-
43
+ rollback = results_errors?(results)
39
44
  raise ::ActiveRecord::Rollback if rollback
40
45
 
41
46
  results
@@ -44,10 +49,16 @@ module Groovestack
44
49
  rollback = true
45
50
  end
46
51
 
47
- return results unless rollback
52
+ rollback ? handle_rollback(results) : results
53
+ end
48
54
 
49
- # IF rollback, extract errors and return null for data
55
+ def results_errors?(results)
56
+ results.any? do |result|
57
+ result.is_a?(::GraphQL::Query::Result) && result.to_h['errors'].present?
58
+ end
59
+ end
50
60
 
61
+ def handle_rollback(results)
51
62
  results.map do |result|
52
63
  ::GraphQL::Query::Result.new(
53
64
  query: result.query,
@@ -55,7 +66,6 @@ module Groovestack
55
66
  )
56
67
  end
57
68
  end
58
- # rubocop:enable Metrics/AbcSize, Metrics/MethodLength, Metrics/PerceivedComplexity, Metrics/CyclomaticComplexity
59
69
  end
60
70
  end
61
71
  end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Groovestack
4
+ module Base
5
+ module GraphQL
6
+ module Types
7
+ module AASMEventAttributes
8
+ def all_events
9
+ object.class.aasm.events.map { |event| event.name.to_s }
10
+ end
11
+
12
+ def permitted_events
13
+ object.aasm.events(permitted: true).map { |event| event.name.to_s }
14
+ end
15
+
16
+ def status_events
17
+ all = all_events
18
+ permitted = permitted_events
19
+
20
+ all.map do |event|
21
+ { name: event.titleize, key: event, enabled: permitted.include?(event) }
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Groovestack
4
+ module Base
5
+ module GraphQL
6
+ module Types
7
+ class Currency < ::Groovestack::Base::GraphQL::Base::Object
8
+ description 'A currency object'
9
+
10
+ field :code, String, null: false, description: 'currency code', method: :iso_code
11
+ field :symbol, String, null: false, description: 'currency symbol'
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Groovestack
4
+ module Base
5
+ module GraphQL
6
+ module Types
7
+ class Money < ::Groovestack::Base::GraphQL::Base::Object
8
+ description 'A money object'
9
+
10
+ field :amount, String, null: false, description: 'amount in decimal'
11
+ field :currency, ::Groovestack::Base::GraphQL::Types::Currency, null: false, description: 'currency metadata'
12
+ field :formatted_amount, String, null: false,
13
+ description: 'amount in decimal formatted with currency symbol',
14
+ method: :format
15
+
16
+ def amount
17
+ object.amount.to_s
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Groovestack
4
+ module Base
5
+ module GraphQL
6
+ module Types
7
+ class SubscriptionPayload < ::Groovestack::Base::GraphQL::Base::Object
8
+ field :event, ::GraphQL::Types::JSON, null: false
9
+ field :subscription, String, null: false
10
+ field :subscription_args, ::GraphQL::Types::JSON, null: false
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Groovestack
4
+ module Base
5
+ module GraphQL
6
+ module Types
7
+ module TypeResolver
8
+ extend ActiveSupport::Concern
9
+
10
+ included do
11
+ field :type, String, null: true, resolver_method: :object_type
12
+
13
+ def object_type
14
+ object.class.to_s if object.respond_to?(:class)
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Groovestack
4
4
  module Base
5
- VERSION = '0.1.7'
5
+ VERSION = '0.1.9'
6
6
  end
7
7
  end
@@ -12,27 +12,59 @@ require 'groovestack/base/utilities/string'
12
12
  module Groovestack
13
13
  module Base
14
14
  module GraphQL
15
- module Subscriptions
16
- autoload :EventHandler, 'groovestack/base/graphql/subscriptions/event_handler'
15
+ module Authorization
16
+ autoload :AuthorizedField, 'groovestack/base/graphql/authorization/authorized_field'
17
+ autoload :AuthorizedObject, 'groovestack/base/graphql/authorization/authorized_object'
18
+ autoload :VisibleField, 'groovestack/base/graphql/authorization/visible_field'
17
19
  end
18
20
 
19
- autoload :Types, 'groovestack/base/graphql/types'
20
- autoload :BaseInputObject, 'groovestack/base/graphql/base_input_object'
21
- autoload :BaseMutation, 'groovestack/base/graphql/base_mutation'
22
- autoload :Documentation, 'groovestack/base/graphql/documentation'
23
- autoload :BaseSubscription, 'groovestack/base/graphql/base_subscription'
24
- autoload :Helpers, 'groovestack/base/graphql/helpers'
21
+ module Base
22
+ autoload :Argument, 'groovestack/base/graphql/base/argument'
23
+ autoload :Field, 'groovestack/base/graphql/base/field'
24
+ autoload :Object, 'groovestack/base/graphql/base/object'
25
+ autoload :InputObject, 'groovestack/base/graphql/base/input_object'
26
+ autoload :Mutation, 'groovestack/base/graphql/base/mutation'
27
+ autoload :Subscription, 'groovestack/base/graphql/base/subscription'
28
+ end
25
29
 
26
- module Tracers
27
- autoload :AtomicMultiplexTransaction, 'groovestack/base/graphql/tracers/atomic_multiplex_transaction'
30
+ module Controllers
31
+ autoload :GraphQLController, 'groovestack/base/graphql/controllers/graphql_controller'
32
+ end
33
+
34
+ module Documentation
35
+ autoload :Arguments, 'groovestack/base/graphql/documentation/arguments'
36
+ autoload :Fields, 'groovestack/base/graphql/documentation/fields'
37
+ end
38
+
39
+ module Mutations
40
+ autoload :AASMEventTrigger, 'groovestack/base/graphql/mutations/aasm_event_trigger'
41
+ autoload :MethodTrigger, 'groovestack/base/graphql/mutations/method_trigger'
28
42
  end
29
43
 
30
44
  module Providers
31
45
  module ReactAdmin
32
- autoload :Resource, 'groovestack/base/graphql/providers/react_admin/resource'
33
- autoload :Types, 'groovestack/base/graphql/providers/react_admin/types'
46
+ autoload :Resource, 'groovestack/base/graphql/providers/react_admin'
47
+ module Types
48
+ autoload :RAListMetadata, 'groovestack/base/graphql/providers/react_admin'
49
+ end
34
50
  end
35
51
  end
52
+
53
+ module Subscriptions
54
+ autoload :Trigger, 'groovestack/base/graphql/subscriptions/trigger'
55
+ end
56
+
57
+ module Types
58
+ autoload :AASMEventAttributes, 'groovestack/base/graphql/types/aasm_event_attributes'
59
+ autoload :Currency, 'groovestack/base/graphql/types/currency'
60
+ autoload :Money, 'groovestack/base/graphql/types/money'
61
+ autoload :SubscriptionPayload, 'groovestack/base/graphql/types/subscription_payload'
62
+ autoload :TypeResolver, 'groovestack/base/graphql/types/type_resolver'
63
+ end
64
+
65
+ module Tracers
66
+ autoload :AtomicMultiplexTransaction, 'groovestack/base/graphql/tracers'
67
+ end
36
68
  end
37
69
 
38
70
  autoload :ActiveRecord, 'groovestack/base/active_record'
@@ -84,26 +116,49 @@ module Core
84
116
  module Base
85
117
  module GraphQL
86
118
  module Subscriptions
87
- EventHandler = ::Groovestack::Base::GraphQL::Subscriptions::EventHandler
119
+ EventHandler = ::Groovestack::Base::GraphQL::Subscriptions::Trigger
88
120
  end
89
121
 
90
- Types = ::Groovestack::Base::GraphQL::Types
91
- BaseInputObject = ::Groovestack::Base::GraphQL::BaseInputObject
92
- BaseMutation = ::Groovestack::Base::GraphQL::BaseMutation
93
- Documentation = ::Groovestack::Base::GraphQL::Documentation
94
- BaseSubscription = ::Groovestack::Base::GraphQL::BaseSubscription
95
- Helpers = ::Groovestack::Base::GraphQL::Helpers
122
+ module Types
123
+ BaseArgument = ::Groovestack::Base::GraphQL::Base::Argument
124
+ BaseField = ::Groovestack::Base::GraphQL::Base::Field
125
+ BaseObject = ::Groovestack::Base::GraphQL::Base::Object
126
+ AuthorizedBaseField = ::Groovestack::Base::GraphQL::Authorization::AuthorizedField
127
+ AuthorizedBaseObject = ::Groovestack::Base::GraphQL::Authorization::AuthorizedObject
128
+ VisibleBaseField = ::Groovestack::Base::GraphQL::Authorization::VisibleField
129
+ end
130
+ BaseInputObject = ::Groovestack::Base::GraphQL::Base::InputObject
131
+ BaseMutation = ::Groovestack::Base::GraphQL::Base::Mutation
132
+ module Documentation
133
+ Arguments = ::Groovestack::Base::GraphQL::Documentation::Arguments
134
+ Fields = ::Groovestack::Base::GraphQL::Documentation::Fields
135
+ end
136
+ BaseSubscription = ::Groovestack::Base::GraphQL::Base::Subscription
137
+ module Helpers
138
+ module Types
139
+ StatusEventVirtualAttributes = ::Groovestack::Base::GraphQL::Types::AASMEventAttributes
140
+ Typified = ::Groovestack::Base::GraphQL::Types::TypeResolver
141
+ end
96
142
 
97
- module Tracers
98
- AtomicMultiplexTransaction = ::Groovestack::Base::GraphQL::Tracers::AtomicMultiplexTransaction
143
+ module Mutations
144
+ StatusEvents = ::Groovestack::Base::GraphQL::Mutations::AASMEventTrigger
145
+ InstanceMethods = ::Groovestack::Base::GraphQL::Mutations::MethodTrigger
146
+ end
147
+ Controller = ::Groovestack::Base::GraphQL::Controllers::GraphQLController
99
148
  end
100
149
 
101
150
  module Providers
102
151
  module ReactAdmin
103
152
  Resource = ::Groovestack::Base::GraphQL::Providers::ReactAdmin::Resource
104
- Types = ::Groovestack::Base::GraphQL::Providers::ReactAdmin::Types
153
+ module Types
154
+ RAListMetadata = ::Groovestack::Base::GraphQL::Providers::ReactAdmin::Types::RAListMetadata
155
+ end
105
156
  end
106
157
  end
158
+
159
+ module Tracers
160
+ AtomicMultiplexTransaction = ::Groovestack::Base::GraphQL::Tracers::AtomicMultiplexTransaction
161
+ end
107
162
  end
108
163
 
109
164
  ActiveRecord = ::Groovestack::Base::ActiveRecord
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: groovestack-base
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.7
4
+ version: 0.1.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Darren Rush
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2025-04-07 00:00:00.000000000 Z
11
+ date: 2025-04-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -116,27 +116,39 @@ files:
116
116
  - groovestack-base.gemspec
117
117
  - lib/groovestack/base.rb
118
118
  - lib/groovestack/base/active_record.rb
119
- - lib/groovestack/base/graphql/base_input_object.rb
120
- - lib/groovestack/base/graphql/base_mutation.rb
121
- - lib/groovestack/base/graphql/base_subscription.rb
122
- - lib/groovestack/base/graphql/documentation.rb
123
- - lib/groovestack/base/graphql/helpers.rb
124
- - lib/groovestack/base/graphql/providers/react_admin/resource.rb
125
- - lib/groovestack/base/graphql/providers/react_admin/types.rb
126
- - lib/groovestack/base/graphql/subscriptions/event_handler.rb
127
- - lib/groovestack/base/graphql/tracers/atomic_multiplex_transaction.rb
128
- - lib/groovestack/base/graphql/types.rb
119
+ - lib/groovestack/base/graphql/authorization/authorized_field.rb
120
+ - lib/groovestack/base/graphql/authorization/authorized_object.rb
121
+ - lib/groovestack/base/graphql/authorization/visible_field.rb
122
+ - lib/groovestack/base/graphql/base/argument.rb
123
+ - lib/groovestack/base/graphql/base/field.rb
124
+ - lib/groovestack/base/graphql/base/input_object.rb
125
+ - lib/groovestack/base/graphql/base/mutation.rb
126
+ - lib/groovestack/base/graphql/base/object.rb
127
+ - lib/groovestack/base/graphql/base/subscription.rb
128
+ - lib/groovestack/base/graphql/controllers/graphql_controller.rb
129
+ - lib/groovestack/base/graphql/documentation/arguments.rb
130
+ - lib/groovestack/base/graphql/documentation/fields.rb
131
+ - lib/groovestack/base/graphql/mutations/aasm_event_trigger.rb
132
+ - lib/groovestack/base/graphql/mutations/method_trigger.rb
133
+ - lib/groovestack/base/graphql/providers/react_admin.rb
134
+ - lib/groovestack/base/graphql/subscriptions/trigger.rb
135
+ - lib/groovestack/base/graphql/tracers.rb
136
+ - lib/groovestack/base/graphql/types/aasm_event_attributes.rb
137
+ - lib/groovestack/base/graphql/types/currency.rb
138
+ - lib/groovestack/base/graphql/types/money.rb
139
+ - lib/groovestack/base/graphql/types/subscription_payload.rb
140
+ - lib/groovestack/base/graphql/types/type_resolver.rb
129
141
  - lib/groovestack/base/listeners.rb
130
142
  - lib/groovestack/base/pub_sub.rb
131
143
  - lib/groovestack/base/puma/plugin/core_cron.rb
132
144
  - lib/groovestack/base/railtie.rb
133
145
  - lib/groovestack/base/utilities/string.rb
134
146
  - lib/groovestack/base/version.rb
135
- homepage: https://github.com/talysto/groovestack-core/
147
+ homepage: https://talysto.com/tech/groovestack/
136
148
  licenses: []
137
149
  metadata:
138
150
  allowed_push_host: https://rubygems.org
139
- homepage_uri: https://github.com/talysto/groovestack-core/
151
+ homepage_uri: https://talysto.com/tech/groovestack/
140
152
  source_code_uri: https://github.com/talysto/groovestack-core/
141
153
  changelog_uri: https://github.com/talysto/groovestack-core/
142
154
  rubygems_mfa_required: 'true'
@@ -1,11 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Groovestack
4
- module Base
5
- module GraphQL
6
- class BaseInputObject < ::GraphQL::Schema::InputObject
7
- argument_class ::Groovestack::Base::GraphQL::Types::BaseArgument
8
- end
9
- end
10
- end
11
- end
@@ -1,23 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Groovestack
4
- module Base
5
- module GraphQL
6
- class BaseMutation < ::GraphQL::Schema::Mutation
7
- argument_class ::Groovestack::Base::GraphQL::Types::BaseArgument
8
-
9
- def resolve(**args)
10
- perform(**args)
11
- rescue StandardError => e
12
- Groovestack::Base.notify_error(self.class, e)
13
-
14
- raise e
15
- end
16
-
17
- def perform(**args)
18
- raise NotImplementedError
19
- end
20
- end
21
- end
22
- end
23
- end
@@ -1,13 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Groovestack
4
- module Base
5
- module GraphQL
6
- class BaseSubscription < ::GraphQL::Schema::Subscription
7
- argument_class ::Groovestack::Base::GraphQL::Types::BaseArgument
8
- field_class ::Groovestack::Base::GraphQL::Types::BaseField
9
- object_class ::Groovestack::Base::GraphQL::Types::BaseObject
10
- end
11
- end
12
- end
13
- end
@@ -1,152 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Groovestack
4
- module Base
5
- module GraphQL
6
- module Helpers
7
- module Types
8
- module StatusEventVirtualAttributes
9
- def all_events
10
- object.class.aasm.events.map { |event| event.name.to_s }
11
- end
12
-
13
- def permitted_events
14
- object.aasm.events(permitted: true).map { |event| event.name.to_s }
15
- end
16
-
17
- def status_events
18
- all = all_events
19
- permitted = permitted_events
20
-
21
- all.map do |event|
22
- { name: event.titleize, key: event, enabled: permitted.include?(event) }
23
- end
24
- end
25
- end
26
-
27
- module Typified
28
- extend ActiveSupport::Concern
29
-
30
- included do
31
- field :type, String, null: true, resolver_method: :object_type
32
-
33
- def object_type
34
- object.class.to_s if object.respond_to?(:class)
35
- end
36
- end
37
- end
38
- end
39
-
40
- module Mutations
41
- module StatusEvents
42
- def trigger_status_event!(obj:, attrs:, event:, args: nil, authorization_policy: nil) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
43
- # NOTE: args spread requires keys to be symbols, but by default
44
- # they are passed as strings. This is why we use `symbolize_keys` here
45
- raise ::GraphQL::ExecutionError, 'event not present' if event.blank?
46
-
47
- event = event.gsub('!', '').to_sym
48
-
49
- if attrs.present?
50
- raise ::GraphQL::ExecutionError,
51
- 'Cannot update multiple attributes when firing instance methods'
52
- end
53
- unless obj.aasm.events.map(&:name).include?(event)
54
- raise ::GraphQL::ExecutionError,
55
- "#{event.capitalize} unavailable"
56
- end
57
- unless authorization_policy.nil? || authorization_policy.permitted_aasm_events.include?(event)
58
- raise ::GraphQL::ExecutionError,
59
- "Unauthorized not allowed to #{event} this #{obj.class}"
60
- end
61
-
62
- event = :"#{event}!"
63
-
64
- if args.present? && args.is_a?(Array)
65
- obj.send(event.to_s, *args)
66
- elsif args.present? && args.is_a?(Hash)
67
- obj.send(event.to_s, **args.symbolize_keys!)
68
- else
69
- obj.send(event.to_s)
70
- end
71
- end
72
- end
73
-
74
- module InstanceMethods
75
- def trigger_instance_method!(obj:, attrs:, instance_method:, args: nil, authorization_policy: nil) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
76
- raise GraphQL::ExecutionError, 'instance_method not present' if instance_method.blank?
77
-
78
- instance_method = instance_method.to_sym
79
-
80
- raise ::GraphQL::ExecutionError, 'instance_method undefined' unless obj.respond_to?(instance_method)
81
-
82
- if attrs.present?
83
- raise ::GraphQL::ExecutionError,
84
- 'Cannot update multiple attributes when triggering instance method'
85
- end
86
- unless authorization_policy.nil? || authorization_policy.permitted_instance_methods.include?(instance_method) # rubocop:disable Layout/LineLength
87
- raise ::GraphQL::ExecutionError,
88
- "Unauthorized not allowed to trigger #{instance_method} for this #{obj.class}"
89
- end
90
-
91
- if args.present? && args.is_a?(Array)
92
- obj.send(instance_method.to_s, *args)
93
- elsif args.present? && args.is_a?(Hash)
94
- obj.send(instance_method.to_s, **args.symbolize_keys!)
95
- else
96
- obj.send(instance_method.to_s)
97
- end
98
- end
99
- end
100
- end
101
-
102
- module Controller
103
- extend ActiveSupport::Concern
104
-
105
- included do # rubocop:disable Metrics/BlockLength
106
- private
107
-
108
- def operation_name
109
- params[:operationName]
110
- end
111
-
112
- def query
113
- params[:query]
114
- end
115
-
116
- def variables
117
- prepare_variables(params[:variables])
118
- end
119
-
120
- # Handle variables in form data, JSON body, or a blank value
121
- def prepare_variables(variables_param)
122
- case variables_param
123
- when String
124
- if variables_param.present?
125
- JSON.parse(variables_param) || {}
126
- else
127
- {}
128
- end
129
- when Hash
130
- variables_param
131
- when ActionController::Parameters
132
- variables_param.to_unsafe_hash # GraphQL-Ruby will validate name and type of incoming variables.
133
- when nil
134
- {}
135
- else
136
- raise ArgumentError, "Unexpected parameter: #{variables_param}"
137
- end
138
- end
139
-
140
- def handle_error_in_development(err)
141
- logger.error err.message
142
- logger.error err.backtrace.join('\/n')
143
-
144
- render json: { errors: [{ message: err.message, backtrace: err.backtrace }], data: {} },
145
- status: :internal_server_error
146
- end
147
- end
148
- end
149
- end
150
- end
151
- end
152
- end