Skip to content

Commit

Permalink
optimistic implementation (wrong) for updateQueries
Browse files Browse the repository at this point in the history
  • Loading branch information
Slava committed Jul 18, 2016
1 parent e3e4a6a commit e69d151
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
31 changes: 21 additions & 10 deletions src/QueryManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import {
GraphQLResult,
Document,
FragmentDefinition,
OperationDefinition,
} from 'graphql';

import { print } from 'graphql-tag/printer';
Expand Down Expand Up @@ -283,7 +284,7 @@ export class QueryManager {
public mutate({
mutation,
variables,
resultBehaviors,
resultBehaviors = [],
fragments = [],
optimisticResponse,
updateQueries,
Expand Down Expand Up @@ -316,6 +317,9 @@ export class QueryManager {
operationName: getOperationName(mutation),
} as Request;

const additionalResultBehaviors = !optimisticResponse ? [] :
this.collectResultBehaviorsFromReducers(updateQueries, { data: optimisticResponse }, true);

this.store.dispatch({
type: 'APOLLO_MUTATION_INIT',
mutationString,
Expand All @@ -328,20 +332,19 @@ export class QueryManager {
mutationId,
fragmentMap: queryFragmentMap,
optimisticResponse,
resultBehaviors,
resultBehaviors: [...resultBehaviors, ...additionalResultBehaviors],
});

return this.networkInterface.query(request)
.then((result) => {
const additionalResultBehaviors =
this.collectResultBehaviorsFromReducers(updateQueries, result);
resultBehaviors = resultBehaviors || [];
resultBehaviors = [...resultBehaviors, ...additionalResultBehaviors];
this.store.dispatch({
type: 'APOLLO_MUTATION_RESULT',
result,
mutationId,
resultBehaviors,
resultBehaviors: [
...resultBehaviors,
...this.collectResultBehaviorsFromReducers(updateQueries, result),
],
});

return result;
Expand All @@ -351,7 +354,6 @@ export class QueryManager {
type: 'APOLLO_MUTATION_ERROR',
error: err,
mutationId,
resultBehaviors,
});

return Promise.reject(err);
Expand Down Expand Up @@ -591,7 +593,7 @@ export class QueryManager {
});
}

private collectResultBehaviorsFromReducers(updateQueries, mutationResult) {
private collectResultBehaviorsFromReducers(updateQueries, mutationResult, isOptimistic = false) {
if (!updateQueries) {
return [];
}
Expand All @@ -617,7 +619,16 @@ export class QueryManager {

queries.forEach((observableQuery) => {
const queryOptions = observableQuery.options;
const previousResult = this.queryResults[observableQuery.queryId].data;
const queryDefinition: OperationDefinition = getQueryDefinition(queryOptions.query);
const previousResult = readSelectionSetFromStore({
store: isOptimistic ? this.getDataWithOptimisticResults() : this.getApolloState().data,
rootId: 'ROOT_QUERY',
selectionSet: queryDefinition.selectionSet,
variables: queryOptions.variables,
returnPartialData: queryOptions.returnPartialData || queryOptions.noFetch,
fragmentMap: createFragmentMap(queryOptions.fragments || []),
});

additionalResultBehaviors.push({
type: 'QUERY_RESULT',
newResult: reducer(previousResult, {
Expand Down
1 change: 0 additions & 1 deletion src/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ export interface MutationErrorAction {
type: 'APOLLO_MUTATION_ERROR';
error: Error;
mutationId: string;
resultBehaviors?: MutationBehavior[];
};

export function isMutationErrorAction(action: ApolloAction): action is MutationErrorAction {
Expand Down

0 comments on commit e69d151

Please sign in to comment.