Class: GraphQL::Cache::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/graphql/cache/builder.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw, method) ⇒ Builder

Returns a new instance of Builder.



15
16
17
18
# File 'lib/graphql/cache/builder.rb', line 15

def initialize(raw, method)
  self.raw    = raw
  self.method = method
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



4
5
6
# File 'lib/graphql/cache/builder.rb', line 4

def config
  @config
end

#methodObject

Returns the value of attribute method.



4
5
6
# File 'lib/graphql/cache/builder.rb', line 4

def method
  @method
end

#rawObject

Returns the value of attribute raw.



4
5
6
# File 'lib/graphql/cache/builder.rb', line 4

def raw
  @raw
end

Class Method Details

.[](raw) ⇒ Object



6
7
8
9
# File 'lib/graphql/cache/builder.rb', line 6

def self.[](raw)
  build_method = namify(raw.class.name)
  new(raw, build_method)
end

.namify(str) ⇒ Object



11
12
13
# File 'lib/graphql/cache/builder.rb', line 11

def self.namify(str)
  str.split('::').last.downcase
end

Instance Method Details

#build(config) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/graphql/cache/builder.rb', line 20

def build(config)
  self.config = config

  return build_array    if method == 'array'
  return build_relation if method == 'collectionproxy' || method == 'relation'
  build_object
end

#build_arrayObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/graphql/cache/builder.rb', line 67

def build_array
  gql_def = config[:field_definition].type.unwrap.graphql_definition

  raw.map do |item|
    if gql_def.kind.name == 'OBJECT'
      config[:field_definition].type.unwrap.graphql_definition.[:type_class].new(
        item,
        config[:query_context]
      )
    else
      item
    end
  end
end

#build_objectObject



82
83
84
85
86
87
88
89
90
91
92
# File 'lib/graphql/cache/builder.rb', line 82

def build_object
  klass = config[:field_definition].type.unwrap.graphql_definition.[:type_class]
  if klass
    klass.new(
      raw,
      config[:query_context]
    )
  else
    raw
  end
end

#build_relationObject



57
58
59
60
61
62
63
64
65
# File 'lib/graphql/cache/builder.rb', line 57

def build_relation
  GraphQL::Relay::RelationConnection.new(
    raw,
    config[:field_args],
    field:   config[:query_context].field,
    parent:  config[:parent_object].object,
    context: config[:query_context]
  )
end

#deconstructObject



28
29
30
31
32
33
34
35
36
37
# File 'lib/graphql/cache/builder.rb', line 28

def deconstruct
  case self.class.namify(raw.class.name)
  when 'array'
    deconstruct_array(raw)
  when 'relationconnection'
    raw.nodes
  else
    deconstruct_object(raw)
  end
end

#deconstruct_array(raw) ⇒ Object



47
48
49
50
51
52
53
54
55
# File 'lib/graphql/cache/builder.rb', line 47

def deconstruct_array(raw)
  return [] if raw.empty?

  if raw.first.class.ancestors.include? GraphQL::Schema::Object
    raw.map(&:object)
  else
    raw
  end
end

#deconstruct_object(raw) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/graphql/cache/builder.rb', line 39

def deconstruct_object(raw)
  if raw.respond_to?(:object)
    raw.object
  else
    raw
  end
end