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.
-
#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
- #include_hash ⇒ Object
-
#initialize(resource, scope, query, payload: nil, single: false, raise_on_missing: false, cache: nil, cache_expires_in: nil) ⇒ ResourceProxy
constructor
A new instance of ResourceProxy.
- #jsonapi_render_options(opts = {}) ⇒ Object
- #meta ⇒ Object
- #pagination ⇒ Object
- #raise_on_missing? ⇒ Boolean
- #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) ⇒ 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
#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 |
#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
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( = {}) Renderer.new(self, ).as_graphql end |
#as_json(options = {}) ⇒ Object
61 62 63 |
# File 'lib/graphiti/resource_proxy.rb', line 61 def as_json( = {}) Renderer.new(self, ).as_json end |
#cache? ⇒ Boolean Also known as: cached?
24 25 26 |
# File 'lib/graphiti/resource_proxy.rb', line 24 def cache? !!@cache end |
#cache_key ⇒ Object
202 203 204 |
# File 'lib/graphiti/resource_proxy.rb', line 202 def cache_key ActiveSupport::Cache.([@scope.cache_key, @query.cache_key]) end |
#cache_key_with_version ⇒ Object
206 207 208 |
# File 'lib/graphiti/resource_proxy.rb', line 206 def cache_key_with_version ActiveSupport::Cache.([@scope.cache_key_with_version, @query.cache_key]) end |
#data ⇒ Object 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
190 191 192 |
# File 'lib/graphiti/resource_proxy.rb', line 190 def debug_requested? query.debug_requested? end |
#destroy ⇒ Object
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 |
#errors ⇒ Object
38 39 40 |
# File 'lib/graphiti/resource_proxy.rb', line 38 def errors data.errors end |
#etag ⇒ Object
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_fields ⇒ Object
186 187 188 |
# File 'lib/graphiti/resource_proxy.rb', line 186 def extra_fields query.extra_fields end |
#fields ⇒ Object
182 183 184 |
# File 'lib/graphiti/resource_proxy.rb', line 182 def fields query.fields end |
#include_hash ⇒ Object
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 (opts = {}) opts[:expose] ||= {} opts[:expose][:context] = Graphiti.context[:object] opts end |
#meta ⇒ Object
90 91 92 |
# File 'lib/graphiti/resource_proxy.rb', line 90 def @meta ||= data.respond_to?(:meta) ? data. : {} end |
#pagination ⇒ Object
116 117 118 |
# File 'lib/graphiti/resource_proxy.rb', line 116 def pagination @pagination ||= Delegates::Pagination.new(self) end |
#raise_on_missing? ⇒ 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.(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
30 31 32 |
# File 'lib/graphiti/resource_proxy.rb', line 30 def single? !!@single end |
#stats ⇒ Object
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( = {}) Renderer.new(self, ).to_graphql end |
#to_json(options = {}) ⇒ Object
57 58 59 |
# File 'lib/graphiti/resource_proxy.rb', line 57 def to_json( = {}) Renderer.new(self, ).to_json end |
#to_jsonapi(options = {}) ⇒ Object
52 53 54 55 |
# File 'lib/graphiti/resource_proxy.rb', line 52 def to_jsonapi( = {}) = () Renderer.new(self, ).to_jsonapi end |
#to_xml(options = {}) ⇒ Object
65 66 67 |
# File 'lib/graphiti/resource_proxy.rb', line 65 def to_xml( = {}) Renderer.new(self, ).to_xml end |
#update ⇒ Object 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_at ⇒ Object
194 195 196 |
# File 'lib/graphiti/resource_proxy.rb', line 194 def updated_at @scope.updated_at end |