Class: GraphQL::Cache::Middleware
- Inherits:
-
Object
- Object
- GraphQL::Cache::Middleware
- Defined in:
- lib/graphql/cache/middleware.rb
Overview
graphql-ruby middleware to wrap resolvers for caching
Instance Attribute Summary collapse
-
#cache ⇒ Object
Returns the value of attribute cache.
-
#field_args ⇒ Object
Returns the value of attribute field_args.
-
#field_definition ⇒ Object
Returns the value of attribute field_definition.
-
#object ⇒ Object
Returns the value of attribute object.
-
#parent_object ⇒ Object
Returns the value of attribute parent_object.
-
#parent_type ⇒ Object
Returns the value of attribute parent_type.
-
#query_context ⇒ Object
Returns the value of attribute query_context.
Class Method Summary collapse
Instance Method Summary collapse
- #cache_config ⇒ Object
- #cache_key ⇒ Object
- #call(&block) ⇒ Object
- #id_from_object ⇒ Object
-
#initialize(parent_type, parent_object, field_definition, field_args, query_context) ⇒ Middleware
constructor
A new instance of Middleware.
- #metadata_hash ⇒ Object
- #object_key ⇒ Object
Constructor Details
#initialize(parent_type, parent_object, field_definition, field_args, query_context) ⇒ Middleware
Returns a new instance of Middleware.
14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/graphql/cache/middleware.rb', line 14 def initialize(parent_type, parent_object, field_definition, field_args, query_context) self.parent_type = parent_type self.parent_object = parent_object self.field_definition = field_definition self.field_args = field_args self.query_context = query_context self.object = parent_object.object if parent_object self.cache = ::GraphQL::Cache.cache end |
Instance Attribute Details
#cache ⇒ Object
Returns the value of attribute cache.
7 8 9 |
# File 'lib/graphql/cache/middleware.rb', line 7 def cache @cache end |
#field_args ⇒ Object
Returns the value of attribute field_args.
7 8 9 |
# File 'lib/graphql/cache/middleware.rb', line 7 def field_args @field_args end |
#field_definition ⇒ Object
Returns the value of attribute field_definition.
7 8 9 |
# File 'lib/graphql/cache/middleware.rb', line 7 def field_definition @field_definition end |
#object ⇒ Object
Returns the value of attribute object.
7 8 9 |
# File 'lib/graphql/cache/middleware.rb', line 7 def object @object end |
#parent_object ⇒ Object
Returns the value of attribute parent_object.
7 8 9 |
# File 'lib/graphql/cache/middleware.rb', line 7 def parent_object @parent_object end |
#parent_type ⇒ Object
Returns the value of attribute parent_type.
7 8 9 |
# File 'lib/graphql/cache/middleware.rb', line 7 def parent_type @parent_type end |
#query_context ⇒ Object
Returns the value of attribute query_context.
7 8 9 |
# File 'lib/graphql/cache/middleware.rb', line 7 def query_context @query_context end |
Class Method Details
.call(*args, &block) ⇒ Object
10 11 12 |
# File 'lib/graphql/cache/middleware.rb', line 10 def self.call(*args, &block) new(*args).call(&block) end |
Instance Method Details
#cache_config ⇒ Object
28 29 30 31 32 33 34 35 36 37 |
# File 'lib/graphql/cache/middleware.rb', line 28 def cache_config { parent_type: parent_type, parent_object: parent_object, field_definition: field_definition, field_args: field_args, query_context: query_context, object: object }.merge end |
#cache_key ⇒ Object
55 56 57 58 59 60 61 62 |
# File 'lib/graphql/cache/middleware.rb', line 55 def cache_key @cache_key ||= [ GraphQL::Cache.namespace, object_key, field_definition.name, field_args.keys ].flatten end |
#call(&block) ⇒ Object
47 48 49 50 51 52 53 |
# File 'lib/graphql/cache/middleware.rb', line 47 def call(&block) GraphQL::Cache.fetch( cache_key, config: cache_config, &block ) end |
#id_from_object ⇒ Object
70 71 72 73 74 |
# File 'lib/graphql/cache/middleware.rb', line 70 def id_from_object return object.id if object.respond_to? :id return object.fetch(:id, nil) if object.respond_to? :fetch return object.fetch('id', nil) if object.respond_to? :fetch end |
#metadata_hash ⇒ Object
39 40 41 42 43 44 45 |
# File 'lib/graphql/cache/middleware.rb', line 39 def { metadata: { cache: field_definition.[:cache] || {} } } end |
#object_key ⇒ Object
64 65 66 67 68 |
# File 'lib/graphql/cache/middleware.rb', line 64 def object_key return nil unless object "#{object.class.name}:#{id_from_object}" end |