Skip to content

Commit

Permalink
fix stacking optimistic mutations
Browse files Browse the repository at this point in the history
  • Loading branch information
Slava committed Jul 12, 2016
1 parent e55a9e6 commit 7e72e8e
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 40 deletions.
6 changes: 5 additions & 1 deletion src/optimistic-data/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@ import {

import {
getDataWithOptimisticResults,
Store,
} from '../store';

import assign = require('lodash.assign');

// Currently every OptimisticStore stack's element contains an entirely new copy of `data`
// This could be optimized with a copy-on-write data structure like immutable.js
export type OptimisticStore = {
Expand All @@ -38,8 +41,9 @@ export function optimistic(
resultBehaviors: action.resultBehaviors,
} as ApolloAction;

const fakeStore = assign({}, store, { optimistic: previousState }) as Store;
const fakeDataResultState = data(
getDataWithOptimisticResults(store),
getDataWithOptimisticResults(fakeStore),
fakeMutationResultAction,
store.queries,
store.mutations,
Expand Down
78 changes: 39 additions & 39 deletions test/optimistic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,45 @@ describe('optimistic mutation results', () => {
});

it('handles a single error for a single mutation', () => {
return setup({
request: { query: mutation },
error: new Error('forbidden (test error)'),
})
.then(() => {
const dataId = client.dataId({
__typename: 'TodoList',
id: '5',
});
const promise = client.mutate({
mutation,
optimisticResponse,
resultBehaviors: [
{
type: 'ARRAY_INSERT',
resultPath: [ 'createTodo' ],
storePath: [ dataId, 'todos' ],
where: 'PREPEND',
},
],
});

const dataInStore = client.queryManager.getDataWithOptimisticResults();
assert.equal((dataInStore['TodoList5'] as any).todos.length, 4);
assert.equal((dataInStore['Todo99'] as any).text, 'Optimistically generated');

return promise;
})
.catch((err) => {
assert.instanceOf(err, Error);
assert.equal(err.message, 'forbidden (test error)');

const dataInStore = client.queryManager.getDataWithOptimisticResults();
assert.equal((dataInStore['TodoList5'] as any).todos.length, 3);
assert.notProperty(dataInStore, 'Todo99');
});
});

it('handles errors produced by one mutation in a series', () => {
return setup({
request: { query: mutation },
error: new Error('forbidden (test error)'),
Expand Down Expand Up @@ -518,44 +557,5 @@ describe('optimistic mutation results', () => {
assert.notInclude((dataInStore['TodoList5'] as any).todos, 'Todo99');
});
});

it('handles errors produced by one mutation in a series', () => {
return setup({
request: { query: mutation },
error: new Error('forbidden (test error)'),
})
.then(() => {
const dataId = client.dataId({
__typename: 'TodoList',
id: '5',
});
const promise = client.mutate({
mutation,
optimisticResponse,
resultBehaviors: [
{
type: 'ARRAY_INSERT',
resultPath: [ 'createTodo' ],
storePath: [ dataId, 'todos' ],
where: 'PREPEND',
},
],
});

const dataInStore = client.queryManager.getDataWithOptimisticResults();
assert.equal((dataInStore['TodoList5'] as any).todos.length, 4);
assert.equal((dataInStore['Todo99'] as any).text, 'Optimistically generated');

return promise;
})
.catch((err) => {
assert.instanceOf(err, Error);
assert.equal(err.message, 'forbidden (test error)');

const dataInStore = client.queryManager.getDataWithOptimisticResults();
assert.equal((dataInStore['TodoList5'] as any).todos.length, 3);
assert.notProperty(dataInStore, 'Todo99');
});
});
});
});

0 comments on commit 7e72e8e

Please sign in to comment.