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 Attribute Summary collapse
-
#schema ⇒ Object
Memoize the schema to support deprecated node_ident-level resolve functions TODO: remove after Schema.resolve_type is required.
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 TODO: remove after Schema.resolve_type is required.
- #type_from_object=(new_type_from_object_proc) ⇒ Object
Methods included from Define::InstanceDefinable
#definition_proc=, included, #metadata
Constructor Details
#initialize ⇒ GlobalNodeIdentification
Returns a new instance of GlobalNodeIdentification.
25 26 27 28 |
# File 'lib/graphql/relay/global_node_identification.rb', line 25 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.
20 21 22 |
# File 'lib/graphql/relay/global_node_identification.rb', line 20 def id_separator @id_separator end |
Instance Attribute Details
#schema ⇒ Object
Memoize the schema to support deprecated node_ident-level resolve functions TODO: remove after Schema.resolve_type is required
17 18 19 |
# File 'lib/graphql/relay/global_node_identification.rb', line 17 def schema @schema end |
Instance Method Details
#field ⇒ Object
Returns a field for finding objects from a global ID, which Relay needs
54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/graphql/relay/global_node_identification.rb', line 54 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) ⇒ Object
Get type-name & ID from global ID (This reverts the opaque transform)
93 94 95 96 |
# File 'lib/graphql/relay/global_node_identification.rb', line 93 def from_global_id(global_id) ensure_defined @from_global_id_proc.call(global_id) end |
#from_global_id=(proc) ⇒ Object
98 99 100 101 |
# File 'lib/graphql/relay/global_node_identification.rb', line 98 def from_global_id=(proc) ensure_defined @from_global_id_proc = proc end |
#interface ⇒ Object
Returns ‘NodeInterface`, which all Relay types must implement
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/graphql/relay/global_node_identification.rb', line 31 def interface @interface ||= begin ensure_defined ident = self if @type_from_object_proc # TODO: remove after Schema.resolve_type is required GraphQL::InterfaceType.define do name "Node" field :id, !types.ID resolve_type -> (obj, ctx) { ident.type_from_object(obj) } end else GraphQL::InterfaceType.define do name "Node" field :id, !types.ID end end end end |
#object_from_id(id, ctx) ⇒ Object
Use the provided config to get an object from a UUID
126 127 128 129 |
# File 'lib/graphql/relay/global_node_identification.rb', line 126 def object_from_id(id, ctx) ensure_defined @object_from_id_proc.call(id, ctx) end |
#object_from_id=(proc) ⇒ Object
131 132 133 134 |
# File 'lib/graphql/relay/global_node_identification.rb', line 131 def object_from_id=(proc) ensure_defined @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)
81 82 83 84 |
# File 'lib/graphql/relay/global_node_identification.rb', line 81 def to_global_id(type_name, id) ensure_defined @to_global_id_proc.call(type_name, id) end |
#to_global_id=(proc) ⇒ Object
86 87 88 89 |
# File 'lib/graphql/relay/global_node_identification.rb', line 86 def to_global_id=(proc) ensure_defined @to_global_id_proc = proc end |
#type_from_object(object) ⇒ Object
Use the provided config to get a type for a given object TODO: remove after Schema.resolve_type is required
106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/graphql/relay/global_node_identification.rb', line 106 def type_from_object(object) ensure_defined warn("type_from_object(object) is deprecated; use Schema.resolve_type(object) instead") if @type_from_object_proc schema.resolve_type = @type_from_object_proc @type_from_object_proc = nil end schema.resolve_type(object) end |
#type_from_object=(new_type_from_object_proc) ⇒ Object
118 119 120 121 122 |
# File 'lib/graphql/relay/global_node_identification.rb', line 118 def type_from_object=(new_type_from_object_proc) ensure_defined warn("type_from_object(object) is deprecated; use Schema.resolve_type(object) instead") @type_from_object_proc = new_type_from_object_proc end |