graphql-fragment_cache 1.11.0 → 1.12.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 66fc20ed7d15e85c1d4ce2890d6b28c368a75e3e06ef69ff186470b5a0953567
4
- data.tar.gz: 4d0d3f12716b8d4d5be627411121cbdba2d067991ea1d6874f3b2891f2636a4a
3
+ metadata.gz: fb58b8735fa1b7a92dae359467252e366bf34da888f46ef98db0737db7b6dfc4
4
+ data.tar.gz: 037dd3a8c61ed1f359b50d6ae0e4ae7f169d825fb5c6da8cf0ee8968c6e1b9b4
5
5
  SHA512:
6
- metadata.gz: 5758f73fc04e6e63246b6d21899891220f4e7a7733561a72b6b652a26a6f7bd602425d95d14d2711f15a90865caf907d78244c253243541fd414fa59c93c4954
7
- data.tar.gz: bea6d191cdaba4f7b5ed13250c1d5aaec02e0aafbd44cc8b9d29f5a8dba3718f43db62f683df01d3946ff611603f6dd5a6bba2001344ada4d29143c8aae8291a
6
+ metadata.gz: 6eef1102518837f505b077f515b7a6e38a6e37993f072921193e30a2a877aec2e67899e8d256b23738cabda2bddf7a8e99967743c14b504afcedd389e2d23734
7
+ data.tar.gz: cb110783f67fb0674065fae17ff860e6c2a81a2fde04f5800634c689f2a1bcf01a54d5183e0ffbf5359d709a9b5493d967be39809abccf91fb5701b00eac7f7f
data/CHANGELOG.md CHANGED
@@ -4,6 +4,7 @@
4
4
 
5
5
  ## 1.11.0 (2022-02-26)
6
6
 
