Class: GraphQL::Cache::Builder
- Inherits:
-
Object
- Object
- GraphQL::Cache::Builder
- Defined in:
- lib/graphql/cache/builder.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 both deconstructing an object to be stored in cache and re-hydrating a GraphQL object from raw cache values
Instance Attribute Summary collapse
-
#config ⇒ Hash
The middleware config hash describing a field's resolution.
-
#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
methodstring based onraw.class.name. -
.namify(str) ⇒ Object
Ruby-only means of "demodularizing" a string.
Instance Method Summary collapse
-
#build(config) ⇒ Object
Builds a compitable GraphQL object based on the resolution config.
-
#deconstruct ⇒ Object
Deconstructs a GraphQL field into a cachable value.
-
#initialize(raw, method) ⇒ Builder
constructor
A new instance of Builder.
Constructor Details
#initialize(raw, method) ⇒ Builder
Returns a new instance of Builder.
45 46 47 48 |
# File 'lib/graphql/cache/builder.rb', line 45 def initialize(raw, method) self.raw = raw self.method = method end |
Instance Attribute Details
#config ⇒ Hash
The middleware config hash describing a field's resolution
29 30 31 |
# File 'lib/graphql/cache/builder.rb', line 29 def config @config end |
#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.
23 24 25 |
# File 'lib/graphql/cache/builder.rb', line 23 def method @method end |
#raw ⇒ Object
The raw value to perform actions on. Could be a raw cached value, or a raw GraphQL Field.
14 15 16 |
# File 'lib/graphql/cache/builder.rb', line 14 def raw @raw end |
Class Method Details
.[](raw) ⇒ Object
Initializer helper that generates a valid method string based
on raw.class.name.
35 36 37 38 |
# File 'lib/graphql/cache/builder.rb', line 35 def self.[](raw) build_method = namify(raw.class.name) new(raw, build_method) end |
.namify(str) ⇒ Object
Ruby-only means of "demodularizing" a string
41 42 43 |
# File 'lib/graphql/cache/builder.rb', line 41 def self.namify(str) str.split('::').last.downcase end |
Instance Method Details
#build(config) ⇒ Object
Builds a compitable GraphQL object based on the resolution config
53 54 55 56 57 58 59 |
# File 'lib/graphql/cache/builder.rb', line 53 def build(config) self.config = config return build_array if method == 'array' return build_relation if method == 'collectionproxy' || method == 'relation' build_object end |
#deconstruct ⇒ Object
Deconstructs a GraphQL field into a cachable value
64 65 66 67 68 69 |
# File 'lib/graphql/cache/builder.rb', line 64 def deconstruct return deconstruct_array(raw) if raw.class == Array return raw.nodes if raw.class.ancestors.include? GraphQL::Relay::BaseConnection deconstruct_object(raw) end |