Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions lib/graphql/fragment_cache/cache_key_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,16 +130,22 @@ def path_cache_key

next lookahead.field.name if lookahead.arguments.empty?

args = lookahead.arguments.map { "#{_1}:#{traverse_argument(_2)}" }.sort.join(",")
args = lookahead.arguments.select { include_argument?(_1) }.map { "#{_1}:#{traverse_argument(_2)}" }.sort.join(",")
"#{lookahead.field.name}(#{args})"
}.join("/")
end
end

def include_argument?(argument_name)
return false if @options[:exclude_arguments]&.include?(argument_name)
return false if @options[:include_arguments] && !@options[:include_arguments].include?(argument_name)
true
end

def traverse_argument(argument)
return argument unless argument.is_a?(GraphQL::Schema::InputObject)

"{#{argument.map { "#{_1}:#{traverse_argument(_2)}" }.sort.join(",")}}"
"{#{argument.map { include_argument?(_1) ? "#{_1}:#{traverse_argument(_2)}" : nil }.compact.sort.join(",")}}"
end

def object_cache_key
Expand Down
26 changes: 25 additions & 1 deletion spec/graphql/fragment_cache/cache_key_builder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,18 @@
end

specify { is_expected.to eq "graphql/cachedPost/schema_key-cachedPost(id:#{id})[id.title.author[id.name]]" }

context "when excluding arguments" do
let(:options) { {exclude_arguments: [:id]} }

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

context "when including arguments" do
let(:options) { {include_arguments: [:id]} }

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

context "when cached field has aliased selections" do
Expand Down Expand Up @@ -109,7 +121,7 @@

specify { is_expected.to eq "graphql/cachedPostByInput/schema_key-cachedPostByInput(input_with_id:{id:#{id},int_arg:42})[id.title.author[id.name]]" }

context "when argument is complext input" do
context "when argument is complex input" do
let(:query) do
<<~GQL
query GetPostByComplexInput($complexPostInput: ComplexPostInput!) {
Expand All @@ -130,6 +142,18 @@
let(:variables) { {complexPostInput: {stringArg: "woo", inputWithId: {id: id, intArg: 42}}} }

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]]" }

context "when excluding arguments" do
let(:options) { {exclude_arguments: [:int_arg]} }

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]]" }
end

context "when including arguments" do
let(:options) { {include_arguments: [:complex_post_input, :input_with_id, :int_arg]} }

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

Expand Down
24 changes: 24 additions & 0 deletions spec/graphql/fragment_cache/cacher_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,30 @@ def write_multi(hash, options)

expect(args).to eq([{query_cache_key: "1"}, {query_cache_key: "2"}])
end

context "when different options exist, but should be excluded" do
let(:schema) do
build_schema do
query(
Class.new(Types::Query) {
field :post, Types::Post, null: true do
argument :id, GraphQL::Types::ID, required: true
argument :cache_key, GraphQL::Types::String, required: true
end

define_method(:post) { |id:, cache_key:|
cache_fragment(cache_key: {exclude_arguments: [:cache_key]}) { Post.find(id) }
}
}
)
end
end

it "uses #write_multi ony one time time" do
execute_query
expect(GraphQL::FragmentCache.cache_store).to have_received(:write_multi).once
end
end
end
end

Expand Down
Loading