@@ -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