Class: GraphQL::Relay::GlobalNodeIdentification
- Inherits:
-
Object
- Object
- GraphQL::Relay::GlobalNodeIdentification
- 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.
Instance Attribute Summary collapse
-
#object_from_id_proc ⇒ Object
Returns the value of attribute object_from_id_proc.
-
#type_from_object_proc ⇒ Object
Returns the value of attribute type_from_object_proc.
Class Method Summary collapse
- .from_global_id(id) ⇒ Object
- .instance ⇒ Object
- .new(*args, &block) ⇒ Object
- .to_global_id(type_name, id) ⇒ Object
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).
-
#interface ⇒ Object
Returns ‘NodeInterface`, which all Relay types must implement.
-
#object_from_id(id) ⇒ Object
Use the provided config to get an object from a UUID.
-
#to_global_id(type_name, id) ⇒ Object
Create a global ID for type-name & ID (This is an opaque transform).
-
#type_from_object(object) ⇒ Object
Use the provided config to get a type for a given object.
Instance Attribute Details
#object_from_id_proc ⇒ Object
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_proc ⇒ Object
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
26 27 28 |
# File 'lib/graphql/relay/global_node_identification.rb', line 26 def from_global_id(id) @instance.from_global_id(id) end |
.instance ⇒ Object
22 23 24 |
# File 'lib/graphql/relay/global_node_identification.rb', line 22 def instance @instance end |
.new(*args, &block) ⇒ Object
14 15 16 17 18 19 20 |
# File 'lib/graphql/relay/global_node_identification.rb', line 14 def new(*args, &block) if @instance.nil? @instance = super else raise("Can't make a second global identifier!") end end |
.to_global_id(type_name, id) ⇒ Object
30 31 32 |
# File 'lib/graphql/relay/global_node_identification.rb', line 30 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
50 51 52 53 54 55 56 57 58 59 |
# File 'lib/graphql/relay/global_node_identification.rb', line 50 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)
69 70 71 |
# File 'lib/graphql/relay/global_node_identification.rb', line 69 def from_global_id(global_id) Base64.decode64(global_id).split("-") end |
#interface ⇒ Object
Returns ‘NodeInterface`, which all Relay types must implement
36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/graphql/relay/global_node_identification.rb', line 36 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
81 82 83 |
# File 'lib/graphql/relay/global_node_identification.rb', line 81 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)
63 64 65 |
# File 'lib/graphql/relay/global_node_identification.rb', line 63 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
75 76 77 |
# File 'lib/graphql/relay/global_node_identification.rb', line 75 def type_from_object(object) @type_from_object_proc.call(object) end |