|
27 | 27 | let(:post) { Post.find(42) }
|
28 | 28 |
|
29 | 29 | let(:object) { nil }
|
| 30 | + let(:selected_type) { nil } |
30 | 31 | let(:context) { {} }
|
31 | 32 | let(:query_obj) { GraphQL::Query.new(schema, query, variables: variables, context: context) }
|
32 | 33 |
|
33 |
| - subject { described_class.call(object: object, query: query_obj, path: path) } |
| 34 | + subject { described_class.call(object: object, query: query_obj, path: path, selected_type: selected_type) } |
34 | 35 |
|
35 | 36 | # Make cache keys raw for easier debugging
|
36 | 37 | let(:schema_cache_key) { "schema_key" }
|
|
57 | 58 | GQL
|
58 | 59 | end
|
59 | 60 |
|
| 61 | + let(:selected_type) { Types::Post } |
| 62 | + |
60 | 63 | specify { is_expected.to eq "schema_key/cachedPost(id:#{id})[id.title.author[id.name]]" }
|
61 | 64 | end
|
62 | 65 |
|
|
77 | 80 | end
|
78 | 81 |
|
79 | 82 | let(:path) { ["cachedPostByInput"] }
|
80 |
| - |
| 83 | + let(:selected_type) { Types::Post } |
81 | 84 | let(:variables) { {inputWithId: {id: id, intArg: 42}} }
|
82 | 85 |
|
83 | 86 | specify { is_expected.to eq "schema_key/cachedPostByInput(input_with_id:{id:#{id},int_arg:42})[id.title.author[id.name]]" }
|
|
108 | 111 |
|
109 | 112 | context "when cached cached field is nested" do
|
110 | 113 | let(:path) { ["post", "cachedAuthor"] }
|
| 114 | + let(:selected_type) { Types::User } |
111 | 115 |
|
112 | 116 | let(:query) do
|
113 | 117 | <<~GQL
|
|
147 | 151 | GQL
|
148 | 152 | end
|
149 | 153 |
|
| 154 | + let(:selected_type) { Types::Post } |
| 155 | + |
150 | 156 | specify { is_expected.to eq "schema_key/cachedPost(id:#{id})[id.title.author[id.name]]" }
|
151 | 157 |
|
152 | 158 | context "when nested fragment is used" do
|
|
177 | 183 |
|
178 | 184 | context "when object is passed and responds to #cache_key" do
|
179 | 185 | let(:object) { post }
|
| 186 | + let(:selected_type) { Types::Post } |
180 | 187 |
|
181 | 188 | specify { is_expected.to eq "schema_key/cachedPost(id:#{id})[id.title]/#{post.cache_key}" }
|
182 | 189 | end
|
|
186 | 193 | post.singleton_class.define_method(:graphql_cache_key) { "super-cache" }
|
187 | 194 | end
|
188 | 195 |
|
| 196 | + let(:selected_type) { Types::Post } |
189 | 197 | let(:object) { post }
|
190 | 198 |
|
191 | 199 | specify { is_expected.to eq "schema_key/cachedPost(id:#{id})[id.title]/super-cache" }
|
192 | 200 | end
|
193 | 201 |
|
194 | 202 | context "when object is passed deosn't respond to #cache_key neither #graphql_cache_key" do
|
| 203 | + let(:selected_type) { Types::Post } |
195 | 204 | let(:object) { post.author }
|
196 | 205 |
|
197 | 206 | it "fallbacks to #to_s" do
|
|
200 | 209 | end
|
201 | 210 |
|
202 | 211 | context "when array is passed as object" do
|
| 212 | + let(:selected_type) { Types::Post } |
203 | 213 | let(:object) { [post, :custom, 99] }
|
204 | 214 |
|
205 | 215 | specify { is_expected.to eq "schema_key/cachedPost(id:#{id})[id.title]/#{post.cache_key}/custom/99" }
|
|
216 | 226 | }
|
217 | 227 | GQL
|
218 | 228 | end
|
219 |
| - |
| 229 | + let(:selected_type) { GraphQL::Types::String } |
220 | 230 | let(:path) { ["posts", 0, "cachedTitle"] }
|
221 | 231 |
|
222 | 232 | specify { is_expected.to eq "schema_key/posts/0/cachedTitle[]" }
|
|
240 | 250 | GQL
|
241 | 251 | end
|
242 | 252 |
|
| 253 | + let(:selected_type) { Types::User } |
243 | 254 | let(:path) { ["post", "author"] }
|
244 | 255 |
|
245 | 256 | specify { is_expected.to eq "schema_key/post(id:1)/cachedAuthor[name]" }
|
|
261 | 272 | GQL
|
262 | 273 | end
|
263 | 274 |
|
| 275 | + let(:selected_type) { Types::User } |
264 | 276 | let(:path) { ["post", "author"] }
|
265 | 277 |
|
266 | 278 | specify { is_expected.to eq "schema_key/post(id:1)/cachedAuthor[name]" }
|
267 | 279 | end
|
| 280 | + |
| 281 | + context "when query has union type" do |
| 282 | + let(:query) do |
| 283 | + <<~GQL |
| 284 | + query getFeed { |
| 285 | + feed { |
| 286 | + ...on PostType { |
| 287 | + id |
| 288 | + cachedAvatarUrl |
| 289 | + } |
| 290 | + ...on UserType { |
| 291 | + id |
| 292 | + cachedAvatarUrl |
| 293 | + } |
| 294 | + } |
| 295 | + } |
| 296 | + GQL |
| 297 | + end |
| 298 | + |
| 299 | + let(:selected_type) { Types::Post } |
| 300 | + let(:path) { ["feed", 0, "cachedAvatarUrl"] } |
| 301 | + |
| 302 | + specify { is_expected.to eq "schema_key/feed/0/cachedAvatarUrl[]" } |
| 303 | + |
| 304 | + context "when cached field has alias" do |
| 305 | + let(:query) do |
| 306 | + <<~GQL |
| 307 | + query getFeed { |
| 308 | + feed { |
| 309 | + ...on PostType { |
| 310 | + id |
| 311 | + avatarUrl: cachedAvatarUrl |
| 312 | + } |
| 313 | + ...on UserType { |
| 314 | + id |
| 315 | + avatarUrl: cachedAvatarUrl |
| 316 | + } |
| 317 | + } |
| 318 | + } |
| 319 | + GQL |
| 320 | + end |
| 321 | + |
| 322 | + let(:selected_type) { Types::Post } |
| 323 | + let(:path) { ["feed", 0, "avatarUrl"] } |
| 324 | + |
| 325 | + specify { is_expected.to eq "schema_key/feed/0/cachedAvatarUrl[]" } |
| 326 | + end |
| 327 | + end |
268 | 328 | end
|
0 commit comments