Class: GraphitiGql::Schema::Fields::ToMany

Inherits:
Object
  • Object
show all
Defined in:
lib/graphiti_gql/schema/fields/to_many.rb

Instance Method Summary collapse

Constructor Details

#initialize(sideload, sideload_type) ⇒ ToMany

Returns a new instance of ToMany.



5
6
7
8
9
10
# File 'lib/graphiti_gql/schema/fields/to_many.rb', line 5

def initialize(sideload, sideload_type)
  @sideload = sideload
  @sideload_resource = Schema.registry.get(sideload.resource.class)[:resource]
  @sideload_type = sideload_type
  @connection_type = find_or_build_connection
end

Instance Method Details

#apply(type) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/graphiti_gql/schema/fields/to_many.rb', line 12

def apply(type)
  opts = {
    null: has_one?,
    connection: false,
    extras: [:lookahead]
  }
  field_type = has_one? ? @sideload_type : @connection_type
  field = type.field @sideload.name,
    field_type,
    **opts
  unless has_one?
    field.extension(RelayConnectionExtension, resource: @sideload_resource)
  end
  ListArguments.new(@sideload_resource, @sideload).apply(field)
  _sideload = @sideload
  type.define_method(@sideload.name) do |**arguments|
    Util.is_readable_sideload!(_sideload)
    params = Util.params_from_args(arguments)
    Loaders::Many.factory(_sideload, params).load(object)
  end
end