Skip to content

Commit eb14261

Browse files
committed
Add test that validates cache path
1 parent c138ed6 commit eb14261

1 file changed

Lines changed: 143 additions & 0 deletions

File tree

src/__tests__/local-state/export.ts

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1149,6 +1149,149 @@ describe("@client @export tests", () => {
11491149
expect(fetchCount).toBe(4);
11501150
});
11511151

1152+
test("should refetch after a complete cache hit when a remote field is missing and variables come from @export", async () => {
1153+
const query = gql`
1154+
query UserWithExport($userId: ID!) {
1155+
userId @client @export(as: "userId")
1156+
user(id: $userId) {
1157+
id
1158+
address {
1159+
id
1160+
random
1161+
}
1162+
}
1163+
}
1164+
`;
1165+
1166+
let fetchCount = 0;
1167+
const link = new ApolloLink((operation) => {
1168+
fetchCount += 1;
1169+
const id = operation.variables.userId as string;
1170+
return of({
1171+
data: {
1172+
user: {
1173+
__typename: "User",
1174+
id,
1175+
address: {
1176+
__typename: "Address",
1177+
id: `addr-${id}`,
1178+
random: fetchCount,
1179+
},
1180+
},
1181+
},
1182+
});
1183+
});
1184+
1185+
const cache = new InMemoryCache();
1186+
const client = new ApolloClient({
1187+
cache,
1188+
link,
1189+
localState: new LocalState(),
1190+
});
1191+
1192+
client.writeQuery({
1193+
query: gql`
1194+
{
1195+
userId
1196+
}
1197+
`,
1198+
data: { userId: "1" },
1199+
});
1200+
client.writeQuery({
1201+
query,
1202+
variables: { userId: "1" },
1203+
data: {
1204+
userId: "1",
1205+
user: {
1206+
__typename: "User",
1207+
id: "1",
1208+
address: {
1209+
__typename: "Address",
1210+
id: "addr-1",
1211+
random: 0,
1212+
},
1213+
},
1214+
},
1215+
});
1216+
1217+
const stream = new ObservableStream(client.watchQuery({ query }));
1218+
1219+
// Export resolution is async, so we still get a loading emission
1220+
await expect(stream).toEmitTypedValue({
1221+
data: undefined,
1222+
dataState: "empty",
1223+
loading: true,
1224+
networkStatus: NetworkStatus.loading,
1225+
partial: true,
1226+
});
1227+
1228+
await expect(stream).toEmitTypedValue({
1229+
data: {
1230+
userId: "1",
1231+
user: {
1232+
__typename: "User",
1233+
id: "1",
1234+
address: {
1235+
__typename: "Address",
1236+
id: "addr-1",
1237+
random: 0,
1238+
},
1239+
},
1240+
},
1241+
dataState: "complete",
1242+
loading: false,
1243+
networkStatus: NetworkStatus.ready,
1244+
partial: false,
1245+
});
1246+
expect(fetchCount).toBe(0);
1247+
1248+
// Make sure writing the same variables don't refetch unnecessarily
1249+
client.writeQuery({
1250+
query: gql`
1251+
{
1252+
userId
1253+
}
1254+
`,
1255+
data: { userId: "1" },
1256+
});
1257+
await expect(stream).not.toEmitAnything({ timeout: 200 });
1258+
expect(fetchCount).toBe(0);
1259+
1260+
cache.evict({
1261+
id: cache.identify({ __typename: "User", id: "1" }),
1262+
fieldName: "address",
1263+
});
1264+
cache.gc();
1265+
1266+
await expect(stream).toEmitSimilarValue({
1267+
expected: (previous) => ({
1268+
...previous,
1269+
loading: true,
1270+
networkStatus: NetworkStatus.loading,
1271+
}),
1272+
});
1273+
1274+
await expect(stream).toEmitTypedValue({
1275+
data: {
1276+
userId: "1",
1277+
user: {
1278+
__typename: "User",
1279+
id: "1",
1280+
address: {
1281+
__typename: "Address",
1282+
id: "addr-1",
1283+
random: 1,
1284+
},
1285+
},
1286+
},
1287+
dataState: "complete",
1288+
loading: false,
1289+
networkStatus: NetworkStatus.ready,
1290+
partial: false,
1291+
});
1292+
expect(fetchCount).toBe(1);
1293+
});
1294+
11521295
test("merges concurrent cache updates during a streamed response when variables come from @export", async () => {
11531296
const multipart = mockDeferStreamGraphQL17Alpha9();
11541297

0 commit comments

Comments
 (0)