Class: Rails::GraphQL::GlobalID

Inherits:
GlobalID
  • Object
show all
Defined in:
lib/rails/graphql/global_id.rb

Overview

GraphQL Global ID

GraphQL implementation of the Global ID that adds extended support to how things are located and stored. Mostly used for caching and custom access to the tree and request process.

TODO: Implement signed operations

Defined Under Namespace

Classes: Serializer

Constant Summary collapse

SERIALIZER_KEY =
"_gql_globalid"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(gid, options = {}) ⇒ GlobalID

Returns a new instance of GlobalID.



66
67
68
# File 'lib/rails/graphql/global_id.rb', line 66

def initialize(gid, options = {})
  @uri = gid.is_a?(URI::GQL) ? gid : URI::GQL.parse(gid)
end

Class Method Details

.create(object, options = nil) ⇒ Object

Create a new GraphQL Global identifier



52
53
54
55
# File 'lib/rails/graphql/global_id.rb', line 52

def create(object, options = nil)
  scope = options&.delete(:scope) || scope_of(object)
  new(URI::GQL.create(object, scope, options), options)
end

.scope_of(object) ⇒ Object

Find the scope on which the object is applied to



58
59
60
# File 'lib/rails/graphql/global_id.rb', line 58

def scope_of(object)
  object.try(:schema_type) if object.gid_base_class.is_a?(Helpers::WithSchemaFields)
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



82
83
84
# File 'lib/rails/graphql/global_id.rb', line 82

def ==(other)
  other.is_a?(GlobalID) && @uri == other.uri
end

#base_classObject



74
75
76
77
78
79
80
# File 'lib/rails/graphql/global_id.rb', line 74

def base_class
  if %w[Schema Directive Type].include?(class_name)
    GraphQL.const_get(class_name, false)
  else
    GraphQL.type_map.fetch(class_name, namespace: namespace)
  end
end

#find(options = {}) ⇒ Object



70
71
72
# File 'lib/rails/graphql/global_id.rb', line 70

def find(options = {})
  base_class.try(:find_by_gid, self)
end