Class: GraphQL::Relay::GlobalNodeIdentification
- Inherits:
-
Object
- Object
- GraphQL::Relay::GlobalNodeIdentification
- Includes:
- Define::InstanceDefinable
- 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.
Constant Summary collapse
- DEFAULT_TO_GLOBAL_ID =
-> (type_name, id) { id_str = id.to_s if type_name.include?(self.id_separator) || id_str.include?(self.id_separator) raise "to_global_id(#{type_name}, #{id}) contains reserved characters `#{self.id_separator}`" end Base64.strict_encode64([type_name, id_str].join(self.id_separator)) }
- DEFAULT_FROM_GLOBAL_ID =
-> (global_id) { Base64.decode64(global_id).split(self.id_separator) }
Class Attribute Summary collapse
-
.id_separator ⇒ Object
Returns the value of attribute id_separator.
-
.instance ⇒ Object
Returns the value of attribute instance.
Instance Attribute Summary collapse
-
#description ⇒ Object
Returns the value of attribute description.
Class Method Summary collapse
Instance Method Summary collapse
-
#field ⇒ Object
Returns a field for finding objects from a global ID, which Relay needs.
-
#from_global_id(global_id) ⇒ Object
Get type-name & ID from global ID (This reverts the opaque transform).
- #from_global_id=(proc) ⇒ Object
-
#initialize ⇒ GlobalNodeIdentification
constructor
A new instance of GlobalNodeIdentification.
-
#interface ⇒ Object
Returns ‘NodeInterface`, which all Relay types must implement.
-
#object_from_id(id, ctx) ⇒ Object
Use the provided config to get an object from a UUID.
- #object_from_id=(proc) ⇒ Object
-
#to_global_id(type_name, id) ⇒ Object
Create a global ID for type-name & ID (This is an opaque transform).
- #to_global_id=(proc) ⇒ Object
-
#type_from_object(object) ⇒ Object
Use the provided config to get a type for a given object.
- #type_from_object=(proc) ⇒ Object
Constructor Details
#initialize ⇒ GlobalNodeIdentification
32 33 34 35 |
# File 'lib/graphql/relay/global_node_identification.rb', line 32 def initialize @to_global_id_proc = DEFAULT_TO_GLOBAL_ID @from_global_id_proc = DEFAULT_FROM_GLOBAL_ID end |
Class Attribute Details
.id_separator ⇒ Object
Returns the value of attribute id_separator.
16 17 18 |
# File 'lib/graphql/relay/global_node_identification.rb', line 16 def id_separator @id_separator end |
.instance ⇒ Object
Returns the value of attribute instance.
16 17 18 |
# File 'lib/graphql/relay/global_node_identification.rb', line 16 def instance @instance end |
Instance Attribute Details
#description ⇒ Object
Returns the value of attribute description.
13 14 15 |
# File 'lib/graphql/relay/global_node_identification.rb', line 13 def description @description end |
Class Method Details
.from_global_id(id) ⇒ Object
21 22 23 |
# File 'lib/graphql/relay/global_node_identification.rb', line 21 def from_global_id(id) instance.from_global_id(id) end |
.new(*args, &block) ⇒ Object
17 18 19 |
# File 'lib/graphql/relay/global_node_identification.rb', line 17 def new(*args, &block) @instance = super end |
.to_global_id(type_name, id) ⇒ Object
25 26 27 |
# File 'lib/graphql/relay/global_node_identification.rb', line 25 def to_global_id(type_name, id) instance.to_global_id(type_name, id) end |
Instance Method Details
#field ⇒ Object
Returns a field for finding objects from a global ID, which Relay needs
52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/graphql/relay/global_node_identification.rb', line 52 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) } description ident.description end end |
#from_global_id(global_id) ⇒ Object
Get type-name & ID from global ID (This reverts the opaque transform)
88 89 90 |
# File 'lib/graphql/relay/global_node_identification.rb', line 88 def from_global_id(global_id) @from_global_id_proc.call(global_id) end |
#from_global_id=(proc) ⇒ Object
92 93 94 |
# File 'lib/graphql/relay/global_node_identification.rb', line 92 def from_global_id=(proc) @from_global_id_proc = proc end |
#interface ⇒ Object
Returns ‘NodeInterface`, which all Relay types must implement
38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/graphql/relay/global_node_identification.rb', line 38 def interface @interface ||= begin 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) ⇒ Object
Use the provided config to get an object from a UUID
116 117 118 |
# File 'lib/graphql/relay/global_node_identification.rb', line 116 def object_from_id(id, ctx) @object_from_id_proc.call(id, ctx) end |
#object_from_id=(proc) ⇒ Object
120 121 122 |
# File 'lib/graphql/relay/global_node_identification.rb', line 120 def object_from_id=(proc) @object_from_id_proc = proc end |
#to_global_id(type_name, id) ⇒ Object
Create a global ID for type-name & ID (This is an opaque transform)
78 79 80 |
# File 'lib/graphql/relay/global_node_identification.rb', line 78 def to_global_id(type_name, id) @to_global_id_proc.call(type_name, id) end |
#to_global_id=(proc) ⇒ Object
82 83 84 |
# File 'lib/graphql/relay/global_node_identification.rb', line 82 def to_global_id=(proc) @to_global_id_proc = proc end |
#type_from_object(object) ⇒ Object
Use the provided config to get a type for a given object
98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/graphql/relay/global_node_identification.rb', line 98 def type_from_object(object) 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) ⇒ Object
110 111 112 |
# File 'lib/graphql/relay/global_node_identification.rb', line 110 def type_from_object=(proc) @type_from_object_proc = proc end |