From 25c47f151fbe7ab5c7f32986ca8c27ac8bbc91be Mon Sep 17 00:00:00 2001 From: Jerel Miller Date: Thu, 22 Aug 2024 21:55:53 -0600 Subject: [PATCH] Don't warn for caches that don't enable data masking --- src/cache/core/__tests__/cache.ts | 27 +++++++++++++++++++++++++++ src/cache/core/cache.ts | 8 ++++---- 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/src/cache/core/__tests__/cache.ts b/src/cache/core/__tests__/cache.ts index 22e7a8df4c9..0bb8a4609e9 100644 --- a/src/cache/core/__tests__/cache.ts +++ b/src/cache/core/__tests__/cache.ts @@ -198,6 +198,33 @@ describe("abstract cache", () => { ); }); + it("returns original data and does not warn on caches that don't implement the required interface with the default data masking config", () => { + using consoleSpy = spyOnConsole("warn"); + + const query = gql` + query { + user { + __typename + id + ...UserFields + } + } + + fragment UserFields on User { + name + } + `; + const data = { + user: { __typename: "User", id: 1, name: "Mister Masked" }, + }; + const cache = new TestCache(); + + const result = cache.maskOperation(query, data); + + expect(result).toBe(data); + expect(consoleSpy.warn).not.toHaveBeenCalled(); + }); + it("returns masked query for caches that implement required interface", () => { class MaskingCache extends TestCache { public readonly dataMasking = true; diff --git a/src/cache/core/cache.ts b/src/cache/core/cache.ts index 52375afb345..9e49564bf42 100644 --- a/src/cache/core/cache.ts +++ b/src/cache/core/cache.ts @@ -382,6 +382,10 @@ export abstract class ApolloCache implements DataProxy { } public maskOperation(document: DocumentNode, data: TData) { + if (!this.dataMasking) { + return data; + } + if (!this.fragmentMatches) { if (__DEV__) { invariant.warn( @@ -392,10 +396,6 @@ export abstract class ApolloCache implements DataProxy { return data; } - if (!this.dataMasking) { - return data; - } - return maskOperation(data, document, this.fragmentMatches.bind(this)); }