|
# frozen_string_literal: true |
|
|
|
require "rails_helper" |
|
|
|
describe "Fragment caching" do |
|
let(:user) { create(:user, username: "cacho-macho") } |
|
let(:post) { create(:post, title: "relay me cached", creator: user) } |
|
let(:variables) { {post: post.slug} } |
|
|
|
let(:query) do |
|
%q( |
|
query getPostBySlug($slug: String) { |
|
post(slug: $slug) { |
|
id |
|
title |
|
} |
|
} |
|
) |
|
end |
|
|
|
specify "with cache disabled" do |
|
expect(node.fetch("title")).to eq "relay me cached" |
|
|
|
# update title |
|
post.update_column :title, "you shall not cache" |
|
|
|
new_result = execute_query(query, variables: variables) |
|
|
|
new_node = new_result.dig("data", "post") |
|
expect(new_node.fetch("title")).to eq "you shall not cache" |
|
end |
|
|
|
context "with cache enabled", :cache do |
|
specify "cache uses selection" do |
|
expect(node.fetch("title")).to eq "relay me cached" |
|
|
|
# update title |
|
post.update_column :title, "you shall not cache" |
|
|
|
new_result = execute_query(query, variables: variables) |
|
|
|
new_node = new_result.dig("data", "post") |
|
# selection hasn't changed |
|
expect(new_node.fetch("title")).to eq "relay me cached" |
|
|
|
new_query = ' |
|
query getPostBySlug2($slug: String) { |
|
post(slug: $slug) { |
|
id |
|
name |
|
description |
|
} |
|
} |
|
' |
|
new_result = execute_query(new_query, variables: variables) |
|
|
|
new_node = new_result.dig("data", "post") |
|
expect(new_node.fetch("title")).to eq "you shall not cache" |
|
end |
|
|
|
specify "cache uses object cache_key" do |
|
expect(node.fetch("title")).to eq "relay me cached" |
|
|
|
# update title with touching updated_at |
|
travel_to 1.minute.since |
|
post.update!(title: "you shall not cache") |
|
|
|
new_result = execute_query(query, variables: variables) |
|
|
|
new_node = new_result.dig("data", "post") |
|
expect(new_node.fetch("title")).to eq "you shall not cache" |
|
end |
|
|
|
context "with batch loading" do |
|
let(:query) do |
|
%q(query getPostBySlug($slug: String) { |
|
post(slug: $slug) { |
|
id |
|
name |
|
author { |
|
name |
|
} |
|
} |
|
}) |
|
end |
|
|
|
specify "should skip caching 'cause we cannot cache lazy values" do |
|
expect(node.dig("author", "name"))).to eq "cacho-macho" |
|
|
|
user.update_column :name, "uncacheable" |
|
|
|
new_result = execute_query(query, variables: variables) |
|
expect(new_result.dig("data", "post", "author", "name").to eq "uncacheable" |
|
end |
|
end |
|
end |
|
end |