Class: Graphiti::ResourceProxy

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/graphiti/resource_proxy.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource, scope, query, payload: nil, single: false, raise_on_missing: false) ⇒ ResourceProxy

Returns a new instance of ResourceProxy.



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/graphiti/resource_proxy.rb', line 7

def initialize(resource, scope, query,
  payload: nil,
  single: false,
  raise_on_missing: false)
  @resource = resource
  @scope = scope
  @query = query
  @payload = payload
  @single = single
  @raise_on_missing = raise_on_missing
end

Instance Attribute Details

#payloadObject (readonly)

Returns the value of attribute payload.



5
6
7
# File 'lib/graphiti/resource_proxy.rb', line 5

def payload
  @payload
end

#queryObject (readonly)

Returns the value of attribute query.



5
6
7
# File 'lib/graphiti/resource_proxy.rb', line 5

def query
  @query
end

#resourceObject (readonly)

Returns the value of attribute resource.



5
6
7
# File 'lib/graphiti/resource_proxy.rb', line 5

def resource
  @resource
end

#scopeObject (readonly)

Returns the value of attribute scope.



5
6
7
# File 'lib/graphiti/resource_proxy.rb', line 5

def scope
  @scope
end

Instance Method Details

#[](val) ⇒ Object



31
32
33
# File 'lib/graphiti/resource_proxy.rb', line 31

def [](val)
  data[val]
end

#as_graphql(options = {}) ⇒ Object



62
63
64
# File 'lib/graphiti/resource_proxy.rb', line 62

def as_graphql(options = {})
  Renderer.new(self, options).as_graphql
end

#as_json(options = {}) ⇒ Object



50
51
52
# File 'lib/graphiti/resource_proxy.rb', line 50

def as_json(options = {})
  Renderer.new(self, options).as_json
end

#dataObject Also known as: to_a



66
67
68
69
70
71
72
73
74
75
# File 'lib/graphiti/resource_proxy.rb', line 66

def data
  @data ||= begin
    records = @scope.resolve
    if records.empty? && raise_on_missing?
      raise Graphiti::Errors::RecordNotFound
    end
    records = records[0] if single?
    records
  end
end

#debug_requested?Boolean

Returns:

  • (Boolean)


178
179
180
# File 'lib/graphiti/resource_proxy.rb', line 178

def debug_requested?
  query.debug_requested?
end

#destroyObject



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/graphiti/resource_proxy.rb', line 138

def destroy
  data
  transaction_response = @resource.transaction do
     = {method: :destroy}
    model = @resource.destroy(@query.filters[:id], )
    model.instance_variable_set(:@__serializer_klass, @resource.serializer)
    @resource.after_graph_persist(model, )
    validator = ::Graphiti::Util::ValidationResponse.new \
      model, @payload
    validator.validate!
    @resource.before_commit(model, )

    {result: validator}
  end
  @data, success = transaction_response[:result].to_a
  success
end

#each(&blk) ⇒ Object



82
83
84
# File 'lib/graphiti/resource_proxy.rb', line 82

def each(&blk)
  to_a.each(&blk)
end

#errorsObject



27
28
29
# File 'lib/graphiti/resource_proxy.rb', line 27

def errors
  data.errors
end

#extra_fieldsObject



174
175
176
# File 'lib/graphiti/resource_proxy.rb', line 174

def extra_fields
  query.extra_fields
end

#fieldsObject



170
171
172
# File 'lib/graphiti/resource_proxy.rb', line 170

def fields
  query.fields
end

#include_hashObject



163
164
165
166
167
168
# File 'lib/graphiti/resource_proxy.rb', line 163

def include_hash
  @include_hash ||= begin
    base = @payload ? @payload.include_hash : {}
    base.deep_merge(@query.include_hash)
  end
end

#jsonapi_render_options(opts = {}) ⇒ Object



35
36
37
38
39
# File 'lib/graphiti/resource_proxy.rb', line 35

def jsonapi_render_options(opts = {})
  opts[:expose] ||= {}
  opts[:expose][:context] = Graphiti.context[:object]
  opts
end

#metaObject



78
79
80
# File 'lib/graphiti/resource_proxy.rb', line 78

def meta
  @meta ||= data.respond_to?(:meta) ? data.meta : {}
end

#paginationObject



104
105
106
# File 'lib/graphiti/resource_proxy.rb', line 104

def pagination
  @pagination ||= Delegates::Pagination.new(self)
end

#raise_on_missing?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/graphiti/resource_proxy.rb', line 23

def raise_on_missing?
  !!@raise_on_missing
end

#save(action: :create) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/graphiti/resource_proxy.rb', line 108

def save(action: :create)
  # TODO: remove this. Only used for persisting many-to-many with AR
  # (see activerecord adapter)
  original = Graphiti.context[:namespace]
  begin
    Graphiti.context[:namespace] = action
    ::Graphiti::RequestValidator.new(@resource, @payload.params, action).validate!
    validator = persist {
      @resource.persist_with_relationships \
        @payload.meta(action: action),
        @payload.attributes,
        @payload.relationships
    }
  ensure
    Graphiti.context[:namespace] = original
  end
  @data, success = validator.to_a

  if success
    # If the context namespace is `update` or `create`, certain
    # adapters will cause N+1 validation calls, so lets explicitly
    # switch to a lookup context.
    Graphiti.with_context(Graphiti.context[:object], :show) do
      @scope.resolve_sideloads([@data])
    end
  end

  success
end

#single?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/graphiti/resource_proxy.rb', line 19

def single?
  !!@single
end

#statsObject



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/graphiti/resource_proxy.rb', line 86

def stats
  @stats ||= if @query.hash[:stats]
    scope = @scope.unpaginated_object
    if resource.adapter.can_group?
      if (group = @query.hash[:stats].delete(:group_by))
        scope = resource.adapter.group(scope, group[0])
      end
    end
    payload = Stats::Payload.new @resource,
      @query,
      scope,
      data
    payload.generate
  else
    {}
  end
end

#to_graphql(options = {}) ⇒ Object



58
59
60
# File 'lib/graphiti/resource_proxy.rb', line 58

def to_graphql(options = {})
  Renderer.new(self, options).to_graphql
end

#to_json(options = {}) ⇒ Object



46
47
48
# File 'lib/graphiti/resource_proxy.rb', line 46

def to_json(options = {})
  Renderer.new(self, options).to_json
end

#to_jsonapi(options = {}) ⇒ Object



41
42
43
44
# File 'lib/graphiti/resource_proxy.rb', line 41

def to_jsonapi(options = {})
  options = jsonapi_render_options(options)
  Renderer.new(self, options).to_jsonapi
end

#to_xml(options = {}) ⇒ Object



54
55
56
# File 'lib/graphiti/resource_proxy.rb', line 54

def to_xml(options = {})
  Renderer.new(self, options).to_xml
end

#updateObject Also known as: update_attributes



156
157
158
159
# File 'lib/graphiti/resource_proxy.rb', line 156

def update
  data
  save(action: :update)
end