|
1 | 1 | import type { ForestRun } from "@graphitation/apollo-forest-run"; |
2 | 2 | import type { Scenario, ScenarioContext } from "./types"; |
| 3 | +import { parse } from "graphql"; |
| 4 | + |
| 5 | +// Pre-parse 50 unique named queries to simulate a real app with many active operations. |
| 6 | +// Each has its own operation name and data, creating separate trees in the forest. |
| 7 | +// This is the setup that exposes O(n) scans in getCoveringOperationIds. |
| 8 | +const EXTRA_OPS_COUNT = 50; |
| 9 | +const extraOperations = Array.from({ length: EXTRA_OPS_COUNT }, (_, i) => ({ |
| 10 | + query: parse(`query BgQuery${i} { node${i}(id: "${i}") { id value } }`), |
| 11 | + data: { [`node${i}`]: { __typename: `Node${i}`, id: `${i}`, value: i } }, |
| 12 | +})); |
3 | 13 |
|
4 | 14 | const addWatchers = ( |
5 | 15 | watcherCount: number, |
@@ -404,4 +414,32 @@ export const scenarios = [ |
404 | 414 | }; |
405 | 415 | }, |
406 | 416 | }, |
| 417 | + { |
| 418 | + name: "write-many-ops-one-field-change", |
| 419 | + prepare: (ctx: ScenarioContext) => { |
| 420 | + const { operations, CacheFactory, configuration, watcherCount } = ctx; |
| 421 | + const cache = new CacheFactory(configuration); |
| 422 | + |
| 423 | + // Populate the cache with many unrelated operations (simulating real app) |
| 424 | + for (const { query, data } of extraOperations) { |
| 425 | + cache.writeQuery({ query, data }); |
| 426 | + } |
| 427 | + |
| 428 | + // Write the main query, add watchers, then measure a write with a change. |
| 429 | + // Each watcher's diff triggers readOperation → applyTransformations → |
| 430 | + // createChunkMatcher → getCoveringOperationIds which scans all trees. |
| 431 | + const { data, query } = operations["complex-nested"]; |
| 432 | + cache.writeQuery({ query, data: data["complex-nested"] }); |
| 433 | + addWatchers(watcherCount, cache, query); |
| 434 | + |
| 435 | + return { |
| 436 | + run() { |
| 437 | + return cache.writeQuery({ |
| 438 | + query, |
| 439 | + data: data["complex-nested-single-field-change"], |
| 440 | + }); |
| 441 | + }, |
| 442 | + }; |
| 443 | + }, |
| 444 | + }, |
407 | 445 | ] as const satisfies Scenario[]; |
0 commit comments