class GraphQL::Cache::Deconstructor
GraphQL
objects can't be serialized to cache so we have to maintain an abstraction between the raw cache value and the GraphQL
expected object. This class exposes methods for deconstructing an object to be stored in cache
Attributes
A flag indicating the type of object construction to use when building a new GraphQL
object. Can be one of 'array', 'collectionproxy', 'relation'. These values have been chosen because it is easy to use the class names of the possible object types for this purpose.
@return [String] 'array' or 'collectionproxy' or 'relation'
Public Class Methods
Initializer helper that generates a valid `method` string based on `raw.class.name`.
@return [Object] A newly initialized GraphQL::Cache::Deconstructor
instance
# File lib/graphql/cache/deconstructor.rb, line 28 def self.[](raw) build_method = namify(raw.class.name) new(raw, build_method) end
Ruby-only means of “demodularizing” a string
# File lib/graphql/cache/deconstructor.rb, line 34 def self.namify(str) str.split('::').last.downcase end
# File lib/graphql/cache/deconstructor.rb, line 38 def initialize(raw, method) self.raw = raw self.method = method end
Public Instance Methods
@private
# File lib/graphql/cache/deconstructor.rb, line 57 def deconstruct_array(raw) return [] if raw.empty? if raw.first.class.ancestors.include? GraphQL::Schema::Object raw.map(&:object) else raw end end
@private
# File lib/graphql/cache/deconstructor.rb, line 68 def deconstruct_object(raw) if raw.respond_to?(:object) raw.object else raw end end
Deconstructs a GraphQL
field into a cachable value
@return [Object] A value suitable for writing to cache
# File lib/graphql/cache/deconstructor.rb, line 46 def perform if %(array collectionproxy).include? method deconstruct_array(raw) elsif raw.class.ancestors.include? GraphQL::Relay::BaseConnection raw.nodes else deconstruct_object(raw) end end