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 2215228
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 5 deletions.
5 changes: 2 additions & 3 deletions exchanges/graphcache/src/cacheExchange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion exchanges/graphcache/src/store/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
2 changes: 1 addition & 1 deletion exchanges/graphcache/src/store/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ export {
clearDataState,
noopDataState,
reserveLayer,
clearLayer,
getCurrentDependencies,
hydrateData,
} from './data';

export * from './keys';
Expand Down

0 comments on commit 2215228

Please sign in to comment.