Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 5 additions & 0 deletions .changeset/gold-crabs-invent.md
Original file line number Diff line number Diff line change
@@ -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.
71 changes: 71 additions & 0 deletions src/__tests__/ApolloClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down Expand Up @@ -555,6 +584,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", () => {
Expand Down
4 changes: 2 additions & 2 deletions src/core/ApolloClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1506,7 +1506,7 @@ export class ApolloClient {
TVariables extends OperationVariables = OperationVariables,
>(
options: ApolloClient.ReadQueryOptions<TData, TVariables>,
optimistic: boolean = false
optimistic = !!options.optimistic
): Unmasked<TData> | null {
return this.cache.readQuery<TData, TVariables>(
{ ...options, query: this.transform(options.query) },
Expand Down Expand Up @@ -1663,7 +1663,7 @@ export class ApolloClient {
TVariables extends OperationVariables = OperationVariables,
>(
options: ApolloClient.ReadFragmentOptions<TData, TVariables>,
optimistic: boolean = false
optimistic = !!options.optimistic
): Unmasked<TData> | null {
return this.cache.readFragment<TData, TVariables>(
{ ...options, fragment: this.transform(options.fragment) },
Expand Down
Loading