Class: GraphQL::Cache::Builder

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

Class Method Summary collapse

Instance Method Summary collapse

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

#configHash

The middleware config hash describing a field's resolution

Returns:

  • (Hash)

See Also:



29
30
31
# File 'lib/graphql/cache/builder.rb', line 29

def config
  @config
end

#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'



23
24
25
# File 'lib/graphql/cache/builder.rb', line 23

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)


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.

Returns:

  • (Object)

    A newly initialized GraphQL::Cache::Builder instance



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

Returns:

  • (Object)

    An object suitable for returning from a GraphQL middlware call



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

#deconstructObject

Deconstructs a GraphQL field into a cachable value

Returns:

  • (Object)

    A value suitable for writing to cache



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