From 30ea75e421a16cf95739c0a0360bc20a8e103573 Mon Sep 17 00:00:00 2001 From: Jerel Miller Date: Tue, 16 Jun 2026 22:33:13 -0400 Subject: [PATCH 1/3] Fix issue where optimistic option is not honored in readFragment --- src/__tests__/ApolloClient.ts | 42 +++++++++++++++++++++++++++++++++++ src/core/ApolloClient.ts | 2 +- 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/src/__tests__/ApolloClient.ts b/src/__tests__/ApolloClient.ts index 2b428760d00..c08045eb753 100644 --- a/src/__tests__/ApolloClient.ts +++ b/src/__tests__/ApolloClient.ts @@ -555,6 +555,48 @@ describe("ApolloClient", () => { }) ).toEqual({ __typename: "Foo", a: 1, b: 2, c: 3 }); }); + + it("reads optimistic data when optimistic is passed in options", () => { + const cache = new InMemoryCache(); + const client = new ApolloClient({ + link: ApolloLink.empty(), + cache, + }); + + const fragment = gql` + fragment NameFragment on Person { + id + firstName + lastName + } + `; + + cache.recordOptimisticTransaction((proxy) => { + proxy.writeFragment({ + from: { __typename: "Person", id: 1 }, + fragment, + data: { + __typename: "Person", + id: 1, + firstName: "Test", + lastName: "User", + }, + }); + }, "1"); + + expect( + client.readFragment({ + from: { __typename: "Person", id: 1 }, + fragment, + optimistic: true, + }) + ).toEqual({ + __typename: "Person", + id: 1, + firstName: "Test", + lastName: "User", + }); + }); }); describe("writeQuery", () => { diff --git a/src/core/ApolloClient.ts b/src/core/ApolloClient.ts index ff9e9d77211..0826f2f8ac0 100644 --- a/src/core/ApolloClient.ts +++ b/src/core/ApolloClient.ts @@ -1663,7 +1663,7 @@ export class ApolloClient { TVariables extends OperationVariables = OperationVariables, >( options: ApolloClient.ReadFragmentOptions, - optimistic: boolean = false + optimistic = !!options.optimistic ): Unmasked | null { return this.cache.readFragment( { ...options, fragment: this.transform(options.fragment) }, From fde9607f1a6b2977da5fceb14cd7b67115071678 Mon Sep 17 00:00:00 2001 From: Jerel Miller Date: Tue, 16 Jun 2026 22:36:07 -0400 Subject: [PATCH 2/3] Fix issue with readQuery with optimistic passed as options --- src/__tests__/ApolloClient.ts | 29 +++++++++++++++++++++++++++++ src/core/ApolloClient.ts | 2 +- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/__tests__/ApolloClient.ts b/src/__tests__/ApolloClient.ts index c08045eb753..39eeb5a3414 100644 --- a/src/__tests__/ApolloClient.ts +++ b/src/__tests__/ApolloClient.ts @@ -210,6 +210,35 @@ describe("ApolloClient", () => { }) ).toEqual({ a: 1, b: 2 }); }); + + it("reads optimistic data when optimistic is passed in options", () => { + const cache = new InMemoryCache(); + const client = new ApolloClient({ + link: ApolloLink.empty(), + cache, + }); + + const query = gql` + query { + a + b + } + `; + + cache.recordOptimisticTransaction((proxy) => { + proxy.writeQuery({ + query, + data: { a: 1, b: 2 }, + }); + }, "1"); + + expect( + client.readQuery({ + query, + optimistic: true, + }) + ).toEqual({ a: 1, b: 2 }); + }); }); it("will read some data from the store with default values", () => { diff --git a/src/core/ApolloClient.ts b/src/core/ApolloClient.ts index 0826f2f8ac0..cbde11446b2 100644 --- a/src/core/ApolloClient.ts +++ b/src/core/ApolloClient.ts @@ -1506,7 +1506,7 @@ export class ApolloClient { TVariables extends OperationVariables = OperationVariables, >( options: ApolloClient.ReadQueryOptions, - optimistic: boolean = false + optimistic = !!options.optimistic ): Unmasked | null { return this.cache.readQuery( { ...options, query: this.transform(options.query) }, From 8fa5cff01aefd7a7004c7b3dc17fafc6c35a5e15 Mon Sep 17 00:00:00 2001 From: Jerel Miller Date: Tue, 16 Jun 2026 22:37:13 -0400 Subject: [PATCH 3/3] Add changeset --- .changeset/gold-crabs-invent.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/gold-crabs-invent.md diff --git a/.changeset/gold-crabs-invent.md b/.changeset/gold-crabs-invent.md new file mode 100644 index 00000000000..38f0fc4527e --- /dev/null +++ b/.changeset/gold-crabs-invent.md @@ -0,0 +1,5 @@ +--- +"@apollo/client": patch +--- + +Fixes an issue where `client.readFragment` and `client.readQuery` ignored the `optimistic` option when passed in the options object.