graphql-fragment_cache 1.21.0 → 1.22.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -0
- data/README.md +28 -0
- data/lib/graphql/fragment_cache/cache_key_builder.rb +12 -2
- data/lib/graphql/fragment_cache/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c2f7385deb55dd824ed24ca60059bba372ad645bd2d1a183dee49089b2788648
|
4
|
+
data.tar.gz: 991e976fc7d2a6451e7362e6d899829511d4979544aaf2f01cff25b210782f47
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 07ac9a5d1ba73da5561a7c433499486986ccbc9b70c07ea2aaf22b4781d331cd16ca8e9e05005cea2031d5955a188cb5d96431d04439ae229ad97441d913627c
|
7
|
+
data.tar.gz: b9fb29e2a1d1a3bf24808b6186802d4c093764d11a5b80b07e6db8cb97c9454dbff6b878bd5c3767288ff551ee560573ce8ec3834fb01375a3a24aee312bad1c
|
data/CHANGELOG.md
CHANGED
@@ -2,6 +2,10 @@
|
|
2
2
|
|
3
3
|
## master
|
4
4
|
|
5
|
+
## 1.22.0 (2025-02-20)
|
6
|
+
|
7
|
+
- [PR#134](https://github.com/DmitryTsepelev/graphql-ruby-fragment_cache/pull/134) Add possibility to include and exclude arguments from generated cache key ([@mgruner][])
|
8
|
+
|
5
9
|
## 1.21.0 (2025-02-01)
|
6
10
|
|
7
11
|
- [PR#130](https://github.com/DmitryTsepelev/graphql-ruby-fragment_cache/pull/130) Dataloader support ([@DmitryTsepelev][])
|
@@ -217,3 +221,4 @@
|
|
217
221
|
[@noma4i]: https://github.com/noma4i
|
218
222
|
[@Drowze]: https://github.com/Drowze
|
219
223
|
[@danielhartnell]: https://github.com/danielhartnell
|
224
|
+
[@mgruner]: https://github.com/mgruner
|
data/README.md
CHANGED
@@ -147,6 +147,34 @@ class QueryType < BaseObject
|
|
147
147
|
end
|
148
148
|
```
|
149
149
|
|
150
|
+
### Query arguments processing
|
151
|
+
|
152
|
+
You can influence the way that graphql arguments are include in the cache key.
|
153
|
+
|
154
|
+
A use case might be a `:renew_cache` parameter that can be used to force a cache rewrite,
|
155
|
+
but should not be included with the cache key itself. Use `cache_key: { exclude_arguments: […]}`
|
156
|
+
to specify a list of arguments to be excluded from the implicit cache key.
|
157
|
+
|
158
|
+
```ruby
|
159
|
+
class QueryType < BaseObject
|
160
|
+
field :post, PostType, null: true do
|
161
|
+
argument :id, ID, required: true
|
162
|
+
argument :renew_cache, Boolean, required: false
|
163
|
+
end
|
164
|
+
|
165
|
+
def post(id:, renew_cache: false)
|
166
|
+
if renew_cache
|
167
|
+
context.scoped_set!(:renew_cache, true)
|
168
|
+
end
|
169
|
+
cache_fragment(cache_key: {exclude_arguments: [:renew_cache]}) { Post.find(id) }
|
170
|
+
end
|
171
|
+
end
|
172
|
+
```
|
173
|
+
|
174
|
+
Likewise, you can use `cache_key: { include_arguments: […] }` to specify an allowlist of arguments
|
175
|
+
to be included in the cache key. In this case all arguments for the cache key must be specified, including
|
176
|
+
parent arguments of nested fields.
|
177
|
+
|
150
178
|
### User-provided cache key (custom key)
|
151
179
|
|
152
180
|
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):
|
@@ -130,16 +130,26 @@ module GraphQL
|
|
130
130
|
|
131
131
|
next lookahead.field.name if lookahead.arguments.empty?
|
132
132
|
|
133
|
-
args = lookahead.arguments.map { "#{_1}:#{traverse_argument(_2)}" }.sort.join(",")
|
133
|
+
args = lookahead.arguments.select { include_argument?(_1) }.map { "#{_1}:#{traverse_argument(_2)}" }.sort.join(",")
|
134
134
|
"#{lookahead.field.name}(#{args})"
|
135
135
|
}.join("/")
|
136
136
|
end
|
137
137
|
end
|
138
138
|
|
139
|
+
def include_argument?(argument_name)
|
140
|
+
exclude_arguments = @options.dig(:cache_key, :exclude_arguments)
|
141
|
+
return false if exclude_arguments&.include?(argument_name)
|
142
|
+
|
143
|
+
include_arguments = @options.dig(:cache_key, :include_arguments)
|
144
|
+
return false if include_arguments && !include_arguments.include?(argument_name)
|
145
|
+
|
146
|
+
true
|
147
|
+
end
|
148
|
+
|
139
149
|
def traverse_argument(argument)
|
140
150
|
return argument unless argument.is_a?(GraphQL::Schema::InputObject)
|
141
151
|
|
142
|
-
"{#{argument.map { "#{_1}:#{traverse_argument(_2)}" }.sort.join(",")}}"
|
152
|
+
"{#{argument.map { include_argument?(_1) ? "#{_1}:#{traverse_argument(_2)}" : nil }.compact.sort.join(",")}}"
|
143
153
|
end
|
144
154
|
|
145
155
|
def object_cache_key
|
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
|
+
version: 1.22.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- DmitryTsepelev
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2025-02-
|
11
|
+
date: 2025-02-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: graphql
|