Class: GraphQL::Cache::Deconstructor

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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

#methodString

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.

Returns:

  • (String)

    'array' or 'collectionproxy' or 'relation'



22
23
24
# File 'lib/graphql/cache/deconstructor.rb', line 22

def method
  @method
end

#rawObject

The raw value to perform actions on. Could be a raw cached value, or a raw GraphQL Field.

Returns:

  • (Object)


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.

Returns:

  • (Object)

    A newly initialized GraphQL::Cache::Deconstructor instance



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

#performObject

Deconstructs a GraphQL field into a cachable value

Returns:

  • (Object)

    A value suitable for writing to cache



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