Class: GraphQL::DefinitionHelpers::DefinedByConfig::DefinitionConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/graphql/relay/monkey_patches/definition_config.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#object_from_id_procObject

Support GlobalNodeIdentification



43
44
45
# File 'lib/graphql/relay/monkey_patches/definition_config.rb', line 43

def object_from_id_proc
  @object_from_id_proc
end

#type_from_object_procObject

Support GlobalNodeIdentification



43
44
45
# File 'lib/graphql/relay/monkey_patches/definition_config.rb', line 43

def type_from_object_proc
  @type_from_object_proc
end

Instance Method Details

#connection(name, type = nil, desc = nil, property: nil, &block) ⇒ Object

Wraps a field definition with a ConnectionField

  • applies default fields

  • wraps the resolve proc to make a connection



6
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
# File 'lib/graphql/relay/monkey_patches/definition_config.rb', line 6

def connection(name, type = nil, desc = nil, property: nil, &block)
  # Wrap the given block to define the default args
  definition_block = -> (config) {
    argument :first, types.Int
    argument :after, types.String
    argument :last, types.Int
    argument :before, types.String
    argument :order, types.String
    self.instance_eval(&block) if block_given?
  }
  connection_field = field(name, type, desc, property: property, &definition_block)
  # Wrap the defined resolve proc
  # TODO: make a public API on GraphQL::Field to expose this proc
  original_resolve = connection_field.instance_variable_get(:@resolve_proc)
  connection_resolve = -> (obj, args, ctx) {
    items = original_resolve.call(obj, args, ctx)
    if items == GraphQL::Query::DEFAULT_RESOLVE
      method_name = property || name
      p "Obj: #{obj}  ##{method_name}"
      items = obj.public_send(method_name)
    end
    connection_class = GraphQL::Relay::BaseConnection.connection_for_items(items)
    connection_class.new(items, args)
  }
  connection_field.resolve = connection_resolve
  fields[name.to_s] = connection_field
end

#global_id_field(field_name) ⇒ Object



37
38
39
40
# File 'lib/graphql/relay/monkey_patches/definition_config.rb', line 37

def global_id_field(field_name)
  name || raise("You must define the type's name before creating a GlobalIdField")
  field(field_name, field: GraphQL::Relay::GlobalIdField.new(name))
end

#object_from_id(proc) ⇒ Object



44
45
46
# File 'lib/graphql/relay/monkey_patches/definition_config.rb', line 44

def object_from_id(proc)
  @object_from_id_proc = proc
end

#type_from_object(proc) ⇒ Object



48
49
50
# File 'lib/graphql/relay/monkey_patches/definition_config.rb', line 48

def type_from_object(proc)
  @type_from_object_proc = proc
end