diff --git a/change/@graphitation-apollo-forest-run-85adcd22-ca87-4f61-901a-565fc0b2a7ea.json b/change/@graphitation-apollo-forest-run-85adcd22-ca87-4f61-901a-565fc0b2a7ea.json new file mode 100644 index 000000000..638cd84fd --- /dev/null +++ b/change/@graphitation-apollo-forest-run-85adcd22-ca87-4f61-901a-565fc0b2a7ea.json @@ -0,0 +1,7 @@ +{ + "type": "patch", + "comment": "fix(apollo-forest-run): cache.modify within optimistic transaction", + "packageName": "@graphitation/apollo-forest-run", + "email": "vladimir.razuvaev@gmail.com", + "dependentChangeType": "patch" +} diff --git a/packages/apollo-forest-run/src/__tests__/regression.test.ts b/packages/apollo-forest-run/src/__tests__/regression.test.ts index 22451bd21..e1869fcbf 100644 --- a/packages/apollo-forest-run/src/__tests__/regression.test.ts +++ b/packages/apollo-forest-run/src/__tests__/regression.test.ts @@ -998,3 +998,52 @@ test("correctly writes fragment on abstract type", () => { ], }); }); + +test("correctly handles optimistic cache.modify", () => { + const query = gql` + { + items { + id + name + } + } + `; + const foo = { __typename: "Foo", id: "1", name: "foo" }; + const bar = { __typename: "Bar", id: "2", name: "bar" }; + const cache = new ForestRun(); + + cache.write({ + dataId: "ROOT_QUERY", + query, + result: { + items: [{ ...foo }, { ...bar }], + }, + }); + + let optimisticModify; + const before = cache.diff({ query, optimistic: true }); + cache.recordOptimisticTransaction(() => { + optimisticModify = cache.modify({ + id: cache.identify(foo), + fields: { name: () => "fooChanged" }, + }); + }, "test"); + const optimisticData = cache.diff({ query, optimistic: true }); + cache.removeOptimistic("test"); + const restoredData = cache.diff({ query, optimistic: true }); + + expect(before.result).toEqual({ + items: [foo, bar], + }); + expect(optimisticData.result).toEqual({ + items: [{ ...foo, name: "fooChanged" }, bar], + }); + expect(restoredData.result).toEqual({ + items: [foo, bar], + }); + expect(optimisticModify).toBe(true); +}); + +test("definitely fails", () => { + expect(true).toBe(false); +});