Skip to content

Commit 3dd6432

Browse files
phryneasalessbell
andauthored
watchFragment: forward additional options to diffOptions (#11926)
* `watchFragment`: forward additional options to `diffOptions` fixes #11924 * chore: bump .size-limits.json * chore: bump .size-limits.json * Clean up Prettier, Size-limit, and Api-Extractor --------- Co-authored-by: Alessia Bellisario <[email protected]> Co-authored-by: alessbell <[email protected]>
1 parent 7d833b8 commit 3dd6432

File tree

4 files changed

+111
-3
lines changed

4 files changed

+111
-3
lines changed

.changeset/good-suns-happen.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@apollo/client": patch
3+
---
4+
5+
`watchFragment`: forward additional options to `diffOptions`

.size-limits.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
2-
"dist/apollo-client.min.cjs": 40185,
3-
"import { ApolloClient, InMemoryCache, HttpLink } from \"dist/index.js\" (production)": 32977
2+
"dist/apollo-client.min.cjs": 40206,
3+
"import { ApolloClient, InMemoryCache, HttpLink } from \"dist/index.js\" (production)": 32999
44
}

src/__tests__/ApolloClient.ts

+96
Original file line numberDiff line numberDiff line change
@@ -2419,6 +2419,102 @@ describe("ApolloClient", () => {
24192419
new Error("Timeout waiting for next event")
24202420
);
24212421
});
2422+
it("works with `variables`", async () => {
2423+
const cache = new InMemoryCache();
2424+
const client = new ApolloClient({
2425+
cache,
2426+
link: ApolloLink.empty(),
2427+
});
2428+
const ItemFragment = gql`
2429+
fragment ItemFragment on Item {
2430+
id
2431+
text(language: $language)
2432+
}
2433+
`;
2434+
2435+
cache.writeFragment({
2436+
fragment: ItemFragment,
2437+
data: {
2438+
__typename: "Item",
2439+
id: 5,
2440+
text: "Item #5",
2441+
},
2442+
variables: { language: "Esperanto" },
2443+
});
2444+
2445+
const observable = client.watchFragment({
2446+
fragment: ItemFragment,
2447+
from: { __typename: "Item", id: 5 },
2448+
variables: { language: "Esperanto" },
2449+
});
2450+
2451+
const stream = new ObservableStream(observable);
2452+
2453+
{
2454+
const result = await stream.takeNext();
2455+
2456+
expect(result).toStrictEqual({
2457+
data: {
2458+
__typename: "Item",
2459+
id: 5,
2460+
text: "Item #5",
2461+
},
2462+
complete: true,
2463+
});
2464+
}
2465+
});
2466+
it("supports the @includes directive with `variables`", async () => {
2467+
const cache = new InMemoryCache();
2468+
const client = new ApolloClient({
2469+
cache,
2470+
link: ApolloLink.empty(),
2471+
});
2472+
const ItemFragment = gql`
2473+
fragment ItemFragment on Item {
2474+
id
2475+
text @include(if: $withText)
2476+
}
2477+
`;
2478+
2479+
cache.writeFragment({
2480+
fragment: ItemFragment,
2481+
data: {
2482+
__typename: "Item",
2483+
id: 5,
2484+
text: "Item #5",
2485+
},
2486+
variables: { withText: true },
2487+
});
2488+
cache.writeFragment({
2489+
fragment: ItemFragment,
2490+
data: {
2491+
__typename: "Item",
2492+
id: 5,
2493+
},
2494+
variables: { withText: false },
2495+
});
2496+
2497+
const observable = client.watchFragment({
2498+
fragment: ItemFragment,
2499+
from: { __typename: "Item", id: 5 },
2500+
variables: { withText: true },
2501+
});
2502+
2503+
const stream = new ObservableStream(observable);
2504+
2505+
{
2506+
const result = await stream.takeNext();
2507+
2508+
expect(result).toStrictEqual({
2509+
data: {
2510+
__typename: "Item",
2511+
id: 5,
2512+
text: "Item #5",
2513+
},
2514+
complete: true,
2515+
});
2516+
}
2517+
});
24222518
});
24232519

24242520
describe("defaultOptions", () => {

src/cache/core/cache.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -228,10 +228,17 @@ export abstract class ApolloCache<TSerialized> implements DataProxy {
228228
public watchFragment<TData = any, TVars = OperationVariables>(
229229
options: WatchFragmentOptions<TData, TVars>
230230
): Observable<WatchFragmentResult<TData>> {
231-
const { fragment, fragmentName, from, optimistic = true } = options;
231+
const {
232+
fragment,
233+
fragmentName,
234+
from,
235+
optimistic = true,
236+
...otherOptions
237+
} = options;
232238
const query = this.getFragmentDoc(fragment, fragmentName);
233239

234240
const diffOptions: Cache.DiffOptions<TData, TVars> = {
241+
...otherOptions,
235242
returnPartialData: true,
236243
id: typeof from === "string" ? from : this.identify(from),
237244
query,

0 commit comments

Comments
 (0)