Class: GraphQL::Cache::Key

Inherits:
Object
  • Object
show all
Defined in:
lib/graphql/cache/key.rb

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(obj, args, type, field) ⇒ Key

Initializes a new Key with the given graphql query context

Parameters:

  • obj (Object)

    The resolved parent object for a field's resolution

  • args (GraphQL::Arguments)

    The internal graphql-ruby wrapper for field arguments

  • type (GraphQL::Schema::Type)

    The type definition of the parent object

  • field (GraphQL::Schema::Field)

    The field being resolved



27
28
29
30
31
32
33
34
35
# 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.[:cache]

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

Instance Attribute Details

#argumentsObject

Arguments passed during graphql query execution



10
11
12
# File 'lib/graphql/cache/key.rb', line 10

def arguments
  @arguments
end

#fieldObject

The graphql field being resolved



16
17
18
# File 'lib/graphql/cache/key.rb', line 16

def field
  @field
end

#metadataObject

Metadata passed to the cache key on field definition



19
20
21
# File 'lib/graphql/cache/key.rb', line 19

def 
  @metadata
end

#objectObject

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



7
8
9
# File 'lib/graphql/cache/key.rb', line 7

def object
  @object
end

#typeObject

The graphql parent type



13
14
15
# File 'lib/graphql/cache/key.rb', line 13

def type
  @type
end

Instance Method Details

#arguments_clauseObject

Produces the portion of the key representing the query arguments



73
74
75
# File 'lib/graphql/cache/key.rb', line 73

def arguments_clause
  @arguments_clause ||= arguments.to_h.to_a.flatten
end

#field_clauseObject

Produces the portion of the key representing the resolving field



68
69
70
# File 'lib/graphql/cache/key.rb', line 68

def field_clause
  field.name
end

#object_clauseObject

Produces the portion of the key representing the parent object



56
57
58
59
60
# File 'lib/graphql/cache/key.rb', line 56

def object_clause
  return nil unless object

  "#{object.class.name}:#{object_identifier}"
end

#to_sObject

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


45
46
47
48
49
50
51
52
53
# 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_clauseObject

Produces the portion of the key representing the parent type



63
64
65
# File 'lib/graphql/cache/key.rb', line 63

def type_clause
  type.name
end