class GraphQL::Cache::Key
Represents a cache key generated from the graphql context provided when initialized
Attributes
Arguments passed during graphql query execution
The graphql field being resolved
Metadata passed to the cache key on field definition
The resolved parent object (object this resolver method is called on)
The graphql parent type
Public Class Methods
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
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
Produces the portion of the key representing the resolving field
# File lib/graphql/cache/key.rb, line 68 def field_clause field.name end
@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
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
@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
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
Produces the portion of the key representing the parent type
# File lib/graphql/cache/key.rb, line 63 def type_clause type.name end