Class: GraphQL::Relay::Identifier::GlobalObjectIdentifier
- Inherits:
-
Object
- Object
- GraphQL::Relay::Identifier::GlobalObjectIdentifier
- Defined in:
- lib/graphql/relay/identifier/global_object_identifier.rb
Overview
GlobalObjectIdentifier is a utility class that provides a way to create and manage unique identifiers for entities implementing a Node in a GraphQL schema.
This implementation is ORM-agnostic but assumes certain methods like find_by! are available on your model classes. You may need to customize the find method based on your actual ORM.
Defined Under Namespace
Classes: Identifier
Class Method Summary collapse
- .add_identifier(klass, *attribute_names) ⇒ Object
- .from_relay_id(node_id) ⇒ Object
- .parse(node_id) ⇒ Object
- .remove_identifier(klass) ⇒ Object
- .to_relay_id(object, type) ⇒ Object
- .unsafe_to_relay_id(object, graphql_name:, object_class_name:) ⇒ Object
Class Method Details
.add_identifier(klass, *attribute_names) ⇒ Object
140 141 142 |
# File 'lib/graphql/relay/identifier/global_object_identifier.rb', line 140 def add_identifier(klass, *attribute_names) identifiers[klass] = Identifier.build_for(klass, *attribute_names) end |
.from_relay_id(node_id) ⇒ Object
144 145 146 |
# File 'lib/graphql/relay/identifier/global_object_identifier.rb', line 144 def from_relay_id(node_id) parse(node_id)&.find end |
.parse(node_id) ⇒ Object
148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/graphql/relay/identifier/global_object_identifier.rb', line 148 def parse(node_id) _typename, json = GraphQL::Schema::UniqueWithinType.decode(node_id) return unless json hash = JSON.parse(json) klass = hash["klass"] identifiers[klass].new(hash) rescue JSON::ParserError, GraphQL::ExecutionError nil end |
.remove_identifier(klass) ⇒ Object
161 162 163 164 |
# File 'lib/graphql/relay/identifier/global_object_identifier.rb', line 161 def remove_identifier(klass) identifiers.delete(klass) Identifier.remove_for(klass) end |
.to_relay_id(object, type) ⇒ Object
166 167 168 |
# File 'lib/graphql/relay/identifier/global_object_identifier.rb', line 166 def to_relay_id(object, type) unsafe_to_relay_id(object, graphql_name: type.graphql_name, object_class_name: object.class.name) end |
.unsafe_to_relay_id(object, graphql_name:, object_class_name:) ⇒ Object
170 171 172 173 174 175 176 177 |
# File 'lib/graphql/relay/identifier/global_object_identifier.rb', line 170 def unsafe_to_relay_id(object, graphql_name:, object_class_name:) # This allows us to get relay ids for types without having to access the GraphQL type, but without the safety # or consistency of the GraphQL type. Used by external callers (e.g. the API) to generate relay ids. GraphQL::Schema::UniqueWithinType.encode( graphql_name, identifiers[object_class_name].as_json(object).to_json ) end |