Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

watchFragment: forward additional options to diffOptions #11926

Merged
merged 6 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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/good-suns-happen.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@apollo/client": patch
---

`watchFragment`: forward additional options to `diffOptions`
4 changes: 2 additions & 2 deletions .size-limits.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"dist/apollo-client.min.cjs": 39825,
"import { ApolloClient, InMemoryCache, HttpLink } from \"dist/index.js\" (production)": 32851
"dist/apollo-client.min.cjs": 39642,
"import { ApolloClient, InMemoryCache, HttpLink } from \"dist/index.js\" (production)": 32870
}
96 changes: 96 additions & 0 deletions src/__tests__/ApolloClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2419,6 +2419,102 @@ describe("ApolloClient", () => {
new Error("Timeout waiting for next event")
);
});
it("works with `variables`", async () => {
const cache = new InMemoryCache();
const client = new ApolloClient({
cache,
link: ApolloLink.empty(),
});
const ItemFragment = gql`
fragment ItemFragment on Item {
id
text(language: $language)
}
`;

cache.writeFragment({
fragment: ItemFragment,
data: {
__typename: "Item",
id: 5,
text: "Item #5",
},
variables: { language: "Esperanto" },
});

const observable = client.watchFragment({
fragment: ItemFragment,
from: { __typename: "Item", id: 5 },
variables: { language: "Esperanto" },
});

const stream = new ObservableStream(observable);

{
const result = await stream.takeNext();

expect(result).toStrictEqual({
data: {
__typename: "Item",
id: 5,
text: "Item #5",
},
complete: true,
});
}
});
it("supports the @includes directive with `variables`", async () => {
const cache = new InMemoryCache();
const client = new ApolloClient({
cache,
link: ApolloLink.empty(),
});
const ItemFragment = gql`
fragment ItemFragment on Item {
id
text @include(if: $withText)
}
`;

cache.writeFragment({
fragment: ItemFragment,
data: {
__typename: "Item",
id: 5,
text: "Item #5",
},
variables: { withText: true },
});
cache.writeFragment({
fragment: ItemFragment,
data: {
__typename: "Item",
id: 5,
},
variables: { withText: false },
});

const observable = client.watchFragment({
fragment: ItemFragment,
from: { __typename: "Item", id: 5 },
variables: { withText: true },
});

const stream = new ObservableStream(observable);

{
const result = await stream.takeNext();

expect(result).toStrictEqual({
data: {
__typename: "Item",
id: 5,
text: "Item #5",
},
complete: true,
});
}
});
});

describe("defaultOptions", () => {
Expand Down
9 changes: 8 additions & 1 deletion src/cache/core/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,10 +228,17 @@ export abstract class ApolloCache<TSerialized> implements DataProxy {
public watchFragment<TData = any, TVars = OperationVariables>(
options: WatchFragmentOptions<TData, TVars>
): Observable<WatchFragmentResult<TData>> {
const { fragment, fragmentName, from, optimistic = true } = options;
const {
fragment,
fragmentName,
from,
optimistic = true,
...otherOptions
} = options;
const query = this.getFragmentDoc(fragment, fragmentName);

const diffOptions: Cache.DiffOptions<TData, TVars> = {
...otherOptions,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to fix the reported problem, forwarding just variables is already enough, but it might be a good idea to forward all unspecified options. WDYT @alessbell?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only other one is canonizeResults which is is deprecated and actually not used anywhere... I think we can remove it since it never did anything :/

returnPartialData: true,
id: typeof from === "string" ? from : this.identify(from),
query,
Expand Down
Loading