Skip to content

Commit

Permalink
Prevent flushed results from clearing non-optimistic results
Browse files Browse the repository at this point in the history
Previously we called clearLayer when clearing an optimistic
mutation result batch, which may erase existing mutation
result data. This shouldn't be possible but we can account
for duplicate mutation results this way and also stop
exposing clearLayer.
  • Loading branch information
kitten committed Apr 24, 2020
1 parent e373675 commit 398254f
Showing 6 changed files with 9 additions and 9 deletions.
5 changes: 2 additions & 3 deletions exchanges/graphcache/src/cacheExchange.ts
Original file line number Diff line number Diff line change
@@ -26,10 +26,9 @@ import {
} from 'wonka';

import { query, write, writeOptimistic } from './operations';
import { hydrateData, clearLayer } from './store/data';
import { makeDict, isDictEmpty } from './helpers/dict';
import { filterVariables, getMainOperation } from './ast';
import { Store, noopDataState, reserveLayer } from './store';
import { Store, noopDataState, hydrateData, reserveLayer } from './store';

import {
UpdatesConfig,
@@ -404,7 +403,7 @@ export const cacheExchange = (opts?: CacheExchangeOpts): Exchange => ({
}

for (let i = 0; i < mutationResultBuffer.length; i++) {
clearLayer(store.data, mutationResultBuffer[i].operation.key);
reserveLayer(store.data, mutationResultBuffer[i].operation.key);
}

for (const dep in blockedDependencies) {
4 changes: 2 additions & 2 deletions exchanges/graphcache/src/store/data.test.ts
Original file line number Diff line number Diff line change
@@ -87,10 +87,10 @@ describe('garbage collection', () => {

expect(InMemoryData.readRecord('Todo:1', 'id')).toBe('1');

InMemoryData.clearLayer(data, 1);
InMemoryData.reserveLayer(data, 1);
InMemoryData.gc();
expect(InMemoryData.readRecord('Todo:1', 'id')).toBe(undefined);

expect(InMemoryData.readRecord('Todo:1', 'id')).toBe(undefined);
expect(InMemoryData.getCurrentDependencies()).toEqual({
'Query.todo': true,
'Todo:1': true,
2 changes: 1 addition & 1 deletion exchanges/graphcache/src/store/data.ts
Original file line number Diff line number Diff line change
@@ -490,7 +490,7 @@ const createLayer = (data: InMemoryData, layerKey: number) => {
};

/** Clears all links and records of an optimistic layer */
export const clearLayer = (data: InMemoryData, layerKey: number) => {
const clearLayer = (data: InMemoryData, layerKey: number) => {
if (data.refLock[layerKey]) {
delete data.refLock[layerKey];
delete data.records.optimistic[layerKey];
2 changes: 1 addition & 1 deletion exchanges/graphcache/src/store/index.ts
Original file line number Diff line number Diff line change
@@ -3,8 +3,8 @@ export {
clearDataState,
noopDataState,
reserveLayer,
clearLayer,
getCurrentDependencies,
hydrateData,
} from './data';

export * from './keys';
3 changes: 2 additions & 1 deletion exchanges/graphcache/src/store/store.test.ts
Original file line number Diff line number Diff line change
@@ -469,7 +469,8 @@ describe('Store with OptimisticMutationConfig', () => {
],
});

InMemoryData.clearLayer(store.data, 1);
InMemoryData.noopDataState(store.data, 1);

({ data } = query(store, { query: Todos }));
expect(data).toEqual({
__typename: 'Query',
2 changes: 1 addition & 1 deletion exchanges/graphcache/src/test-utils/examples-1.test.ts
Original file line number Diff line number Diff line change
@@ -433,7 +433,7 @@ it('correctly resolves optimistic updates on Relay schemas', () => {

write(store, { query: getRoot }, queryData);
writeOptimistic(store, { query: updateItem, variables: { id: '2' } }, 1);
InMemoryData.clearLayer(store.data, 1);
InMemoryData.noopDataState(store.data, 1);
const queryRes = query(store, { query: getRoot });

expect(queryRes.partial).toBe(false);

0 comments on commit 398254f

Please sign in to comment.