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

.id_separatorObject

Returns the value of attribute id_separator


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

def id_separator
  @id_separator
end

.instanceObject

Returns the value of attribute instance


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

def instance
  @instance
end

Instance Attribute Details

#object_from_id_procObject

Returns the value of attribute object_from_id_proc


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

def object_from_id_proc
  @object_from_id_proc
end

#type_from_object_procObject

Returns the value of attribute type_from_object_proc


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

def type_from_object_proc
  @type_from_object_proc
end

Class Method Details

.from_global_id(id) ⇒ Object


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

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

.new(*args, &block) ⇒ Object


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

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

.to_global_id(type_name, id) ⇒ Object


28
29
30
# File 'lib/graphql/relay/global_node_identification.rb', line 28

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


48
49
50
51
52
53
54
55
56
57
# File 'lib/graphql/relay/global_node_identification.rb', line 48

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], ctx)
    }
  end
end

#from_global_id(global_id) ⇒ Object

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


71
72
73
# File 'lib/graphql/relay/global_node_identification.rb', line 71

def from_global_id(global_id)
  Base64.decode64(global_id).split(self.class.id_separator)
end

#interfaceObject

Returns `NodeInterface`, which all Relay types must implement


34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/graphql/relay/global_node_identification.rb', line 34

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, ctx) ⇒ Object

Use the provided config to get an object from a UUID


89
90
91
# File 'lib/graphql/relay/global_node_identification.rb', line 89

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

#to_global_id(type_name, id) ⇒ Object

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


61
62
63
64
65
66
67
# File 'lib/graphql/relay/global_node_identification.rb', line 61

def to_global_id(type_name, id)
  id_str = id.to_s
  if type_name.include?(self.class.id_separator) || id_str.include?(self.class.id_separator)
    raise "to_global_id(#{type_name}, #{id}) contains reserved characters `#{self.class.id_separator}`"
  end
  Base64.strict_encode64([type_name, id_str].join(self.class.id_separator))
end

#type_from_object(object) ⇒ Object

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


77
78
79
80
81
82
83
84
85
# File 'lib/graphql/relay/global_node_identification.rb', line 77

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