graphql-fragment_cache 1.4.1 → 1.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 89da5923396f256876f648b15e23b77b14a731b4e5b642fc67074d50fcefc5c3
4
- data.tar.gz: b4c5a2fe21477726e41835168a8768e8adba9d03d3bf3f618705a31e0ee391a1
3
+ metadata.gz: fc02c6847d8e55983814bba96e0bd303893a1639648530048f0ccf6ab1527eab
4
+ data.tar.gz: 52530f6e108ed656077a02a78a44dd2cf498e4004b9d27897572f6f77aaec353
5
5
  SHA512:
6
- metadata.gz: 470dc39abc207c3d3db874d1a954d665fadf105c31b3e62ef18bfe0d60ca002e505e7861a52ee2ba652a4789f2c3566c2b7e2134fe22d09ef62b6fe17cf95a7d
7
- data.tar.gz: ceb3ec4abdf657527aac1e2ca5ab441bb3551a33aefe2fd38c2e1f6cb31e9bbea52b19c9aa30edf10a9e0071593697d4c6cd7f3b9254ad3682a995796375abc4
6
+ metadata.gz: e5b95e2b3f68011a8cdc0fc23b3a5f117f0ce9c711efcfb504230f6a84fb8c6c53a156d6671f97c8ea52cad199de597954badb2e456d33303b80e6e2e2105bec
7
+ data.tar.gz: d7275ef053d1127a17476244bd7253cb98dab81df865195b7bae7241bcd6106564af13848dfeaf39676acb96e962be73fb7782274ae4ccce2190065234b5fbf9
data/CHANGELOG.md CHANGED
@@ -2,6 +2,10 @@
2
2
 
3
3
  ## master
4
4
 
5
+ ## 1.5.0 (2021-02-20)
6
+
7
+ - [PR#50](https://github.com/DmitryTsepelev/graphql-ruby-fragment_cache/pull/50) Add object_cache_key to CacheKeyBuilder ([@bbugh][])
8
+
5
9
  ## 1.4.1 (2021-01-21)
6
10
 
7
11
  - [PR#48](https://github.com/DmitryTsepelev/graphql-ruby-fragment_cache/pull/48) Support graphql-ruby 1.12 ([@DmitryTsepelev][])
data/README.md CHANGED
@@ -112,10 +112,10 @@ selections_cache_key = "[#{%w[id name].join(".")}]"
112
112
 
113
113
  query_cache_key = Digest::SHA1.hexdigest("#{path_cache_key}#{selections_cache_key}")
114
114
 
115
- cache_key = "#{schema_cache_key}/#{query_cache_key}"
115
+ cache_key = "#{schema_cache_key}/#{query_cache_key}/#{object_cache_key}"
116
116
  ```
117
117
 
118
- You can override `schema_cache_key`, `query_cache_key` or `path_cache_key` by passing parameters to the `cache_fragment` calls:
118
+ You can override `schema_cache_key`, `query_cache_key`, `path_cache_key` or `object_cache_key` by passing parameters to the `cache_fragment` calls:
119
119
 
120
120
  ```ruby
121
121
  class QueryType < BaseObject
@@ -140,6 +140,21 @@ class PostType < BaseObject
140
140
  end
141
141
  ```
142
142
 
143
+ Overriding `object_cache_key` is helpful in the case where the value that is cached is different than the one used as a key, like a database query that is pre-processed before caching.
144
+
145
+ ```ruby
146
+ class QueryType < BaseObject
147
+ field :post, PostType, null: true do
148
+ argument :id, ID, required: true
149
+ end
150
+
151
+ def post(id:)
152
+ query = Post.where("updated_at < ?", Time.now - 1.day)
153
+ cache_fragment(object_cache_key: query.cache_key) { query.some_process }
154
+ end
155
+ end
156
+ ```
157
+
143
158
  ### User-provided cache key
144
159
 
145
160
  In most cases you want your cache key to depend on the resolved object (say, `ActiveRecord` model). You can do that by passing an argument to the `#cache_fragment` method in a similar way to Rails views [`#cache` method](https://guides.rubyonrails.org/caching_with_rails.html#fragment-caching):
@@ -198,6 +213,7 @@ end
198
213
 
199
214
  The way cache key part is generated for the passed argument is the following:
200
215
 
216
+ - Use `object_cache_key: "some_cache_key"` if passed to `cache_fragment`
201
217
  - Use `#graphql_cache_key` if implemented.
202
218
  - Use `#cache_key` (or `#cache_key_with_version` for modern Rails) if implemented.
203
219
  - Use `self.to_s` for _primitive_ types (strings, symbols, numbers, booleans).
@@ -113,8 +113,13 @@ module GraphQL
113
113
 
114
114
  def build
115
115
  Digest::SHA1.hexdigest("#{schema_cache_key}/#{query_cache_key}").then do |base_key|
116
- next base_key unless object
117
- "#{base_key}/#{object_key(object)}"
116
+ if @options[:object_cache_key]
117
+ "#{base_key}/#{@options[:object_cache_key]}"
118
+ elsif object
119
+ "#{base_key}/#{object_key(object)}"
120
+ else
121
+ base_key
122
+ end
118
123
  end
119
124
  end
120
125
 
@@ -113,8 +113,13 @@ module GraphQL
113
113
 
114
114
  def build
115
115
  Digest::SHA1.hexdigest("#{schema_cache_key}/#{query_cache_key}").then do |base_key|
116
- next base_key unless object
117
- "#{base_key}/#{object_key(object)}"
116
+ if @options[:object_cache_key]
117
+ "#{base_key}/#{@options[:object_cache_key]}"
118
+ elsif object
119
+ "#{base_key}/#{object_key(object)}"
120
+ else
121
+ base_key
122
+ end
118
123
  end
119
124
  end
120
125
 
@@ -2,6 +2,6 @@
2
2
 
3
3
  module GraphQL
4
4
  module FragmentCache
5
- VERSION = "1.4.1"
5
+ VERSION = "1.5.0"
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: graphql-fragment_cache
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.1
4
+ version: 1.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - DmitryTsepelev
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-01-21 00:00:00.000000000 Z
11
+ date: 2021-02-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: graphql