Class: ActiveAdmin::GraphQL::ResourceQueryProxy

Inherits:
Object
  • Object
show all
Includes:
Controller
Defined in:
lib/active_admin/graphql/resource_query_proxy.rb,
lib/active_admin/graphql/resource_query_proxy/controller.rb

Overview

Reuses ResourceController data-access behaviour (+scoped_collection+, find_collection, find_resource, authorization scoping, Ransack, menu scopes, sorting, includes) so GraphQL list/detail mutations align with the HTML/JSON list and member REST endpoints.

Defined Under Namespace

Modules: Controller

Instance Method Summary collapse

Constructor Details

#initialize(aa_resource:, user:, namespace:, graph_params: {}) ⇒ ResourceQueryProxy

Returns a new instance of ResourceQueryProxy.



16
17
18
19
20
21
# File 'lib/active_admin/graphql/resource_query_proxy.rb', line 16

def initialize(aa_resource:, user:, namespace:, graph_params: {})
  @aa_resource = aa_resource
  @user = user
  @namespace = namespace
  @graph_params = normalize_graph_params(graph_params)
end

Instance Method Details

#build_new(attributes) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/active_admin/graphql/resource_query_proxy.rb', line 52

def build_new(attributes)
  if missing_required_belongs_to?
    raise ::GraphQL::ExecutionError,
      "#{@aa_resource.belongs_to_config.to_param} is required for nested resource #{@aa_resource.resource_name}"
  end

  c = controller_for("new")
  chain = c.send(:apply_authorization_scope, c.send(:scoped_collection))
  permitted = attributes.stringify_keys.slice(*@aa_resource.graphql_assignable_attribute_names)
  chain.build(permitted)
end

#find_member(id) ⇒ Object



27
28
29
30
31
32
# File 'lib/active_admin/graphql/resource_query_proxy.rb', line 27

def find_member(id)
  extra = member_route_params_for_find(id)
  controller_for("show", extra).send(:find_resource)
rescue ActiveRecord::RecordNotFound
  nil
end

#find_members(ids) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/active_admin/graphql/resource_query_proxy.rb', line 34

def find_members(ids)
  string_ids = Array(ids).map(&:to_s).uniq
  return {} if string_ids.empty?

  model = @aa_resource.resource_class
  if ActiveAdmin::PrimaryKey.composite?(model)
    return string_ids.index_with { |identifier| find_member(identifier) }
  end

  controller = controller_for("index")
  relation = controller.send(:apply_authorization_scope, controller.send(:scoped_collection))
  primary_key = model.primary_key.to_sym
  indexed = relation.where(primary_key => string_ids).index_by do |record|
    record.public_send(primary_key).to_s
  end
  string_ids.index_with { |identifier| indexed[identifier] }
end

#relation_for_indexObject



23
24
25
# File 'lib/active_admin/graphql/resource_query_proxy.rb', line 23

def relation_for_index
  controller_for("index").send(:find_collection, except: %i[pagination collection_decorator])
end

#run_batch_action(batch_sym, ids, inputs: {}) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/active_admin/graphql/resource_query_proxy.rb', line 64

def run_batch_action(batch_sym, ids, inputs: {})
  unless @aa_resource.batch_actions_enabled?
    raise ::GraphQL::ExecutionError, "batch actions are disabled for #{@aa_resource.resource_name}"
  end

  inputs = normalize_batch_inputs(inputs)
  extra = {
    "batch_action" => batch_sym.to_s,
    "collection_selection" => Array(ids).map(&:to_s),
    # TruffleRuby freezes JSON's internal "{}" buffer, so Hash#to_json raises FrozenError.
    # Dumping into a dedicated StringIO avoids mutating the shared literal.
    "batch_action_inputs" => dump_batch_inputs(inputs)
  }
  c = controller_for("batch_action", extra)
  perform_controller_command!(c) { c.send(:batch_action) }
end

#run_collection_action(action_name, extra_params: {}) ⇒ Object



92
93
94
95
96
97
98
99
100
101
# File 'lib/active_admin/graphql/resource_query_proxy.rb', line 92

def run_collection_action(action_name, extra_params: {})
  action_name = action_name.to_s
  unless @aa_resource.collection_actions.any? { |a| a.name.to_s == action_name }
    raise ::GraphQL::ExecutionError, "unknown collection action #{action_name.inspect}"
  end

  extras = normalize_extra_params(extra_params)
  c = controller_for(action_name, extras)
  perform_controller_command!(c) { c.send(action_name) }
end

#run_member_action(action_name, id, extra_params: {}) ⇒ Object



81
82
83
84
85
86
87
88
89
90
# File 'lib/active_admin/graphql/resource_query_proxy.rb', line 81

def run_member_action(action_name, id, extra_params: {})
  action_name = action_name.to_s
  unless @aa_resource.member_actions.any? { |a| a.name.to_s == action_name }
    raise ::GraphQL::ExecutionError, "unknown member action #{action_name.inspect}"
  end

  extras = normalize_extra_params(extra_params)
  c = controller_for(action_name, {"id" => id.to_s}.merge(extras))
  perform_controller_command!(c) { c.send(action_name) }
end