Class: GraphQL::Cache::Deconstructor
- Inherits:
-
Object
- Object
- GraphQL::Cache::Deconstructor
- Defined in:
- lib/graphql/cache/deconstructor.rb
Overview
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
Instance Attribute Summary collapse
-
#method ⇒ String
A flag indicating the type of object construction to use when building a new GraphQL object.
-
#raw ⇒ Object
The raw value to perform actions on.
Class Method Summary collapse
-
.[](raw) ⇒ Object
Initializer helper that generates a valid
method
string based onraw.class.name
. -
.namify(str) ⇒ Object
Ruby-only means of "demodularizing" a string.
Instance Method Summary collapse
-
#initialize(raw, method) ⇒ Deconstructor
constructor
A new instance of Deconstructor.
-
#perform ⇒ Object
Deconstructs a GraphQL field into a cachable value.
Constructor Details
#initialize(raw, method) ⇒ Deconstructor
Returns a new instance of Deconstructor.
38 39 40 41 |
# File 'lib/graphql/cache/deconstructor.rb', line 38 def initialize(raw, method) self.raw = raw self.method = method end |
Instance Attribute Details
#method ⇒ String
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.
22 23 24 |
# File 'lib/graphql/cache/deconstructor.rb', line 22 def method @method end |
#raw ⇒ Object
The raw value to perform actions on. Could be a raw cached value, or a raw GraphQL Field.
13 14 15 |
# File 'lib/graphql/cache/deconstructor.rb', line 13 def raw @raw end |
Class Method Details
.[](raw) ⇒ Object
Initializer helper that generates a valid method
string based
on raw.class.name
.
28 29 30 31 |
# File 'lib/graphql/cache/deconstructor.rb', line 28 def self.[](raw) build_method = namify(raw.class.name) new(raw, build_method) end |
.namify(str) ⇒ Object
Ruby-only means of "demodularizing" a string
34 35 36 |
# File 'lib/graphql/cache/deconstructor.rb', line 34 def self.namify(str) str.split('::').last.downcase end |
Instance Method Details
#perform ⇒ Object
Deconstructs a GraphQL field into a cachable value
46 47 48 49 50 51 52 53 54 |
# 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 |