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, cache: nil, cache_expires_in: nil) ⇒ ResourceProxy

Returns a new instance of ResourceProxy.



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

def initialize(resource, scope, query,
  payload: nil,
  single: false,
  raise_on_missing: false,
  cache: nil,
  cache_expires_in: nil)

  @resource = resource
  @scope = scope
  @query = query
  @payload = payload
  @single = single
  @raise_on_missing = raise_on_missing
  @cache = cache
  @cache_expires_in = cache_expires_in
end

Instance Attribute Details

#cacheObject (readonly)

Returns the value of attribute cache.



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

def cache
  @cache
end

#cache_expires_inObject (readonly)

Returns the value of attribute cache_expires_in.



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

def cache_expires_in
  @cache_expires_in
end

#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



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

def [](val)
  data[val]
end

#as_graphql(options = {}) ⇒ Object



73
74
75
# File 'lib/graphiti/resource_proxy.rb', line 73

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

#as_json(options = {}) ⇒ Object



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

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

#cache?Boolean Also known as: cached?

Returns:

  • (Boolean)


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

def cache?
  !!@cache
end

#cache_keyObject



202
203
204
# File 'lib/graphiti/resource_proxy.rb', line 202

def cache_key
  ActiveSupport::Cache.expand_cache_key([@scope.cache_key, @query.cache_key])
end

#cache_key_with_versionObject



206
207
208
# File 'lib/graphiti/resource_proxy.rb', line 206

def cache_key_with_version
  ActiveSupport::Cache.expand_cache_key([@scope.cache_key_with_version, @query.cache_key])
end

#dataObject Also known as: to_a, resolve_data



77
78
79
80
81
82
83
84
85
86
# File 'lib/graphiti/resource_proxy.rb', line 77

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)


190
191
192
# File 'lib/graphiti/resource_proxy.rb', line 190

def debug_requested?
  query.debug_requested?
end

#destroyObject



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/graphiti/resource_proxy.rb', line 150

def destroy
  resolve_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



94
95
96
# File 'lib/graphiti/resource_proxy.rb', line 94

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

#errorsObject



38
39
40
# File 'lib/graphiti/resource_proxy.rb', line 38

def errors
  data.errors
end

#etagObject



198
199
200
# File 'lib/graphiti/resource_proxy.rb', line 198

def etag
  "W/#{ActiveSupport::Digest.hexdigest(cache_key_with_version.to_s)}"
end

#extra_fieldsObject



186
187
188
# File 'lib/graphiti/resource_proxy.rb', line 186

def extra_fields
  query.extra_fields
end

#fieldsObject



182
183
184
# File 'lib/graphiti/resource_proxy.rb', line 182

def fields
  query.fields
end

#include_hashObject



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

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

#jsonapi_render_options(opts = {}) ⇒ Object



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

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

#metaObject



90
91
92
# File 'lib/graphiti/resource_proxy.rb', line 90

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

#paginationObject



116
117
118
# File 'lib/graphiti/resource_proxy.rb', line 116

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

#raise_on_missing?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/graphiti/resource_proxy.rb', line 34

def raise_on_missing?
  !!@raise_on_missing
end

#save(action: :create) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/graphiti/resource_proxy.rb', line 120

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)


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

def single?
  !!@single
end

#statsObject



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/graphiti/resource_proxy.rb', line 98

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



69
70
71
# File 'lib/graphiti/resource_proxy.rb', line 69

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

#to_json(options = {}) ⇒ Object



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

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

#to_jsonapi(options = {}) ⇒ Object



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

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

#to_xml(options = {}) ⇒ Object



65
66
67
# File 'lib/graphiti/resource_proxy.rb', line 65

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

#updateObject Also known as: update_attributes



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

def update
  resolve_data
  save(action: :update)
end

#updated_atObject



194
195
196
# File 'lib/graphiti/resource_proxy.rb', line 194

def updated_at
  @scope.updated_at
end