graphql-fragment_cache 1.0.5 → 1.1.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: 12ad57aeb345c05f3025f6cc232ec06b16afc603d4330451682f526c6516db71
4
- data.tar.gz: e486c3090b73fbe3f372cf1a7557c9df9753c3ddcddd720d97d02f3294625dbe
3
+ metadata.gz: b8dcfa21629a163b02f1097275602c294eb6e6cad5be237a11f5567c3fedce8c
4
+ data.tar.gz: 812fde3ac50fcb43ebb5e00c8291817c876a9bb0c7ccaa5544319dd01fb7a143
5
5
  SHA512:
6
- metadata.gz: d4a05c0bdc3aae4783cf6d0417feb70e3b56fe31f1c80a16fcf8b6cb9bf6da21e882be7a3458defc8d28fe8797f204852193356fe951891b3861bae88cbaed47
7
- data.tar.gz: cdd254f8c323d82c63a9c15bd4c767ca96feee841c4a1f7f5a9059eb0fc166ef52878a68e2170239dd8e0d3b152646d111800ad08beedadeb4080fc2f542a694
6
+ metadata.gz: da34c5a7e12669352a0a095338bd52895414a49628d758cf0df6d698fc342f6b94032fdce6bf8df70b866ea6ddf2a5d6666ab7dd7b70050ab0b058448b8dcfca
7
+ data.tar.gz: 937fa344650d90187acc5985c86b4f3320ed276a03aaf02531484596e1a0b20b09cc2fdb2d4596c8150d3992fdef9c48085623ff1030e16135da7f019b255e46
@@ -2,6 +2,10 @@
2
2
 
3
3
  ## master
4
4
 
5
+ ## 1.1.0 (2020-10-26)
6
+
7
+ - [PR#38](https://github.com/DmitryTsepelev/graphql-ruby-fragment_cache/pull/38) Support caching from other places than field or resolver ([@DmitryTsepelev][])
8
+
5
9
  ## 1.0.5 (2020-10-13)
6
10
 
7
11
  - [PR#35](https://github.com/DmitryTsepelev/graphql-ruby-fragment_cache/pull/35) Prefer using `#write_multi` on cache store when possible ([@DmitryTsepelev][])
data/README.md CHANGED
@@ -38,6 +38,14 @@ class BaseType < GraphQL::Schema::Object
38
38
  end
39
39
  ```
40
40
 
41
+ If you're using [resolvers](https://graphql-ruby.org/fields/resolvers.html) — include the module into the base resolver as well:
42
+
43
+ ```ruby
44
+ class Resolvers::BaseResolver < GraphQL::Schema::Resolver
45
+ include GraphQL::FragmentCache::ObjectHelpers
46
+ end
47
+ ```
48
+
41
49
  Now you can add `cache_fragment:` option to your fields to turn caching on:
42
50
 
43
51
  ```ruby
@@ -47,7 +55,7 @@ class PostType < BaseObject
47
55
  end
48
56
  ```
49
57
 
50
- Alternatively, you can use `cache_fragment` method inside resolvers:
58
+ Alternatively, you can use `cache_fragment` method inside resolver methods:
51
59
 
52
60
  ```ruby
53
61
  class QueryType < BaseObject
@@ -275,6 +283,35 @@ class QueryType < BaseObject
275
283
  end
276
284
  ```
277
285
 
286
+ ## How to use `#cache_fragment` in extensions (and other places where context is not available)
287
+
288
+ If you want to call `#cache_fragment` from places other that fields or resolvers, you'll need to pass `context` explicitly and turn on `raw_value` support. For instance, let's take a look at this extension:
289
+
290
+ ```ruby
291
+ class Types::QueryType < Types::BaseObject
292
+ class CurrentMomentExtension < GraphQL::Schema::FieldExtension
293
+ # turning on cache_fragment support
294
+ include GraphQL::FragmentCache::ObjectHelpers
295
+
296
+ def resolve(object:, arguments:, context:)
297
+ # context is passed explicitly
298
+ cache_fragment(context: context) do
299
+ result = yield(object, arguments)
300
+ "#{result} (at #{Time.now})"
301
+ end
302
+ end
303
+ end
304
+
305
+ field :event, String, null: false, extensions: [CurrentMomentExtension]
306
+
307
+ def event
308
+ "something happened"
309
+ end
310
+ end
311
+ ```
312
+
313
+ With this approach you can use `#cache_fragment` in any place you have an access to the `context`. When context is not available, the error `cannot find context, please pass it explicitly` will be thrown.
314
+
278
315
  ## Limitations
279
316
 
280
317
  Caching does not work for Union types, because of the `Lookahead` implementation: it requires the exact type to be passed to the `selection` method (you can find the [discussion](https://github.com/rmosolgo/graphql-ruby/pull/3007) here). This method is used for cache key building, and I haven't found a workaround yet ([PR in progress](https://github.com/DmitryTsepelev/graphql-ruby-fragment_cache/pull/30)). If you get `Failed to look ahead the field` error — please pass `query_cache_key` explicitly:
@@ -10,6 +10,12 @@ module GraphQL
10
10
  module ObjectHelpers
11
11
  extend Forwardable
12
12
 
13
+ def self.included(base)
14
+ return if base < GraphQL::Execution::Interpreter::HandlesRawValue
15
+
16
+ base.include(GraphQL::Execution::Interpreter::HandlesRawValue)
17
+ end
18
+
13
19
  NO_OBJECT = Object.new
14
20
 
15
21
  def cache_fragment(object_to_cache = NO_OBJECT, **options, &block)
@@ -17,14 +23,18 @@ module GraphQL
17
23
 
18
24
  options[:object] = object_to_cache if object_to_cache != NO_OBJECT
19
25
 
20
- fragment = Fragment.new(context, options)
26
+ context_to_use = options.delete(:context)
27
+ context_to_use = context if context_to_use.nil? && respond_to?(:context)
28
+ raise ArgumentError, "cannot find context, please pass it explicitly" unless context_to_use
29
+
30
+ fragment = Fragment.new(context_to_use, options)
21
31
 
22
32
  if (cached = fragment.read)
23
33
  return cached == Fragment::NIL_IN_CACHE ? nil : raw_value(cached)
24
34
  end
25
35
 
26
36
  (block_given? ? block.call : object_to_cache).tap do |resolved_value|
27
- context.fragments << fragment
37
+ context_to_use.fragments << fragment
28
38
  end
29
39
  end
30
40
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module GraphQL
4
4
  module FragmentCache
5
- VERSION = "1.0.5"
5
+ VERSION = "1.1.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.0.5
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - DmitryTsepelev
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-10-13 00:00:00.000000000 Z
11
+ date: 2020-10-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: graphql