class GraphQL::Cache::Key

Represents a cache key generated from the graphql context provided when initialized

Attributes

arguments[RW]

Arguments passed during graphql query execution

field[RW]

The graphql field being resolved

metadata[RW]

Metadata passed to the cache key on field definition

object[RW]

The resolved parent object (object this resolver method is called on)

type[RW]

The graphql parent type

Public Class Methods

new(obj, args, type, field) click to toggle source

Initializes a new Key with the given graphql query context

@param obj [Object] The resolved parent object for a field's resolution @param args [GraphQL::Arguments] The internal graphql-ruby wrapper for field arguments @param type [GraphQL::Schema::Type] The type definition of the parent object @param field [GraphQL::Schema::Field] The field being resolved

# File lib/graphql/cache/key.rb, line 27
def initialize(obj, args, type, field)
  @object    = obj.object
  @arguments = args
  @type      = type
  @field     = field
  @metadata  = field.metadata[:cache]

  @metadata = { cache: @metadata } unless @metadata.is_a?(Hash)
end

Public Instance Methods

arguments_clause() click to toggle source

Produces the portion of the key representing the query arguments

# File lib/graphql/cache/key.rb, line 73
def arguments_clause
  @arguments_clause ||= arguments.to_h.to_a.flatten
end
field_clause() click to toggle source

Produces the portion of the key representing the resolving field

# File lib/graphql/cache/key.rb, line 68
def field_clause
  field.name
end
guess_id() click to toggle source

@private

# File lib/graphql/cache/key.rb, line 92
def guess_id
  return object.cache_key_with_version if object.respond_to?(:cache_key_with_version)
  return object.cache_key if object.respond_to?(:cache_key)
  return object.id if object.respond_to?(:id)
  object.object_id
end
object_clause() click to toggle source

Produces the portion of the key representing the parent object

# File lib/graphql/cache/key.rb, line 56
def object_clause
  return nil unless object

  "#{object.class.name}:#{object_identifier}"
end
object_identifier() click to toggle source

@private

# File lib/graphql/cache/key.rb, line 78
def object_identifier
  case metadata[:key]
  when Symbol
    object.send(metadata[:key])
  when Proc
    metadata[:key].call(object)
  when NilClass
    guess_id
  else
    metadata[:key]
  end
end
to_s() click to toggle source

Returns the string representation of this cache key suitable for using as a key when writing to cache

The key is constructed with this structure:

“` namespace:type:field:arguments:object-id “`

# File lib/graphql/cache/key.rb, line 45
def to_s
  @to_s ||= [
    GraphQL::Cache.namespace,
    type_clause,
    field_clause,
    arguments_clause,
    object_clause
  ].flatten.compact.join(':')
end
type_clause() click to toggle source

Produces the portion of the key representing the parent type

# File lib/graphql/cache/key.rb, line 63
def type_clause
  type.name
end