graphql-fragment_cache 1.10.0 → 1.11.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 +4 -0
- data/README.md +1 -13
- data/lib/.rbnext/2.3/graphql/fragment_cache/cache_key_builder.rb +203 -0
- data/lib/.rbnext/2.3/graphql/fragment_cache/memory_store.rb +64 -0
- data/lib/.rbnext/2.7/graphql/fragment_cache/cache_key_builder.rb +203 -0
- data/lib/.rbnext/2.7/graphql/fragment_cache/ext/graphql_cache_key.rb +93 -0
- data/lib/graphql/fragment_cache/cache_key_builder.rb +6 -1
- data/lib/graphql/fragment_cache/schema/tracer.rb +1 -1
- data/lib/graphql/fragment_cache/version.rb +1 -1
- data/lib/graphql/fragment_cache.rb +4 -13
- metadata +12 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 66fc20ed7d15e85c1d4ce2890d6b28c368a75e3e06ef69ff186470b5a0953567
|
4
|
+
data.tar.gz: 4d0d3f12716b8d4d5be627411121cbdba2d067991ea1d6874f3b2891f2636a4a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5758f73fc04e6e63246b6d21899891220f4e7a7733561a72b6b652a26a6f7bd602425d95d14d2711f15a90865caf907d78244c253243541fd414fa59c93c4954
|
7
|
+
data.tar.gz: bea6d191cdaba4f7b5ed13250c1d5aaec02e0aafbd44cc8b9d29f5a8dba3718f43db62f683df01d3946ff611603f6dd5a6bba2001344ada4d29143c8aae8291a
|
data/CHANGELOG.md
CHANGED
@@ -2,6 +2,10 @@
|
|
2
2
|
|
3
3
|
## master
|
4
4
|
|
5
|
+
## 1.11.0 (2022-02-26)
|
6
|
+
|
7
|
+
- [PR#79](https://github.com/DmitryTsepelev/graphql-ruby-fragment_cache/pull/79) Support graphql-ruby 2.0.0 ([@DmitryTsepelev][])
|
8
|
+
|
5
9
|
## 1.10.0 (2022-01-30)
|
6
10
|
|
7
11
|
- [PR#77](https://github.com/DmitryTsepelev/graphql-ruby-fragment_cache/pull/77) Drop Ruby 2.5 support, add Ruby 3.0 ([@DmitryTsepelev][])
|
data/README.md
CHANGED
@@ -17,13 +17,10 @@ end
|
|
17
17
|
|
18
18
|
## Getting started
|
19
19
|
|
20
|
-
Add the gem to your Gemfile `gem 'graphql-fragment_cache'` and add the plugin to your schema class
|
20
|
+
Add the gem to your Gemfile `gem 'graphql-fragment_cache'` and add the plugin to your schema class:
|
21
21
|
|
22
22
|
```ruby
|
23
23
|
class GraphqSchema < GraphQL::Schema
|
24
|
-
use GraphQL::Execution::Interpreter
|
25
|
-
use GraphQL::Analysis::AST
|
26
|
-
|
27
24
|
use GraphQL::FragmentCache
|
28
25
|
|
29
26
|
query QueryType
|
@@ -69,15 +66,6 @@ class QueryType < BaseObject
|
|
69
66
|
end
|
70
67
|
```
|
71
68
|
|
72
|
-
If you use [connections](https://graphql-ruby.org/pagination/connection_concepts.html) and plan to cache them—please turn on [brand new](https://github.com/rmosolgo/graphql-ruby/blob/master/lib/graphql/pagination/connections.rb#L5) connections hierarchy in your schema:
|
73
|
-
|
74
|
-
```ruby
|
75
|
-
class GraphqSchema < GraphQL::Schema
|
76
|
-
# ...
|
77
|
-
use GraphQL::Pagination::Connections
|
78
|
-
end
|
79
|
-
```
|
80
|
-
|
81
69
|
## Cache key generation
|
82
70
|
|
83
71
|
Cache keys consist of the following parts: namespace, implicit key, and explicit key.
|
@@ -0,0 +1,203 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "json"
|
4
|
+
require "digest"
|
5
|
+
|
6
|
+
using RubyNext
|
7
|
+
|
8
|
+
module GraphQL
|
9
|
+
module FragmentCache
|
10
|
+
using Ext
|
11
|
+
|
12
|
+
using(Module.new {
|
13
|
+
refine Array do
|
14
|
+
def traverse_argument(argument)
|
15
|
+
return argument unless argument.is_a?(GraphQL::Schema::InputObject)
|
16
|
+
|
17
|
+
"{#{argument.map { |_1, _2| "#{_1}:#{traverse_argument(_2)}" }.sort.join(",")}}"
|
18
|
+
end
|
19
|
+
|
20
|
+
def to_selections_key
|
21
|
+
map { |val|
|
22
|
+
children = val.selections.empty? ? "" : "[#{val.selections.to_selections_key}]"
|
23
|
+
|
24
|
+
field_name = val.field.name
|
25
|
+
field_alias = val.ast_nodes.map(&:alias).join
|
26
|
+
field_name = "#{field_alias}:#{field_name}" unless field_alias.empty?
|
27
|
+
|
28
|
+
unless val.arguments.empty?
|
29
|
+
args = val.arguments.map { |_1, _2| "#{_1}:#{traverse_argument(_2)}" }.sort.join(",")
|
30
|
+
field_name += "(#{args})"
|
31
|
+
end
|
32
|
+
|
33
|
+
"#{field_name}#{children}"
|
34
|
+
}.join(".")
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
refine ::GraphQL::Language::Nodes::AbstractNode do
|
39
|
+
def alias?(_)
|
40
|
+
false
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
refine ::GraphQL::Language::Nodes::Field do
|
45
|
+
def alias?(val)
|
46
|
+
self.alias == val
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
refine ::GraphQL::Execution::Lookahead do
|
51
|
+
def selection_with_alias(name, **kwargs)
|
52
|
+
return selection(name, **kwargs) if selects?(name, **kwargs)
|
53
|
+
alias_selection(name, **kwargs)
|
54
|
+
end
|
55
|
+
|
56
|
+
def alias_selection(name, selected_type: @selected_type, arguments: nil)
|
57
|
+
return alias_selections[name] if alias_selections.key?(name)
|
58
|
+
|
59
|
+
alias_node = lookup_alias_node(ast_nodes, name)
|
60
|
+
return ::GraphQL::Execution::Lookahead::NULL_LOOKAHEAD unless alias_node
|
61
|
+
|
62
|
+
next_field_name = alias_node.name
|
63
|
+
|
64
|
+
# From https://github.com/rmosolgo/graphql-ruby/blob/1a9a20f3da629e63ea8e5ee8400be82218f9edc3/lib/graphql/execution/lookahead.rb#L91
|
65
|
+
next_field_defn =
|
66
|
+
if GraphQL::FragmentCache.graphql_ruby_before_2_0?
|
67
|
+
get_class_based_field(selected_type, next_field_name)
|
68
|
+
else
|
69
|
+
@query.get_field(selected_type, next_field_name)
|
70
|
+
end
|
71
|
+
|
72
|
+
alias_selections[name] =
|
73
|
+
if next_field_defn
|
74
|
+
next_nodes = []
|
75
|
+
arguments = @query.arguments_for(alias_node, next_field_defn)
|
76
|
+
arguments = arguments.is_a?(::GraphQL::Execution::Interpreter::Arguments) ? arguments.keyword_arguments : arguments
|
77
|
+
@ast_nodes.each do |ast_node|
|
78
|
+
ast_node.selections.each do |selection|
|
79
|
+
find_selected_nodes(selection, next_field_name, next_field_defn, arguments: arguments, matches: next_nodes)
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
if next_nodes.any?
|
84
|
+
::GraphQL::Execution::Lookahead.new(query: @query, ast_nodes: next_nodes, field: next_field_defn, owner_type: selected_type)
|
85
|
+
else
|
86
|
+
::GraphQL::Execution::Lookahead::NULL_LOOKAHEAD
|
87
|
+
end
|
88
|
+
else
|
89
|
+
::GraphQL::Execution::Lookahead::NULL_LOOKAHEAD
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
def alias_selections
|
94
|
+
return @alias_selections if defined?(@alias_selections)
|
95
|
+
@alias_selections ||= {}
|
96
|
+
end
|
97
|
+
|
98
|
+
def lookup_alias_node(nodes, name)
|
99
|
+
return if nodes.empty?
|
100
|
+
|
101
|
+
nodes.find do |node|
|
102
|
+
if node.is_a?(GraphQL::Language::Nodes::FragmentSpread)
|
103
|
+
node = @query.fragments[node.name]
|
104
|
+
raise("Invariant: Can't look ahead to nonexistent fragment #{node.name} (found: #{@query.fragments.keys})") unless node
|
105
|
+
end
|
106
|
+
|
107
|
+
return node if node.alias?(name)
|
108
|
+
child = lookup_alias_node(node.children, name)
|
109
|
+
return child if child
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
113
|
+
})
|
114
|
+
|
115
|
+
# Builds cache key for fragment
|
116
|
+
class CacheKeyBuilder
|
117
|
+
using RubyNext
|
118
|
+
|
119
|
+
class << self
|
120
|
+
def call(**options)
|
121
|
+
new(**options).build
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
attr_reader :query, :path, :object, :schema
|
126
|
+
|
127
|
+
def initialize(object: nil, query:, path:, **options)
|
128
|
+
@object = object
|
129
|
+
@query = query
|
130
|
+
@schema = query.schema
|
131
|
+
@path = path
|
132
|
+
@options = options
|
133
|
+
end
|
134
|
+
|
135
|
+
def build
|
136
|
+
[
|
137
|
+
GraphQL::FragmentCache.namespace,
|
138
|
+
implicit_cache_key,
|
139
|
+
object_cache_key
|
140
|
+
].compact.join("/")
|
141
|
+
end
|
142
|
+
|
143
|
+
private
|
144
|
+
|
145
|
+
def implicit_cache_key
|
146
|
+
Digest::SHA1.hexdigest("#{schema_cache_key}/#{query_cache_key}")
|
147
|
+
end
|
148
|
+
|
149
|
+
def schema_cache_key
|
150
|
+
@options.fetch(:schema_cache_key) { schema.schema_cache_key }
|
151
|
+
end
|
152
|
+
|
153
|
+
def query_cache_key
|
154
|
+
@options.fetch(:query_cache_key) { "#{path_cache_key}[#{selections_cache_key}]" }
|
155
|
+
end
|
156
|
+
|
157
|
+
def selections_cache_key
|
158
|
+
current_root =
|
159
|
+
path.reduce(query.lookahead) { |lkhd, field_name|
|
160
|
+
# Handle cached fields inside collections:
|
161
|
+
next lkhd if field_name.is_a?(Integer)
|
162
|
+
|
163
|
+
lkhd.selection_with_alias(field_name)
|
164
|
+
}
|
165
|
+
|
166
|
+
current_root.selections.to_selections_key
|
167
|
+
end
|
168
|
+
|
169
|
+
def path_cache_key
|
170
|
+
@options.fetch(:path_cache_key) do
|
171
|
+
lookahead = query.lookahead
|
172
|
+
|
173
|
+
path.map { |field_name|
|
174
|
+
# Handle cached fields inside collections:
|
175
|
+
next field_name if field_name.is_a?(Integer)
|
176
|
+
|
177
|
+
lookahead = lookahead.selection_with_alias(field_name)
|
178
|
+
raise "Failed to look ahead the field: #{field_name}" if lookahead.is_a?(::GraphQL::Execution::Lookahead::NullLookahead)
|
179
|
+
|
180
|
+
next lookahead.field.name if lookahead.arguments.empty?
|
181
|
+
|
182
|
+
args = lookahead.arguments.map { |_1, _2| "#{_1}:#{traverse_argument(_2)}" }.sort.join(",")
|
183
|
+
"#{lookahead.field.name}(#{args})"
|
184
|
+
}.join("/")
|
185
|
+
end
|
186
|
+
end
|
187
|
+
|
188
|
+
def traverse_argument(argument)
|
189
|
+
return argument unless argument.is_a?(GraphQL::Schema::InputObject)
|
190
|
+
|
191
|
+
"{#{argument.map { |_1, _2| "#{_1}:#{traverse_argument(_2)}" }.sort.join(",")}}"
|
192
|
+
end
|
193
|
+
|
194
|
+
def object_cache_key
|
195
|
+
@options[:object_cache_key] || object_key(object)
|
196
|
+
end
|
197
|
+
|
198
|
+
def object_key(obj)
|
199
|
+
((!obj.nil?) || nil) && obj._graphql_cache_key
|
200
|
+
end
|
201
|
+
end
|
202
|
+
end
|
203
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
using RubyNext
|
4
|
+
|
5
|
+
module GraphQL
|
6
|
+
module FragmentCache
|
7
|
+
# Memory adapter for storing cached fragments
|
8
|
+
class MemoryStore
|
9
|
+
using RubyNext
|
10
|
+
|
11
|
+
class Entry < Struct.new(:value, :expires_at, keyword_init: true)
|
12
|
+
def expired?
|
13
|
+
expires_at && expires_at < Time.now
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
attr_reader :default_expires_in
|
18
|
+
|
19
|
+
def initialize(expires_in: nil, **other)
|
20
|
+
raise ArgumentError, "Unsupported options: #{other.keys.join(",")}" unless other.empty?
|
21
|
+
|
22
|
+
@default_expires_in = expires_in
|
23
|
+
@storage = {}
|
24
|
+
end
|
25
|
+
|
26
|
+
def keys
|
27
|
+
storage.keys
|
28
|
+
end
|
29
|
+
|
30
|
+
def exist?(key)
|
31
|
+
storage.key?(key)
|
32
|
+
end
|
33
|
+
|
34
|
+
def read(key)
|
35
|
+
key = key.to_s
|
36
|
+
((!storage[key].nil?) || nil) && storage[key].then do |entry|
|
37
|
+
if entry.expired?
|
38
|
+
delete(key)
|
39
|
+
next
|
40
|
+
end
|
41
|
+
entry.value
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def write(key, value, expires_in: default_expires_in, **options)
|
46
|
+
key = key.to_s
|
47
|
+
@storage[key] = Entry.new(value: value, expires_at: expires_in ? Time.now + expires_in : nil)
|
48
|
+
end
|
49
|
+
|
50
|
+
def delete(key)
|
51
|
+
key = key.to_s
|
52
|
+
storage.delete(key)
|
53
|
+
end
|
54
|
+
|
55
|
+
def clear
|
56
|
+
storage.clear
|
57
|
+
end
|
58
|
+
|
59
|
+
private
|
60
|
+
|
61
|
+
attr_reader :storage
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
@@ -0,0 +1,203 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "json"
|
4
|
+
require "digest"
|
5
|
+
|
6
|
+
using RubyNext
|
7
|
+
|
8
|
+
module GraphQL
|
9
|
+
module FragmentCache
|
10
|
+
using Ext
|
11
|
+
|
12
|
+
using(Module.new {
|
13
|
+
refine Array do
|
14
|
+
def traverse_argument(argument)
|
15
|
+
return argument unless argument.is_a?(GraphQL::Schema::InputObject)
|
16
|
+
|
17
|
+
"{#{argument.map { |_1, _2| "#{_1}:#{traverse_argument(_2)}" }.sort.join(",")}}"
|
18
|
+
end
|
19
|
+
|
20
|
+
def to_selections_key
|
21
|
+
map { |val|
|
22
|
+
children = val.selections.empty? ? "" : "[#{val.selections.to_selections_key}]"
|
23
|
+
|
24
|
+
field_name = val.field.name
|
25
|
+
field_alias = val.ast_nodes.map(&:alias).join
|
26
|
+
field_name = "#{field_alias}:#{field_name}" unless field_alias.empty?
|
27
|
+
|
28
|
+
unless val.arguments.empty?
|
29
|
+
args = val.arguments.map { |_1, _2| "#{_1}:#{traverse_argument(_2)}" }.sort.join(",")
|
30
|
+
field_name += "(#{args})"
|
31
|
+
end
|
32
|
+
|
33
|
+
"#{field_name}#{children}"
|
34
|
+
}.join(".")
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
refine ::GraphQL::Language::Nodes::AbstractNode do
|
39
|
+
def alias?(_)
|
40
|
+
false
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
refine ::GraphQL::Language::Nodes::Field do
|
45
|
+
def alias?(val)
|
46
|
+
self.alias == val
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
refine ::GraphQL::Execution::Lookahead do
|
51
|
+
def selection_with_alias(name, **kwargs)
|
52
|
+
return selection(name, **kwargs) if selects?(name, **kwargs)
|
53
|
+
alias_selection(name, **kwargs)
|
54
|
+
end
|
55
|
+
|
56
|
+
def alias_selection(name, selected_type: @selected_type, arguments: nil)
|
57
|
+
return alias_selections[name] if alias_selections.key?(name)
|
58
|
+
|
59
|
+
alias_node = lookup_alias_node(ast_nodes, name)
|
60
|
+
return ::GraphQL::Execution::Lookahead::NULL_LOOKAHEAD unless alias_node
|
61
|
+
|
62
|
+
next_field_name = alias_node.name
|
63
|
+
|
64
|
+
# From https://github.com/rmosolgo/graphql-ruby/blob/1a9a20f3da629e63ea8e5ee8400be82218f9edc3/lib/graphql/execution/lookahead.rb#L91
|
65
|
+
next_field_defn =
|
66
|
+
if GraphQL::FragmentCache.graphql_ruby_before_2_0?
|
67
|
+
get_class_based_field(selected_type, next_field_name)
|
68
|
+
else
|
69
|
+
@query.get_field(selected_type, next_field_name)
|
70
|
+
end
|
71
|
+
|
72
|
+
alias_selections[name] =
|
73
|
+
if next_field_defn
|
74
|
+
next_nodes = []
|
75
|
+
arguments = @query.arguments_for(alias_node, next_field_defn)
|
76
|
+
arguments = arguments.is_a?(::GraphQL::Execution::Interpreter::Arguments) ? arguments.keyword_arguments : arguments
|
77
|
+
@ast_nodes.each do |ast_node|
|
78
|
+
ast_node.selections.each do |selection|
|
79
|
+
find_selected_nodes(selection, next_field_name, next_field_defn, arguments: arguments, matches: next_nodes)
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
if next_nodes.any?
|
84
|
+
::GraphQL::Execution::Lookahead.new(query: @query, ast_nodes: next_nodes, field: next_field_defn, owner_type: selected_type)
|
85
|
+
else
|
86
|
+
::GraphQL::Execution::Lookahead::NULL_LOOKAHEAD
|
87
|
+
end
|
88
|
+
else
|
89
|
+
::GraphQL::Execution::Lookahead::NULL_LOOKAHEAD
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
def alias_selections
|
94
|
+
return @alias_selections if defined?(@alias_selections)
|
95
|
+
@alias_selections ||= {}
|
96
|
+
end
|
97
|
+
|
98
|
+
def lookup_alias_node(nodes, name)
|
99
|
+
return if nodes.empty?
|
100
|
+
|
101
|
+
nodes.find do |node|
|
102
|
+
if node.is_a?(GraphQL::Language::Nodes::FragmentSpread)
|
103
|
+
node = @query.fragments[node.name]
|
104
|
+
raise("Invariant: Can't look ahead to nonexistent fragment #{node.name} (found: #{@query.fragments.keys})") unless node
|
105
|
+
end
|
106
|
+
|
107
|
+
return node if node.alias?(name)
|
108
|
+
child = lookup_alias_node(node.children, name)
|
109
|
+
return child if child
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
113
|
+
})
|
114
|
+
|
115
|
+
# Builds cache key for fragment
|
116
|
+
class CacheKeyBuilder
|
117
|
+
using RubyNext
|
118
|
+
|
119
|
+
class << self
|
120
|
+
def call(**options)
|
121
|
+
new(**options).build
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
attr_reader :query, :path, :object, :schema
|
126
|
+
|
127
|
+
def initialize(object: nil, query:, path:, **options)
|
128
|
+
@object = object
|
129
|
+
@query = query
|
130
|
+
@schema = query.schema
|
131
|
+
@path = path
|
132
|
+
@options = options
|
133
|
+
end
|
134
|
+
|
135
|
+
def build
|
136
|
+
[
|
137
|
+
GraphQL::FragmentCache.namespace,
|
138
|
+
implicit_cache_key,
|
139
|
+
object_cache_key
|
140
|
+
].compact.join("/")
|
141
|
+
end
|
142
|
+
|
143
|
+
private
|
144
|
+
|
145
|
+
def implicit_cache_key
|
146
|
+
Digest::SHA1.hexdigest("#{schema_cache_key}/#{query_cache_key}")
|
147
|
+
end
|
148
|
+
|
149
|
+
def schema_cache_key
|
150
|
+
@options.fetch(:schema_cache_key) { schema.schema_cache_key }
|
151
|
+
end
|
152
|
+
|
153
|
+
def query_cache_key
|
154
|
+
@options.fetch(:query_cache_key) { "#{path_cache_key}[#{selections_cache_key}]" }
|
155
|
+
end
|
156
|
+
|
157
|
+
def selections_cache_key
|
158
|
+
current_root =
|
159
|
+
path.reduce(query.lookahead) { |lkhd, field_name|
|
160
|
+
# Handle cached fields inside collections:
|
161
|
+
next lkhd if field_name.is_a?(Integer)
|
162
|
+
|
163
|
+
lkhd.selection_with_alias(field_name)
|
164
|
+
}
|
165
|
+
|
166
|
+
current_root.selections.to_selections_key
|
167
|
+
end
|
168
|
+
|
169
|
+
def path_cache_key
|
170
|
+
@options.fetch(:path_cache_key) do
|
171
|
+
lookahead = query.lookahead
|
172
|
+
|
173
|
+
path.map { |field_name|
|
174
|
+
# Handle cached fields inside collections:
|
175
|
+
next field_name if field_name.is_a?(Integer)
|
176
|
+
|
177
|
+
lookahead = lookahead.selection_with_alias(field_name)
|
178
|
+
raise "Failed to look ahead the field: #{field_name}" if lookahead.is_a?(::GraphQL::Execution::Lookahead::NullLookahead)
|
179
|
+
|
180
|
+
next lookahead.field.name if lookahead.arguments.empty?
|
181
|
+
|
182
|
+
args = lookahead.arguments.map { |_1, _2| "#{_1}:#{traverse_argument(_2)}" }.sort.join(",")
|
183
|
+
"#{lookahead.field.name}(#{args})"
|
184
|
+
}.join("/")
|
185
|
+
end
|
186
|
+
end
|
187
|
+
|
188
|
+
def traverse_argument(argument)
|
189
|
+
return argument unless argument.is_a?(GraphQL::Schema::InputObject)
|
190
|
+
|
191
|
+
"{#{argument.map { |_1, _2| "#{_1}:#{traverse_argument(_2)}" }.sort.join(",")}}"
|
192
|
+
end
|
193
|
+
|
194
|
+
def object_cache_key
|
195
|
+
@options[:object_cache_key] || object_key(object)
|
196
|
+
end
|
197
|
+
|
198
|
+
def object_key(obj)
|
199
|
+
obj&._graphql_cache_key
|
200
|
+
end
|
201
|
+
end
|
202
|
+
end
|
203
|
+
end
|
@@ -0,0 +1,93 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module GraphQL
|
4
|
+
module FragmentCache
|
5
|
+
module Ext
|
6
|
+
# Adds #_graphql_cache_key method to Object,
|
7
|
+
# which just call #graphql_cache_key or #cache_key.
|
8
|
+
#
|
9
|
+
# For other core classes returns string representation.
|
10
|
+
#
|
11
|
+
# Raises ArgumentError otherwise.
|
12
|
+
#
|
13
|
+
# We use a refinement to avoid case/if statements for type checking
|
14
|
+
refine Object do
|
15
|
+
def _graphql_cache_key
|
16
|
+
return graphql_cache_key if respond_to?(:graphql_cache_key)
|
17
|
+
return cache_key if respond_to?(:cache_key)
|
18
|
+
return to_a._graphql_cache_key if respond_to?(:to_a)
|
19
|
+
|
20
|
+
to_s
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
refine Array do
|
25
|
+
def _graphql_cache_key
|
26
|
+
map { |_1| _1._graphql_cache_key }.join("/")
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
refine NilClass do
|
31
|
+
def _graphql_cache_key
|
32
|
+
""
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
refine TrueClass do
|
37
|
+
def _graphql_cache_key
|
38
|
+
"t"
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
refine FalseClass do
|
43
|
+
def _graphql_cache_key
|
44
|
+
"f"
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
refine String do
|
49
|
+
def _graphql_cache_key
|
50
|
+
self
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
refine Symbol do
|
55
|
+
def _graphql_cache_key
|
56
|
+
to_s
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
if RUBY_PLATFORM.match?(/java/i)
|
61
|
+
refine Integer do
|
62
|
+
def _graphql_cache_key
|
63
|
+
to_s
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
refine Float do
|
68
|
+
def _graphql_cache_key
|
69
|
+
to_s
|
70
|
+
end
|
71
|
+
end
|
72
|
+
else
|
73
|
+
refine Numeric do
|
74
|
+
def _graphql_cache_key
|
75
|
+
to_s
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
refine Time do
|
81
|
+
def _graphql_cache_key
|
82
|
+
to_s
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
refine Module do
|
87
|
+
def _graphql_cache_key
|
88
|
+
name
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
@@ -62,7 +62,12 @@ module GraphQL
|
|
62
62
|
next_field_name = alias_node.name
|
63
63
|
|
64
64
|
# From https://github.com/rmosolgo/graphql-ruby/blob/1a9a20f3da629e63ea8e5ee8400be82218f9edc3/lib/graphql/execution/lookahead.rb#L91
|
65
|
-
next_field_defn =
|
65
|
+
next_field_defn =
|
66
|
+
if GraphQL::FragmentCache.graphql_ruby_before_2_0?
|
67
|
+
get_class_based_field(selected_type, next_field_name)
|
68
|
+
else
|
69
|
+
@query.get_field(selected_type, next_field_name)
|
70
|
+
end
|
66
71
|
|
67
72
|
alias_selections[name] =
|
68
73
|
if next_field_defn
|
@@ -23,7 +23,7 @@ module GraphQL
|
|
23
23
|
end
|
24
24
|
|
25
25
|
def verify_connections!(context)
|
26
|
-
return if
|
26
|
+
return if context.schema.new_connections?
|
27
27
|
|
28
28
|
raise StandardError,
|
29
29
|
"GraphQL::Pagination::Connections should be enabled for connection caching"
|
@@ -50,29 +50,20 @@ module GraphQL
|
|
50
50
|
@cache_store = store
|
51
51
|
end
|
52
52
|
|
53
|
-
def
|
54
|
-
Gem::Dependency.new("graphql", "
|
53
|
+
def graphql_ruby_before_2_0?
|
54
|
+
Gem::Dependency.new("graphql", "< 2.0.0").match?("graphql", GraphQL::VERSION)
|
55
55
|
end
|
56
56
|
|
57
57
|
private
|
58
58
|
|
59
59
|
def verify_interpreter_and_analysis!(schema_defn)
|
60
|
-
if
|
61
|
-
unless schema_defn.interpreter?
|
62
|
-
raise StandardError,
|
63
|
-
"GraphQL::Execution::Execute should not be enabled for fragment caching"
|
64
|
-
end
|
65
|
-
|
66
|
-
unless schema_defn.analysis_engine == GraphQL::Analysis::AST
|
67
|
-
raise StandardError,
|
68
|
-
"GraphQL::Analysis should not be enabled for fragment caching"
|
69
|
-
end
|
70
|
-
else
|
60
|
+
if graphql_ruby_before_2_0?
|
71
61
|
unless schema_defn.interpreter?
|
72
62
|
raise StandardError,
|
73
63
|
"GraphQL::Execution::Interpreter should be enabled for fragment caching"
|
74
64
|
end
|
75
65
|
|
66
|
+
puts "schema_defn.analysis_engine #{schema_defn.analysis_engine}"
|
76
67
|
unless schema_defn.analysis_engine == GraphQL::Analysis::AST
|
77
68
|
raise StandardError,
|
78
69
|
"GraphQL::Analysis::AST should be enabled for fragment caching"
|
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.11.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- DmitryTsepelev
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-
|
11
|
+
date: 2022-02-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: graphql
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: 1.
|
19
|
+
version: 1.12.0
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: 1.
|
26
|
+
version: 1.12.0
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: ruby-next
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -190,6 +190,10 @@ files:
|
|
190
190
|
- README.md
|
191
191
|
- bin/console
|
192
192
|
- bin/setup
|
193
|
+
- lib/.rbnext/2.3/graphql/fragment_cache/cache_key_builder.rb
|
194
|
+
- lib/.rbnext/2.3/graphql/fragment_cache/memory_store.rb
|
195
|
+
- lib/.rbnext/2.7/graphql/fragment_cache/cache_key_builder.rb
|
196
|
+
- lib/.rbnext/2.7/graphql/fragment_cache/ext/graphql_cache_key.rb
|
193
197
|
- lib/graphql-fragment_cache.rb
|
194
198
|
- lib/graphql/fragment_cache.rb
|
195
199
|
- lib/graphql/fragment_cache/cache_key_builder.rb
|
@@ -215,7 +219,7 @@ metadata:
|
|
215
219
|
homepage_uri: https://github.com/DmitryTsepelev/graphql-ruby-fragment_cache
|
216
220
|
source_code_uri: https://github.com/DmitryTsepelev/graphql-ruby-fragment_cache
|
217
221
|
changelog_uri: https://github.com/DmitryTsepelev/graphql-ruby-fragment_cache/CHANGELOG.md
|
218
|
-
post_install_message:
|
222
|
+
post_install_message:
|
219
223
|
rdoc_options: []
|
220
224
|
require_paths:
|
221
225
|
- lib
|
@@ -230,8 +234,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
230
234
|
- !ruby/object:Gem::Version
|
231
235
|
version: '0'
|
232
236
|
requirements: []
|
233
|
-
rubygems_version: 3.
|
234
|
-
signing_key:
|
237
|
+
rubygems_version: 3.3.7
|
238
|
+
signing_key:
|
235
239
|
specification_version: 4
|
236
240
|
summary: Fragment cache for graphql-ruby
|
237
241
|
test_files: []
|