Skip to content

Commit

Permalink
Remove mutation tracking from the store
Browse files Browse the repository at this point in the history
  • Loading branch information
shadaj committed Jul 13, 2017
1 parent d741820 commit 1e6e7e2
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 90 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Change log

### vNEXT
- Remove mutation tracking from the Redux store. Mutation status tracking is now handled outside of Redux in the MutationStore class. [PR #1846](https://github.com/apollographql/apollo-client/pull/1846)

### 1.8.0
- Add the `filter` argument to the `@connection` directive so that custom store keys can include query arguments [PR #1862](https://github.com/apollographql/apollo-client/pull/1862)
Expand Down
13 changes: 13 additions & 0 deletions src/core/QueryManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ import {
MutationQueryReducer,
} from '../data/mutationResults';

import {
MutationStore,
} from '../mutations/store';

import {
QueryScheduler,
} from '../scheduler/scheduler';
Expand Down Expand Up @@ -137,6 +141,7 @@ export class QueryManager {
public store: ApolloStore;
public networkInterface: NetworkInterface;
public ssrMode: boolean;
public mutationStore: MutationStore = new MutationStore();

private addTypename: boolean;
private deduplicator: Deduplicator;
Expand Down Expand Up @@ -313,6 +318,8 @@ export class QueryManager {
update: updateWithProxyFn,
});

this.mutationStore.initMutation(mutationId, mutationString, variables);

return new Promise<ExecutionResult>((resolve, reject) => {
this.networkInterface.query(request)
.then((result) => {
Expand All @@ -326,6 +333,8 @@ export class QueryManager {
mutationId,
});

this.mutationStore.markMutationError(mutationId, error);

delete this.queryDocuments[mutationId];
reject(error);
return;
Expand All @@ -343,6 +352,8 @@ export class QueryManager {
update: updateWithProxyFn,
});

this.mutationStore.markMutationResult(mutationId);

// If there was an error in our reducers, reject this promise!
const { reducerError } = this.getApolloState();
if (reducerError && reducerError.mutationId === mutationId) {
Expand Down Expand Up @@ -837,6 +848,8 @@ export class QueryManager {
observableQueryIds: Object.keys(this.observableQueries),
});

this.mutationStore.reset();

// Similarly, we have to have to refetch each of the queries currently being
// observed. We refetch instead of error'ing on these since the assumption is that
// resetting the store doesn't eliminate the need for the queries currently being
Expand Down
86 changes: 27 additions & 59 deletions src/mutations/store.ts
Original file line number Diff line number Diff line change
@@ -1,70 +1,38 @@
import {
ApolloAction,
isMutationInitAction,
isMutationResultAction,
isMutationErrorAction,
isStoreResetAction,
} from '../actions';
export class MutationStore {
private store: {[mutationId: string]: MutationStoreValue} = {};

import {
SelectionSetNode,
} from 'graphql';

export interface MutationStore {
[mutationId: string]: MutationStoreValue;
}

export interface MutationStoreValue {
mutationString: string;
variables: Object;
loading: boolean;
error: Error | null;
}

export interface SelectionSetWithRoot {
id: string;
typeName: string;
selectionSet: SelectionSetNode;
}

export function mutations(
previousState: MutationStore = {},
action: ApolloAction,
): MutationStore {
if (isMutationInitAction(action)) {
const newState = { ...previousState } as MutationStore;
public get(mutationId: string): MutationStoreValue {
return this.store[mutationId];
}

newState[action.mutationId] = {
mutationString: action.mutationString,
variables: action.variables,
public initMutation(mutationId: string, mutationString: string, variables: Object | undefined) {
this.store[mutationId] = {
mutationString: mutationString,
variables: variables || {},
loading: true,
error: null,
};
}

return newState;
} else if (isMutationResultAction(action)) {
const newState = { ...previousState } as MutationStore;

newState[action.mutationId] = {
...previousState[action.mutationId],
loading: false,
error: null,
} as MutationStoreValue;
public markMutationError(mutationId: string, error: Error) {
this.store[mutationId].loading = false;
this.store[mutationId].error = error;
}

return newState;
} else if (isMutationErrorAction(action)) {
const newState = { ...previousState } as MutationStore;
public markMutationResult(mutationId: string) {
this.store[mutationId].loading = false;
this.store[mutationId].error = null;
}

newState[action.mutationId] = {
...previousState[action.mutationId],
loading: false,
error: action.error,
} as MutationStoreValue;
} else if (isStoreResetAction(action)) {
// if we are resetting the store, we no longer need information about the mutations
// that are currently in the store so we can just throw them all away.
return {};
public reset() {
this.store = {};
}
}

return previousState;
export interface MutationStoreValue {
mutationString: string;
variables: Object;
loading: boolean;
error: Error | null;
}

5 changes: 1 addition & 4 deletions src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
} from './queries/store';

import {
mutations,
// mutations,
MutationStore,
} from './mutations/store';

Expand Down Expand Up @@ -62,7 +62,6 @@ export interface ReducerError {
export interface Store {
data: NormalizedCache;
queries: QueryStore;
mutations: MutationStore;
optimistic: OptimisticStore;
reducerError: ReducerError | null;
}
Expand Down Expand Up @@ -110,7 +109,6 @@ export function createApolloReducer(config: ApolloReducerConfig): (state: Store,
try {
const newState: Store = {
queries: queries(state.queries, action),
mutations: mutations(state.mutations, action),

data: data(state.data, action, config),
optimistic: [] as any,
Expand All @@ -134,7 +132,6 @@ export function createApolloReducer(config: ApolloReducerConfig): (state: Store,
);

if (state.data === newState.data &&
state.mutations === newState.mutations &&
state.queries === newState.queries &&
state.optimistic === newState.optimistic &&
state.reducerError === newState.reducerError) {
Expand Down
1 change: 0 additions & 1 deletion test/QueryManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2442,7 +2442,6 @@ describe('QueryManager', () => {
const currentState = queryManager.getApolloState();
const expectedState: any = {
data: {},
mutations: {},
queries: {},
optimistic: [],
reducerError: null,
Expand Down
2 changes: 0 additions & 2 deletions test/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,6 @@ describe('client', () => {
{
apollo: {
queries: {},
mutations: {},
data: {},
optimistic: [],
reducerError: null,
Expand Down Expand Up @@ -588,7 +587,6 @@ describe('client', () => {
metadata: null,
},
},
mutations: {},
reducerError: null,
}) };

Expand Down
36 changes: 18 additions & 18 deletions test/optimistic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,9 +318,9 @@ describe('optimistic mutation results', () => {
updateQueries,
}).then((res) => {
checkBothMutationsAreApplied('This one was created with a mutation.', 'Optimistically generated 2');
const latestState = client.store.getState().apollo.mutations;
assert.equal(latestState['5'].loading, false);
assert.equal(latestState['6'].loading, true);
const latestState = client.queryManager.mutationStore;
assert.equal(latestState.get('5').loading, false);
assert.equal(latestState.get('6').loading, true);

return res;
});
Expand All @@ -331,16 +331,16 @@ describe('optimistic mutation results', () => {
updateQueries,
}).then((res) => {
checkBothMutationsAreApplied('This one was created with a mutation.', 'Second mutation.');
const latestState = client.store.getState().apollo.mutations;
assert.equal(latestState[5].loading, false);
assert.equal(latestState[6].loading, false);
const latestState = client.queryManager.mutationStore;
assert.equal(latestState.get('5').loading, false);
assert.equal(latestState.get('6').loading, false);

return res;
});

const mutationsState = client.store.getState().apollo.mutations;
assert.equal(mutationsState[5].loading, true);
assert.equal(mutationsState[6].loading, true);
const mutationsState = client.queryManager.mutationStore;
assert.equal(mutationsState.get('5').loading, true);
assert.equal(mutationsState.get('6').loading, true);

checkBothMutationsAreApplied('Optimistically generated', 'Optimistically generated 2');

Expand Down Expand Up @@ -486,9 +486,9 @@ describe('optimistic mutation results', () => {
update,
}).then((res) => {
checkBothMutationsAreApplied('This one was created with a mutation.', 'Optimistically generated 2');
const latestState = client.store.getState().apollo.mutations;
assert.equal(latestState['5'].loading, false);
assert.equal(latestState['6'].loading, true);
const latestState = client.queryManager.mutationStore;
assert.equal(latestState.get('5').loading, false);
assert.equal(latestState.get('6').loading, true);

return res;
});
Expand All @@ -499,16 +499,16 @@ describe('optimistic mutation results', () => {
update,
}).then((res) => {
checkBothMutationsAreApplied('This one was created with a mutation.', 'Second mutation.');
const latestState = client.store.getState().apollo.mutations;
assert.equal(latestState[5].loading, false);
assert.equal(latestState[6].loading, false);
const latestState = client.queryManager.mutationStore;
assert.equal(latestState.get('5').loading, false);
assert.equal(latestState.get('6').loading, false);

return res;
});

const mutationsState = client.store.getState().apollo.mutations;
assert.equal(mutationsState[5].loading, true);
assert.equal(mutationsState[6].loading, true);
const mutationsState = client.queryManager.mutationStore;
assert.equal(mutationsState.get('5').loading, true);
assert.equal(mutationsState.get('6').loading, true);

checkBothMutationsAreApplied('Optimistically generated', 'Optimistically generated 2');

Expand Down
6 changes: 0 additions & 6 deletions test/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ describe('createApolloStore', () => {
store.getState()['apollo'],
{
queries: {},
mutations: {},
data: {},
optimistic: [],
reducerError: null,
Expand All @@ -39,7 +38,6 @@ describe('createApolloStore', () => {
store.getState()['test'],
{
queries: {},
mutations: {},
data: {},
optimistic: [],
reducerError: null,
Expand All @@ -64,7 +62,6 @@ describe('createApolloStore', () => {
assert.deepEqual(store.getState(), {
apollo: {
queries: {},
mutations: {},
data: initialState.apollo.data,
optimistic: initialState.apollo.optimistic,
reducerError: null,
Expand Down Expand Up @@ -119,7 +116,6 @@ describe('createApolloStore', () => {

const emptyState: Store = {
queries: { },
mutations: { },
data: { },
optimistic: ([] as any[]),
reducerError: null,
Expand Down Expand Up @@ -163,7 +159,6 @@ describe('createApolloStore', () => {
'metadata': null,
},
},
mutations: {},
data: {},
optimistic: ([] as any[]),
reducerError: null,
Expand Down Expand Up @@ -238,7 +233,6 @@ describe('createApolloStore', () => {

const resetState = {
queries: {},
mutations: {},
data: {},
optimistic: [
{
Expand Down

0 comments on commit 1e6e7e2

Please sign in to comment.