Skip to content

Commit 682cf37

Browse files
committed
Fix cache key builder options handling and tests
1 parent 1d7c8eb commit 682cf37

File tree

3 files changed

+78
-34
lines changed

3 files changed

+78
-34
lines changed

lib/graphql/fragment_cache/cache_key_builder.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,12 @@ def path_cache_key
137137
end
138138

139139
def include_argument?(argument_name)
140-
return false if @options[:exclude_arguments]&.include?(argument_name)
141-
return false if @options[:include_arguments] && !@options[:include_arguments].include?(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+
142146
true
143147
end
144148

spec/graphql/fragment_cache/cache_key_builder_spec.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,13 @@
6868
specify { is_expected.to eq "graphql/cachedPost/schema_key-cachedPost(id:#{id})[id.title.author[id.name]]" }
6969

7070
context "when excluding arguments" do
71-
let(:options) { {exclude_arguments: [:id]} }
71+
let(:options) { {cache_key: {exclude_arguments: [:id]}} }
7272

7373
specify { is_expected.to eq "graphql/cachedPost/schema_key-cachedPost()[id.title.author[id.name]]" }
7474
end
7575

7676
context "when including arguments" do
77-
let(:options) { {include_arguments: [:id]} }
77+
let(:options) { {cache_key: {include_arguments: [:id]}} }
7878

7979
specify { is_expected.to eq "graphql/cachedPost/schema_key-cachedPost(id:#{id})[id.title.author[id.name]]" }
8080
end
@@ -144,13 +144,13 @@
144144
specify { is_expected.to eq "graphql/cachedPostByComplexInput/schema_key-cachedPostByComplexInput(complex_post_input:{input_with_id:{id:#{id},int_arg:42},string_arg:woo})[id.title.author[id.name]]" }
145145

146146
context "when excluding arguments" do
147-
let(:options) { {exclude_arguments: [:int_arg]} }
147+
let(:options) { {cache_key: {exclude_arguments: [:int_arg]}} }
148148

149149
specify { is_expected.to eq "graphql/cachedPostByComplexInput/schema_key-cachedPostByComplexInput(complex_post_input:{input_with_id:{id:#{id}},string_arg:woo})[id.title.author[id.name]]" }
150150
end
151151

152152
context "when including arguments" do
153-
let(:options) { {include_arguments: [:complex_post_input, :input_with_id, :int_arg]} }
153+
let(:options) { {cache_key: {include_arguments: [:complex_post_input, :input_with_id, :int_arg]}} }
154154

155155
specify { is_expected.to eq "graphql/cachedPostByComplexInput/schema_key-cachedPostByComplexInput(complex_post_input:{input_with_id:{int_arg:42}})[id.title.author[id.name]]" }
156156
end

spec/graphql/fragment_cache/cacher_spec.rb

Lines changed: 68 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -76,23 +76,6 @@ def write_multi(hash, options)
7676
end
7777

7878
context "when cached fields have different options" do
79-
let(:schema) do
80-
build_schema do
81-
query(
82-
Class.new(Types::Query) {
83-
field :post, Types::Post, null: true do
84-
argument :id, GraphQL::Types::ID, required: true
85-
argument :cache_key, GraphQL::Types::String, required: true
86-
end
87-
88-
define_method(:post) { |id:, cache_key:|
89-
cache_fragment(query_cache_key: cache_key) { Post.find(id) }
90-
}
91-
}
92-
)
93-
end
94-
end
95-
9679
let(:query) do
9780
<<~GQL
9881
query getPost($id: ID!) {
@@ -107,19 +90,38 @@ def write_multi(hash, options)
10790
GQL
10891
end
10992

110-
it "uses #write_multi two times with different options" do
111-
execute_query
93+
context "when there options are passed to cache_fragment" do
94+
let(:schema) do
95+
build_schema do
96+
query(
97+
Class.new(Types::Query) {
98+
field :post, Types::Post, null: true do
99+
argument :id, GraphQL::Types::ID, required: true
100+
argument :cache_key, GraphQL::Types::String, required: true
101+
end
112102

113-
args = []
114-
expect(GraphQL::FragmentCache.cache_store).to \
115-
have_received(:write_multi).exactly(2).times do |r, options|
116-
args << options
103+
define_method(:post) { |id:, cache_key:|
104+
cache_fragment(query_cache_key: cache_key) { Post.find(id) }
105+
}
106+
}
107+
)
117108
end
109+
end
110+
111+
it "uses #write_multi two times with different query_cache_key options" do
112+
execute_query
113+
114+
args = []
115+
expect(GraphQL::FragmentCache.cache_store).to \
116+
have_received(:write_multi).exactly(2).times do |r, options|
117+
args << options
118+
end
118119

119-
expect(args).to eq([{query_cache_key: "1"}, {query_cache_key: "2"}])
120+
expect(args).to eq([{query_cache_key: "1"}, {query_cache_key: "2"}])
121+
end
120122
end
121123

122-
context "when different options exist, but should be excluded" do
124+
context "when cache key is autogenerated" do
123125
let(:schema) do
124126
build_schema do
125127
query(
@@ -130,16 +132,54 @@ def write_multi(hash, options)
130132
end
131133

132134
define_method(:post) { |id:, cache_key:|
133-
cache_fragment(cache_key: {exclude_arguments: [:cache_key]}) { Post.find(id) }
135+
cache_fragment { Post.find(id) }
134136
}
135137
}
136138
)
137139
end
138140
end
139141

140-
it "uses #write_multi ony one time time" do
142+
it "writes a cache key for each argument value" do
141143
execute_query
142-
expect(GraphQL::FragmentCache.cache_store).to have_received(:write_multi).once
144+
145+
args = []
146+
expect(GraphQL::FragmentCache.cache_store).to \
147+
have_received(:write_multi).once.times do |hash, options|
148+
args << hash
149+
end
150+
151+
expect(args.first.keys.length).to be(2)
152+
end
153+
154+
context "when arguments are excluded" do
155+
let(:schema) do
156+
build_schema do
157+
query(
158+
Class.new(Types::Query) {
159+
field :post, Types::Post, null: true do
160+
argument :id, GraphQL::Types::ID, required: true
161+
argument :cache_key, GraphQL::Types::String, required: true
162+
end
163+
164+
define_method(:post) { |id:, cache_key:|
165+
cache_fragment(cache_key: {exclude_arguments: [:cache_key]}) { Post.find(id) }
166+
}
167+
}
168+
)
169+
end
170+
end
171+
172+
it "writes only one cache key" do
173+
execute_query
174+
175+
args = []
176+
expect(GraphQL::FragmentCache.cache_store).to \
177+
have_received(:write_multi).once.times do |hash, options|
178+
args << hash
179+
end
180+
181+
expect(args.first.keys.length).to be(1)
182+
end
143183
end
144184
end
145185
end

0 commit comments

Comments
 (0)