Class: GraphQLIncludable::Relay::Instrumentation

Inherits:
Object
  • Object
show all
Defined in:
lib/graphql_includable/relay/instrumentation.rb

Instance Method Summary collapse

Instance Method Details

#instrument(_type, field) ⇒ Object

rubocop:disable Metrics/AbcSize

Raises:

  • (ArgumentError)


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
# File 'lib/graphql_includable/relay/instrumentation.rb', line 7

def instrument(_type, field)
  return field unless edge_with_node_connection?(field)

  raise ArgumentError, 'Connection does not support fetching using :property' if field.property.present?

  is_proc_based = proc_based?(field)

  validate!(field, is_proc_based)
  properties = field.[:connection_properties]
  edge_to_node_property = properties[:edge_to_node]
  edges_prop = properties[:edges]
  nodes_prop = properties[:nodes]

  if is_proc_based
    edges_resolver = field.[:resolve_edges]
    nodes_resolver = field.[:resolve_nodes]
  else
    # Use the edges and nodes symbols from the incldues pattern as the propeties to fetch
    edges_resolver = ->(obj, _args, _ctx) { obj.public_send(edges_prop) }
    nodes_resolver = ->(obj, _args, _ctx) { obj.public_send(nodes_prop) }
  end

  _original_resolve = field.resolve_proc
  new_resolve_proc = ->(obj, args, ctx) do
    ConnectionEdgesAndNodes.new(obj, args, ctx,
                                edges_prop, nodes_prop, edge_to_node_property,
                                edges_resolver, nodes_resolver)
  end

  field.redefine { resolve(new_resolve_proc) }
end