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 5, 2017
1 parent ce2649a commit aea2442
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 78 deletions.
20 changes: 20 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 = {};

private addTypename: boolean;
private deduplicator: Deduplicator;
Expand Down Expand Up @@ -309,6 +314,13 @@ export class QueryManager {
update: updateWithProxyFn,
});

this.mutationStore[mutationId] = {
mutationString: mutationString,
variables: variables || {},
loading: true,
error: null,
};

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

this.mutationStore[mutationId].loading = false;
this.mutationStore[mutationId].error = error;

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

this.mutationStore[mutationId].loading = false;
this.mutationStore[mutationId].error = null;

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

this.mutationStore = {};

// 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
59 changes: 0 additions & 59 deletions src/mutations/store.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,3 @@
import {
ApolloAction,
isMutationInitAction,
isMutationResultAction,
isMutationErrorAction,
isStoreResetAction,
} from '../actions';

import {
SelectionSetNode,
} from 'graphql';

export interface MutationStore {
[mutationId: string]: MutationStoreValue;
}
Expand All @@ -21,50 +9,3 @@ export interface MutationStoreValue {
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;

newState[action.mutationId] = {
mutationString: action.mutationString,
variables: action.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;

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

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 {};
}

return previousState;
}
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 @@ -585,7 +584,6 @@ describe('client', () => {
metadata: null,
},
},
mutations: {},
reducerError: null,
}) };

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

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

return res;
});

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

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

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

return res;
});

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

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 aea2442

Please sign in to comment.