Class: Graphiti::ResourceProxy
- Includes:
- Enumerable
- Defined in:
- lib/graphiti/resource_proxy.rb
Instance Attribute Summary collapse
-
#cache ⇒ Object
readonly
Returns the value of attribute cache.
-
#cache_expires_in ⇒ Object
readonly
Returns the value of attribute cache_expires_in.
-
#cache_tag ⇒ Object
readonly
Returns the value of attribute cache_tag.
-
#payload ⇒ Object
readonly
Returns the value of attribute payload.
-
#query ⇒ Object
readonly
Returns the value of attribute query.
-
#resource ⇒ Object
readonly
Returns the value of attribute resource.
-
#scope ⇒ Object
readonly
Returns the value of attribute scope.
Instance Method Summary collapse
- #[](val) ⇒ Object
- #as_graphql(options = {}) ⇒ Object
- #as_json(options = {}) ⇒ Object
- #cache? ⇒ Boolean (also: #cached?)
- #cache_key ⇒ Object
- #cache_key_with_version ⇒ Object
- #data ⇒ Object (also: #to_a, #resolve_data)
- #debug_requested? ⇒ Boolean
- #destroy ⇒ Object
- #each(&blk) ⇒ Object
- #errors ⇒ Object
- #etag ⇒ Object
- #extra_fields ⇒ Object
- #fields ⇒ Object
- #future_resolve_data ⇒ Object
- #include_hash ⇒ Object
-
#initialize(resource, scope, query, payload: nil, single: false, raise_on_missing: false, cache: nil, cache_expires_in: nil, cache_tag: nil) ⇒ ResourceProxy
constructor
A new instance of ResourceProxy.
- #jsonapi_render_options(opts = {}) ⇒ Object
- #meta ⇒ Object
- #pagination ⇒ Object
- #raise_on_missing? ⇒ Boolean
- #resource_cache_tag ⇒ Object
- #save(action: :create) ⇒ Object
- #single? ⇒ Boolean
- #stats ⇒ Object
- #to_graphql(options = {}) ⇒ Object
- #to_json(options = {}) ⇒ Object
- #to_jsonapi(options = {}) ⇒ Object
- #to_xml(options = {}) ⇒ Object
- #update ⇒ Object (also: #update_attributes)
- #updated_at ⇒ Object
Constructor Details
#initialize(resource, scope, query, payload: nil, single: false, raise_on_missing: false, cache: nil, cache_expires_in: nil, cache_tag: nil) ⇒ ResourceProxy
Returns a new instance of ResourceProxy.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# 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, cache_tag: 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 @cache_tag = cache_tag end |
Instance Attribute Details
#cache ⇒ Object (readonly)
Returns the value of attribute cache.
5 6 7 |
# File 'lib/graphiti/resource_proxy.rb', line 5 def cache @cache end |
#cache_expires_in ⇒ Object (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 |
#cache_tag ⇒ Object (readonly)
Returns the value of attribute cache_tag.
5 6 7 |
# File 'lib/graphiti/resource_proxy.rb', line 5 def cache_tag @cache_tag end |
#payload ⇒ Object (readonly)
Returns the value of attribute payload.
5 6 7 |
# File 'lib/graphiti/resource_proxy.rb', line 5 def payload @payload end |
#query ⇒ Object (readonly)
Returns the value of attribute query.
5 6 7 |
# File 'lib/graphiti/resource_proxy.rb', line 5 def query @query end |
#resource ⇒ Object (readonly)
Returns the value of attribute resource.
5 6 7 |
# File 'lib/graphiti/resource_proxy.rb', line 5 def resource @resource end |
#scope ⇒ Object (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
48 49 50 |
# File 'lib/graphiti/resource_proxy.rb', line 48 def [](val) data[val] end |
#as_graphql(options = {}) ⇒ Object
79 80 81 |
# File 'lib/graphiti/resource_proxy.rb', line 79 def as_graphql( = {}) Renderer.new(self, ).as_graphql end |
#as_json(options = {}) ⇒ Object
67 68 69 |
# File 'lib/graphiti/resource_proxy.rb', line 67 def as_json( = {}) Renderer.new(self, ).as_json end |
#cache? ⇒ Boolean Also known as: cached?
30 31 32 |
# File 'lib/graphiti/resource_proxy.rb', line 30 def cache? !!@cache end |
#cache_key ⇒ Object
222 223 224 225 226 227 228 229 230 |
# File 'lib/graphiti/resource_proxy.rb', line 222 def cache_key ActiveSupport::Cache.( [ @scope.cache_key, @query.cache_key, resource_cache_tag ].compact_blank ) end |
#cache_key_with_version ⇒ Object
232 233 234 235 236 237 238 239 240 |
# File 'lib/graphiti/resource_proxy.rb', line 232 def cache_key_with_version ActiveSupport::Cache.( [ @scope.cache_key_with_version, @query.cache_key, resource_cache_tag ].compact_blank ) end |
#data ⇒ Object Also known as: to_a, resolve_data
83 84 85 86 87 88 89 90 91 |
# File 'lib/graphiti/resource_proxy.rb', line 83 def data @data ||= begin records = @scope.resolve raise Graphiti::Errors::RecordNotFound if records.empty? && raise_on_missing? records = records[0] if single? records end end |
#debug_requested? ⇒ Boolean
204 205 206 |
# File 'lib/graphiti/resource_proxy.rb', line 204 def debug_requested? query.debug_requested? end |
#destroy ⇒ Object
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/graphiti/resource_proxy.rb', line 164 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
108 109 110 |
# File 'lib/graphiti/resource_proxy.rb', line 108 def each(&blk) to_a.each(&blk) end |
#errors ⇒ Object
44 45 46 |
# File 'lib/graphiti/resource_proxy.rb', line 44 def errors data.errors end |
#etag ⇒ Object
212 213 214 |
# File 'lib/graphiti/resource_proxy.rb', line 212 def etag "W/#{ActiveSupport::Digest.hexdigest(cache_key_with_version.to_s)}" end |
#extra_fields ⇒ Object
200 201 202 |
# File 'lib/graphiti/resource_proxy.rb', line 200 def extra_fields query.extra_fields end |
#fields ⇒ Object
196 197 198 |
# File 'lib/graphiti/resource_proxy.rb', line 196 def fields query.fields end |
#future_resolve_data ⇒ Object
95 96 97 98 99 100 101 102 |
# File 'lib/graphiti/resource_proxy.rb', line 95 def future_resolve_data @scope.future_resolve.then do |records| raise Graphiti::Errors::RecordNotFound if records.empty? && raise_on_missing? records = records[0] if single? @data = records end end |
#include_hash ⇒ Object
189 190 191 192 193 194 |
# File 'lib/graphiti/resource_proxy.rb', line 189 def include_hash @include_hash ||= begin base = @payload ? @payload.include_hash : {} base.deep_merge(@query.include_hash) end end |
#jsonapi_render_options(opts = {}) ⇒ Object
52 53 54 55 56 |
# File 'lib/graphiti/resource_proxy.rb', line 52 def (opts = {}) opts[:expose] ||= {} opts[:expose][:context] = Graphiti.context[:object] opts end |
#meta ⇒ Object
104 105 106 |
# File 'lib/graphiti/resource_proxy.rb', line 104 def @meta ||= data.respond_to?(:meta) ? data. : {} end |
#pagination ⇒ Object
130 131 132 |
# File 'lib/graphiti/resource_proxy.rb', line 130 def pagination @pagination ||= Delegates::Pagination.new(self) end |
#raise_on_missing? ⇒ Boolean
40 41 42 |
# File 'lib/graphiti/resource_proxy.rb', line 40 def raise_on_missing? !!@raise_on_missing end |
#resource_cache_tag ⇒ Object
216 217 218 219 220 |
# File 'lib/graphiti/resource_proxy.rb', line 216 def resource_cache_tag return unless @cache_tag.present? && @resource.respond_to?(@cache_tag) @resource.try(@cache_tag) end |
#save(action: :create) ⇒ Object
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/graphiti/resource_proxy.rb', line 134 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.(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
36 37 38 |
# File 'lib/graphiti/resource_proxy.rb', line 36 def single? !!@single end |
#stats ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/graphiti/resource_proxy.rb', line 112 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
75 76 77 |
# File 'lib/graphiti/resource_proxy.rb', line 75 def to_graphql( = {}) Renderer.new(self, ).to_graphql end |
#to_json(options = {}) ⇒ Object
63 64 65 |
# File 'lib/graphiti/resource_proxy.rb', line 63 def to_json( = {}) Renderer.new(self, ).to_json end |
#to_jsonapi(options = {}) ⇒ Object
58 59 60 61 |
# File 'lib/graphiti/resource_proxy.rb', line 58 def to_jsonapi( = {}) = () Renderer.new(self, ).to_jsonapi end |
#to_xml(options = {}) ⇒ Object
71 72 73 |
# File 'lib/graphiti/resource_proxy.rb', line 71 def to_xml( = {}) Renderer.new(self, ).to_xml end |
#update ⇒ Object Also known as: update_attributes
182 183 184 185 |
# File 'lib/graphiti/resource_proxy.rb', line 182 def update resolve_data save(action: :update) end |
#updated_at ⇒ Object
208 209 210 |
# File 'lib/graphiti/resource_proxy.rb', line 208 def updated_at @scope.updated_at end |