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



22
23
24
25
26
27
28
29
30
# File 'lib/graphql/cache/key.rb', line 22

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



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

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

#field_clauseObject

Produces the portion of the key representing the resolving field



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

def field_clause
  field.name
end

#object_clauseObject

Produces the portion of the key representing the parent object



45
46
47
48
49
# File 'lib/graphql/cache/key.rb', line 45

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



34
35
36
37
38
39
40
41
42
# File 'lib/graphql/cache/key.rb', line 34

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



52
53
54
# File 'lib/graphql/cache/key.rb', line 52

def type_clause
  type.name
end