Skip to content

Commit

Permalink
Trigger a no-op if a field retrieval failure happens in mutation
Browse files Browse the repository at this point in the history
We avoid running the reducer on those cases

Fixes #647
  • Loading branch information
rricard committed Sep 15, 2016
1 parent 945cb72 commit 8ffdfad
Showing 1 changed file with 27 additions and 17 deletions.
44 changes: 27 additions & 17 deletions src/QueryManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -630,17 +630,24 @@ export class QueryManager {
fragments = getFragmentDefinitions(transformedDoc);
}

const previousResult = readSelectionSetFromStore({
// In case of an optimistic change, apply reducer on top of the
// results including previous optimistic updates. Otherwise, apply it
// on top of the real data only.
store: isOptimistic ? this.getDataWithOptimisticResults() : this.getApolloState().data,
rootId: 'ROOT_QUERY',
selectionSet: queryDefinition.selectionSet,
variables: queryOptions.variables,
returnPartialData: queryOptions.returnPartialData || queryOptions.noFetch,
fragmentMap: createFragmentMap(fragments || []),
});
let previousResult: any = null;
try {
previousResult = readSelectionSetFromStore({
// In case of an optimistic change, apply reducer on top of the
// results including previous optimistic updates. Otherwise, apply it
// on top of the real data only.
store: isOptimistic ? this.getDataWithOptimisticResults() : this.getApolloState().data,
rootId: 'ROOT_QUERY',
selectionSet: queryDefinition.selectionSet,
variables: queryOptions.variables,
returnPartialData: queryOptions.returnPartialData || queryOptions.noFetch,
fragmentMap: createFragmentMap(fragments || []),
});
} catch (e) {
if (!/can't find field/i.test(e.message)) {
throw e;
}
}

return {
previousResult,
Expand Down Expand Up @@ -676,12 +683,15 @@ export class QueryManager {
queryFragments,
} = this.getQueryWithPreviousResult(queryId, isOptimistic);

const newResult = tryFunctionOrLogError(() => reducer(
previousResult, {
mutationResult,
queryName,
queryVariables,
}));
let newResult: any = null;
if (previousResult) {
newResult = tryFunctionOrLogError(() => reducer(
previousResult, {
mutationResult,
queryName,
queryVariables,
}));
}

if (newResult) {
resultBehaviors.push({
Expand Down

0 comments on commit 8ffdfad

Please sign in to comment.