Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions packages/apollo-forest-run/src/ForestRun.ts
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ export class ForestRun extends ApolloCache<SerializedCache> {

const output: SerializedCache = {};
for (const [_, tree] of dataForest.trees.entries()) {
const historyLength = tree.history.getAll()?.length ?? 0;
const historyLength = tree.history?.getAll()?.length ?? 0;
output[key(tree.operation)] = stableConvert(
tree.operation,
tree.result.data,
Expand All @@ -429,7 +429,7 @@ export class ForestRun extends ApolloCache<SerializedCache> {
for (const layer of this.store.optimisticLayers) {
for (const [id, optimisticTree] of layer.trees.entries()) {
const tree = dataForest.trees.get(id);
const historyLength = tree?.history.getAll()?.length ?? 0;
const historyLength = tree?.history?.getAll()?.length ?? 0;
output[key(optimisticTree.operation)] = stableConvert(
optimisticTree.operation,
tree?.result.data ?? null,
Expand Down
5 changes: 5 additions & 0 deletions packages/apollo-forest-run/src/__tests__/extract.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@ describe("ForestRun.extract()", () => {
"data": {
"foo": 0,
},
"hasHistory": false,
"optimisticData": null,
"variables": {},
},
"query Foo:2": {
"data": {
"foo": 1,
},
"hasHistory": false,
"optimisticData": null,
"variables": {
"i": 1,
Expand Down Expand Up @@ -54,6 +56,7 @@ describe("ForestRun.extract()", () => {
"data": {
"foo": 0,
},
"hasHistory": false,
"optimisticData": {
"foo": 2,
},
Expand All @@ -63,6 +66,7 @@ describe("ForestRun.extract()", () => {
"data": {
"foo": 1,
},
"hasHistory": false,
"optimisticData": null,
"variables": {
"i": 1,
Expand All @@ -87,6 +91,7 @@ describe("ForestRun.extract()", () => {
{
"query Foo:1": {
"data": null,
"hasHistory": false,
"optimisticData": {
"foo": 1,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1660,7 +1660,7 @@ describe("diff against base with missing fields", () => {
const forestEnv = {
objectKey: (obj: any) => obj.id,
};
const diffEnv = {};
const diffEnv = { enableHistory: false, enableDataHistory: false };
const doc = gql`
query ($arg: String) {
foo(arg: $arg)
Expand Down Expand Up @@ -1724,6 +1724,8 @@ function diff(
) {
const env = {
objectKey: (obj: any) => obj.id,
enableHistory: false,
enableDataHistory: false,
...testEnv,
};
const baseOperation = createTestOperation(completeObjectDoc, baseVariables);
Expand Down
16 changes: 9 additions & 7 deletions packages/apollo-forest-run/src/diff/diffObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,10 @@ function diffCompositeListValue(
return undefined;
}

const itemsChanges: CompositeListLayoutChange[] = [];
const itemsChanges: CompositeListLayoutChange[] | undefined = context.env
.enableHistory
? []
: undefined;
const layoutDiffResult =
diff?.layout ?? diffCompositeListLayout(context, base, model, itemsChanges);

Expand Down Expand Up @@ -483,7 +486,7 @@ function diffCompositeListLayout(
context: DiffContext,
base: CompositeListValue,
model: CompositeListValue,
listContext: CompositeListLayoutChange[],
listContext?: CompositeListLayoutChange[],
): CompositeListLayoutDifference | undefined | "BREAK" {
// What constitutes layout change?
// - Change of "keyed object" position in the list
Expand All @@ -496,7 +499,6 @@ function diffCompositeListLayout(
const baseChunk = Value.isAggregate(base) ? base.chunks[0] : base;
const modelChunk = Value.isAggregate(model) ? model.chunks[0] : model;
const unusedBaseIndixes = new Set<number>();
const itemChanges: CompositeListLayoutChange[] = [];
for (let i = 0; i < baseLen; i++) {
unusedBaseIndixes.add(i);
}
Expand Down Expand Up @@ -524,7 +526,7 @@ function diffCompositeListLayout(
// Fast-path: no layout difference found
if (firstDirtyIndex === -1) {
for (const index of unusedBaseIndixes) {
listContext.push(
listContext?.push(
LayoutChange.createItemRemoved(index, baseChunk.data[index], env),
);
}
Expand All @@ -548,7 +550,7 @@ function diffCompositeListLayout(
if (modelChunk.data[i] === null) {
layout.push(null);
if (baseChunk.data[i] !== null) {
itemChanges.push(LayoutChange.createItemAdded(i, null, env));
listContext?.push(LayoutChange.createItemAdded(i, null, env));
}
continue;
}
Expand All @@ -567,7 +569,7 @@ function diffCompositeListLayout(
layout.push(baseIndex);
unusedBaseIndixes.delete(baseIndex);
if (i !== baseIndex) {
listContext.push(
listContext?.push(
LayoutChange.createIndexChange(
i,
baseIndex,
Expand Down Expand Up @@ -595,7 +597,7 @@ function diffCompositeListLayout(
}

for (const oldIndex of unusedBaseIndixes) {
listContext.push(
listContext?.push(
LayoutChange.createItemRemoved(oldIndex, baseChunk.data[oldIndex], env),
);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/apollo-forest-run/src/diff/difference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export function createCompositeListDifference(): CompositeListDifference {
itemQueue: new Set(),
dirtyItems: undefined,
layout: undefined,
itemsChanges: [],
itemsChanges: undefined,
errors: undefined,
};
}
Expand Down
2 changes: 1 addition & 1 deletion packages/apollo-forest-run/src/diff/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ export type CompositeListDifference = {
itemState: Map<number, ValueDifference>;
dirtyItems?: Set<number>;
layout?: CompositeListLayoutDifference;
itemsChanges: CompositeListLayoutChange[];
itemsChanges: CompositeListLayoutChange[] | undefined;
errors?: DiffError[];
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1662,12 +1662,12 @@ describe("change reporting", () => {

const [[first, firstFields], [second, secondFields]] = changes.entries();
expect(firstFields?.length).toEqual(1);
expect(firstFields?.[0]?.name).toEqual("foo");
expect(first.data).toBe(base.plainObject);
expect(firstFields?.[0]?.fieldInfo.name).toEqual("scalar");
expect(first.data).toBe(base);

expect(secondFields?.length).toEqual(1);
expect(secondFields?.[0]?.name).toEqual("scalar");
expect(second.data).toBe(base);
expect(secondFields?.[0]?.fieldInfo.name).toEqual("foo");
expect(second.data).toBe(base.plainObject);
});

test("does not report parent as changed on nested chunk change", () => {
Expand All @@ -1683,7 +1683,7 @@ describe("change reporting", () => {

const [[first, firstFields]] = changes.entries();
expect(firstFields?.length).toEqual(1);
expect(firstFields?.[0]?.name).toEqual("foo");
expect(firstFields?.[0]?.fieldInfo.name).toEqual("foo");
expect(first.data).toBe(base.plainObject);
});

Expand All @@ -1710,7 +1710,7 @@ describe("change reporting", () => {
// Expected to report as a change in parent object field
const [[first, firstFields]] = changes.entries();
expect(firstFields?.length).toEqual(1);
expect(firstFields?.[0]?.name).toEqual("scalarList");
expect(firstFields?.[0]?.fieldInfo.name).toEqual("scalarList");
expect(first.data).toBe(base);
});
});
Expand Down Expand Up @@ -1998,6 +1998,8 @@ function prepareDiffTrees(
const env: DiffEnv & ForestEnv = {
objectKey: (obj) => obj.id as string,
logUpdateStats: false,
enableHistory: false,
enableDataHistory: false,
...(testEnv ?? {}),
};
const baseTree = isTree(base)
Expand Down
7 changes: 2 additions & 5 deletions packages/apollo-forest-run/src/forest/updateObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,6 @@ export function recordFieldChange(
updated: SourceValue | undefined,
): void {
const { enableHistory, enableDataHistory } = context.env;
if (!enableHistory) {
return;
}

let changes = context.changes.get(base);
if (!changes) {
Expand Down Expand Up @@ -276,7 +273,7 @@ function updateCompositeListValue(
assert(newValue.data);
accumulateMissingFields(context, newValue);
result[i] = newValue.data;
arrayChanges.push(
arrayChanges?.push(
LayoutChange.createItemAdded(
i,
newValue.data,
Expand Down Expand Up @@ -307,7 +304,7 @@ function updateCompositeListValue(
if (copy.length !== base.data.length) {
dirty = true;
}
context.childChanges = arrayChanges;
context.childChanges = arrayChanges ?? [];

return copy ?? base.data;
}
Expand Down
Loading