From dc76c05452316c602cb7f119ddac7a3cf1144fde Mon Sep 17 00:00:00 2001 From: Palash Agarwal Date: Mon, 14 Jun 2021 18:19:08 -0700 Subject: [PATCH] Clear loader cache in Mutation#after_resolve The `resolve` method of a Mutation can make changes to the DB that invalidate any cached Loader results. So, the cache should be cleared after the resolve method has completed, and before any nested fields on the Mutation query are resolved. In our codebase, this causes authorization errors if the Mutation removes a member from a list/connection, and the client requests that same list field as part of the mutation response. Since the Loader cached the pre-mutation result, the wrong data is returned to the client. --- lib/graphql/batch/mutation_field_extension.rb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/graphql/batch/mutation_field_extension.rb b/lib/graphql/batch/mutation_field_extension.rb index 272a3b1..964a8d2 100644 --- a/lib/graphql/batch/mutation_field_extension.rb +++ b/lib/graphql/batch/mutation_field_extension.rb @@ -8,5 +8,10 @@ def resolve(object:, arguments:, **_rest) GraphQL::Batch::Executor.current.clear end end + + def after_resolve(value:, **_rest) + GraphQL::Batch::Executor.current.clear + value + end end end