class GraphQL::Relay::GlobalNodeIdentification

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.

Constants

DEFAULT_FROM_GLOBAL_ID
DEFAULT_TO_GLOBAL_ID

Attributes

id_separator[RW]

Public Class Methods

new() click to toggle source
# File lib/graphql/relay/global_node_identification.rb, line 21
def initialize
  @to_global_id_proc = DEFAULT_TO_GLOBAL_ID
  @from_global_id_proc = DEFAULT_FROM_GLOBAL_ID
end

Public Instance Methods

field() click to toggle source

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

# File lib/graphql/relay/global_node_identification.rb, line 42
def field
  ensure_defined
  ident = self
  GraphQL::Field.define do
    type(ident.interface)
    argument :id, !types.ID
    resolve -> (obj, args, ctx) {
      ctx.query.schema.node_identification.object_from_id(args[:id], ctx)
    }
    description ident.description
  end
end
from_global_id(global_id) click to toggle source

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

# File lib/graphql/relay/global_node_identification.rb, line 81
def from_global_id(global_id)
  ensure_defined
  @from_global_id_proc.call(global_id)
end
from_global_id=(proc) click to toggle source
# File lib/graphql/relay/global_node_identification.rb, line 86
def from_global_id=(proc)
  ensure_defined
  @from_global_id_proc = proc
end
interface() click to toggle source

Returns `NodeInterface`, which all Relay types must implement

# File lib/graphql/relay/global_node_identification.rb, line 27
def interface
  @interface ||= begin
    ensure_defined
    ident = self
    GraphQL::InterfaceType.define do
      name "Node"
      field :id, !types.ID
      resolve_type -> (obj, schema) {
        ident.type_from_object(obj)
      }
    end
  end
end
object_from_id(id, ctx) click to toggle source

Use the provided config to get an object from a UUID

# File lib/graphql/relay/global_node_identification.rb, line 113
def object_from_id(id, ctx)
  ensure_defined
  @object_from_id_proc.call(id, ctx)
end
object_from_id=(proc) click to toggle source
# File lib/graphql/relay/global_node_identification.rb, line 118
def object_from_id=(proc)
  ensure_defined
  @object_from_id_proc = proc
end
to_global_id(type_name, id) click to toggle source

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

# File lib/graphql/relay/global_node_identification.rb, line 69
def to_global_id(type_name, id)
  ensure_defined
  @to_global_id_proc.call(type_name, id)
end
to_global_id=(proc) click to toggle source
# File lib/graphql/relay/global_node_identification.rb, line 74
def to_global_id=(proc)
  ensure_defined
  @to_global_id_proc = proc
end
type_from_object(object) click to toggle source

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

# File lib/graphql/relay/global_node_identification.rb, line 93
def type_from_object(object)
  ensure_defined
  type_result = @type_from_object_proc.call(object)
  if type_result.nil?
    nil
  elsif !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
type_from_object=(proc) click to toggle source
# File lib/graphql/relay/global_node_identification.rb, line 106
def type_from_object=(proc)
  ensure_defined
  @type_from_object_proc = proc
end