7
+ - [PR#70](https://github.com/DmitryTsepelev/graphql-ruby-fragment_cache/pull/70), [PR#82](https://github.com/DmitryTsepelev/graphql-ruby-fragment_cache/pull/82) Add #read_multi for fragments ([@daukadolt][], [@frostmark][])
7
8
  - [PR#79](https://github.com/DmitryTsepelev/graphql-ruby-fragment_cache/pull/79) Support graphql-ruby 2.0.0 ([@DmitryTsepelev][])
8
9
 
9
10
  ## 1.10.0 (2022-01-30)
@@ -128,3 +129,5 @@
128
129
  [@bbugh]: https://github.com/bbugh
129
130
  [@jeromedalbert]: https://github.com/jeromedalbert
130
131
  [@mretzak]: https://github.com/mretzak
132
+ [@daukadolt]: https://github.com/daukadolt
133
+ [@frostmark]: https://github.com/frostmark
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "graphql/fragment_cache/schema/lazy_cache_resolver"
4
+
3
5
  module GraphQL
4
6
  module FragmentCache
5
7
  # Wraps resolver with cache method
@@ -8,19 +8,41 @@ module GraphQL
8
8
 
9
9
  # Represents a single fragment to cache
10
10
  class Fragment
11
+ NIL_IN_CACHE = Object.new
12
+
13
+ class << self
14
+ def read_multi(fragments)
15
+ unless FragmentCache.cache_store.respond_to?(:read_multi)
16
+ return fragments.map { |f| [f, f.read] }.to_h
17
+ end
18
+
19
+ fragments_to_cache_keys = fragments
20
+ .map { |f| [f, f.cache_key] }.to_h
21
+
22
+ cache_keys = fragments_to_cache_keys.values
23
+
24
+ cache_keys_to_values = FragmentCache.cache_store.read_multi(*cache_keys)
25
+
26
+ fetched_fragments_to_values = cache_keys_to_values
27
+ .map { |key, val| [fragments_to_cache_keys.key(key), val] }
28
+ .to_h
29
+
30
+ fetched_fragments_to_values
31
+ end
32
+ end
33
+
11
34
  attr_reader :options, :path, :context
12
35
 
13
36
  def initialize(context, **options)
14
37
  @context = context
38
+ @keep_in_context = options.delete(:keep_in_context)
15
39
  @options = options
16
40
  @path = interpreter_context[:current_path]
17
41
  end
18
42
 
19
- NIL_IN_CACHE = Object.new
20
-
21
- def read(keep_in_context = false)
43
+ def read
22
44
  return nil if context[:renew_cache] == true
23
- return read_from_context { value_from_cache } if keep_in_context
45
+ return read_from_context { value_from_cache } if @keep_in_context
24
46
 
25
47
  value_from_cache
26
48
  end
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "graphql/fragment_cache/fragment"
4
+ require "graphql/fragment_cache/schema/lazy_cache_resolver"
4
5
 
5
6
  module GraphQL
6
7
  module FragmentCache
@@ -44,14 +45,7 @@ module GraphQL
44
45
 
45
46
  fragment = Fragment.new(context_to_use, **options)
46
47
 
47
- keep_in_context = options.delete(:keep_in_context)
48
- if (cached = fragment.read(keep_in_context))
49
- return cached == Fragment::NIL_IN_CACHE ? nil : raw_value(cached)
50
- end
51
-
52
- (block_given? ? block.call : object_to_cache).tap do |resolved_value|
53
- context_to_use.fragments << fragment
54
- end
48
+ GraphQL::FragmentCache::Schema::LazyCacheResolver.new(fragment, context_to_use, object_to_cache, &block)
55
49
  end
56
50
  end
57
51
  end
@@ -0,0 +1,41 @@
1
+ require "graphql/fragment_cache/fragment"
2
+
3
+ module GraphQL
4
+ module FragmentCache
5
+ module Schema
6
+ using Ext
7
+ class LazyCacheResolver
8
+ def initialize(fragment, query_ctx, object_to_cache, &block)
9
+ @fragment = fragment
10
+ @query_ctx = query_ctx
11
+ @object_to_cache = object_to_cache
12
+ @lazy_state = query_ctx[:lazy_cache_resolver_statez] ||= {
13
+ pending_fragments: Set.new,
14
+ resolved_fragments: {}
15
+ }
16
+ @block = block
17
+
18
+ @lazy_state[:pending_fragments] << @fragment
19
+ end
20
+
21
+ def resolve
22
+ unless @lazy_state[:resolved_fragments].key?(@fragment)
23
+ resolved_fragments = Fragment.read_multi(@lazy_state[:pending_fragments].to_a)
24
+ @lazy_state[:pending_fragments].clear
25
+ resolved_fragments.each { |key, value| @lazy_state[:resolved_fragments][key] = value }
26
+ end
27
+
28
+ cached = @lazy_state[:resolved_fragments][@fragment]
29
+
30
+ if cached
31
+ return cached == Fragment::NIL_IN_CACHE ? nil : GraphQL::Execution::Interpreter::RawValue.new(cached)
32
+ end
33
+
34
+ (@block ? @block.call : @object_to_cache).tap do |resolved_value|
35
+ @query_ctx.fragments << @fragment
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module GraphQL
4
4
  module FragmentCache
5
- VERSION = "1.11.0"
5
+ VERSION = "1.12.0"
6
6
  end
7
7
  end
@@ -11,6 +11,7 @@ require "graphql/fragment_cache/connections/patch"
11
11
  require "graphql/fragment_cache/schema/patch"
12
12
  require "graphql/fragment_cache/schema/tracer"
13
13
  require "graphql/fragment_cache/schema/instrumentation"
14
+ require "graphql/fragment_cache/schema/lazy_cache_resolver"
14
15
 
15
16
  require "graphql/fragment_cache/memory_store"
16
17
 
@@ -30,6 +31,7 @@ module GraphQL
30
31
  schema_defn.tracer(Schema::Tracer)
31
32
  schema_defn.instrument(:query, Schema::Instrumentation)
32
33
  schema_defn.extend(Schema::Patch)
34
+ schema_defn.lazy_resolve(Schema::LazyCacheResolver, :resolve)
33
35
 
34
36
  GraphQL::Pagination::Connections.prepend(Connections::Patch)
35
37
  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.11.0
4
+ version: 1.12.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - DmitryTsepelev
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-02-26 00:00:00.000000000 Z
11
+ date: 2022-08-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: graphql
@@ -209,6 +209,7 @@ files:
209
209
  - lib/graphql/fragment_cache/rails/cache_key_builder.rb
210
210
  - lib/graphql/fragment_cache/railtie.rb
211
211
  - lib/graphql/fragment_cache/schema/instrumentation.rb
212
+ - lib/graphql/fragment_cache/schema/lazy_cache_resolver.rb
212
213
  - lib/graphql/fragment_cache/schema/patch.rb
213
214
  - lib/graphql/fragment_cache/schema/tracer.rb
214
215
  - lib/graphql/fragment_cache/version.rb
@@ -234,7 +235,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
234
235
  - !ruby/object:Gem::Version
235
236
  version: '0'
236
237
  requirements: []
237
- rubygems_version: 3.3.7
238
+ rubygems_version: 3.3.3
238
239
  signing_key:
239
240
  specification_version: 4
240
241
  summary: Fragment cache for graphql-ruby