Class: GraphQL::Relay::GlobalNodeIdentification

Inherits:
Object
  • Object
show all
Includes:
DefinitionHelpers::DefinedByConfig
Defined in:
lib/graphql/relay/global_node_identification.rb

Overview

This object provides helpers for working with global IDs. It’s assumed you’ll only have 1! GlobalIdField depends on that, since it calls class methods which delegate to the singleton instance.

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.instanceObject

Returns the value of attribute instance.



14
15
16
# File 'lib/graphql/relay/global_node_identification.rb', line 14

def instance
  @instance
end

Instance Attribute Details

#object_from_id_procObject

Returns the value of attribute object_from_id_proc.



11
12
13
# File 'lib/graphql/relay/global_node_identification.rb', line 11

def object_from_id_proc
  @object_from_id_proc
end

#type_from_object_procObject

Returns the value of attribute type_from_object_proc.



11
12
13
# File 'lib/graphql/relay/global_node_identification.rb', line 11

def type_from_object_proc
  @type_from_object_proc
end

Class Method Details

.from_global_id(id) ⇒ Object



19
20
21
# File 'lib/graphql/relay/global_node_identification.rb', line 19

def from_global_id(id)
  instance.from_global_id(id)
end

.new(*args, &block) ⇒ Object



15
16
17
# File 'lib/graphql/relay/global_node_identification.rb', line 15

def new(*args, &block)
  @instance = super
end

.to_global_id(type_name, id) ⇒ Object



23
24
25
# File 'lib/graphql/relay/global_node_identification.rb', line 23

def to_global_id(type_name, id)
  instance.to_global_id(type_name, id)
end

Instance Method Details

#fieldObject

Returns a field for finding objects from a global ID, which Relay needs



43
44
45
46
47
48
49
50
51
52
# File 'lib/graphql/relay/global_node_identification.rb', line 43

def field
  ident = self
  GraphQL::Field.define do
    type(ident.interface)
    argument :id, !types.ID
    resolve -> (obj, args, ctx) {
      ident.object_from_id(args[:id])
    }
  end
end

#from_global_id(global_id) ⇒ Object

Get type-name & ID from global ID (This reverts the opaque transform)



62
63
64
# File 'lib/graphql/relay/global_node_identification.rb', line 62

def from_global_id(global_id)
  Base64.decode64(global_id).split("-")
end

#interfaceObject

Returns ‘NodeInterface`, which all Relay types must implement



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/graphql/relay/global_node_identification.rb', line 29

def interface
  @interface ||= begin
    ident = self
    GraphQL::InterfaceType.define do
      name "Node"
      field :id, !types.ID
      resolve_type -> (obj) {
        ident.type_from_object(obj)
      }
    end
  end
end

#object_from_id(id) ⇒ Object

Use the provided config to get an object from a UUID



80
81
82
# File 'lib/graphql/relay/global_node_identification.rb', line 80

def object_from_id(id)
  @object_from_id_proc.call(id)
end

#to_global_id(type_name, id) ⇒ Object

Create a global ID for type-name & ID (This is an opaque transform)



56
57
58
# File 'lib/graphql/relay/global_node_identification.rb', line 56

def to_global_id(type_name, id)
  Base64.strict_encode64("#{type_name}-#{id}")
end

#type_from_object(object) ⇒ Object

Use the provided config to get a type for a given object



68
69
70
71
72
73
74
75
76
# File 'lib/graphql/relay/global_node_identification.rb', line 68

def type_from_object(object)
  type_result = @type_from_object_proc.call(object)
  if !type_result.is_a?(GraphQL::BaseType)
    type_str = "#{type_result} (#{type_result.class.name})"
    raise "type_from_object(#{object}) returned #{type_str}, but it should return a GraphQL type"
  else
    type_result
  end
end