Class: GraphQL::Cache::Key
- Inherits:
-
Object
- Object
- GraphQL::Cache::Key
- Defined in:
- lib/graphql/cache/key.rb
Overview
Represents a cache key generated from the graphql context provided when initialized
Instance Attribute Summary collapse
-
#arguments ⇒ Object
Arguments passed during graphql query execution.
-
#field ⇒ Object
The graphql field being resolved.
-
#metadata ⇒ Object
Metadata passed to the cache key on field definition.
-
#object ⇒ Object
The resolved parent object (object this resolver method is called on).
-
#type ⇒ Object
The graphql parent type.
Instance Method Summary collapse
-
#arguments_clause ⇒ Object
Produces the portion of the key representing the query arguments.
-
#field_clause ⇒ Object
Produces the portion of the key representing the resolving field.
-
#initialize(obj, args, type, field) ⇒ Key
constructor
Initializes a new Key with the given graphql query context.
-
#object_clause ⇒ Object
Produces the portion of the key representing the parent object.
-
#to_s ⇒ Object
Returns the string representation of this cache key suitable for using as a key when writing to cache.
-
#type_clause ⇒ Object
Produces the portion of the key representing the parent type.
Constructor Details
#initialize(obj, args, type, field) ⇒ Key
Initializes a new Key with the given graphql query context
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
#arguments ⇒ Object
Arguments passed during graphql query execution
10 11 12 |
# File 'lib/graphql/cache/key.rb', line 10 def arguments @arguments end |
#field ⇒ Object
The graphql field being resolved
16 17 18 |
# File 'lib/graphql/cache/key.rb', line 16 def field @field end |
#metadata ⇒ Object
Metadata passed to the cache key on field definition
19 20 21 |
# File 'lib/graphql/cache/key.rb', line 19 def @metadata end |
#object ⇒ Object
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 |
#type ⇒ Object
The graphql parent type
13 14 15 |
# File 'lib/graphql/cache/key.rb', line 13 def type @type end |
Instance Method Details
#arguments_clause ⇒ Object
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_clause ⇒ Object
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_clause ⇒ Object
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_s ⇒ Object
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_clause ⇒ Object
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 |