Skip to content

Commit

Permalink
Avoid declaring handleMissing function in execField method.
Browse files Browse the repository at this point in the history
  • Loading branch information
benjamn committed Oct 24, 2018
1 parent ca65ff6 commit 523dd2d
Showing 1 changed file with 32 additions and 29 deletions.
61 changes: 32 additions & 29 deletions packages/apollo-cache-inmemory/src/readFromStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -401,31 +401,15 @@ export class StoreReader {
info,
);

function handleMissing<T>(res: ExecResult<T>): ExecResult<T> {
let missing: ExecResultMissingField[] = null;

if (readStoreResult.missing) {
missing = missing || [];
missing.push(...readStoreResult.missing);
}

if (res.missing) {
missing = missing || [];
missing.push(...res.missing);
}

return {
result: res.result,
missing,
};
}

if (Array.isArray(readStoreResult.result)) {
return handleMissing(this.executeSubSelectedArray(
field,
readStoreResult.result,
execContext,
));
return this.combineExecResults(
readStoreResult,
this.executeSubSelectedArray(
field,
readStoreResult.result,
execContext,
),
);
}

// Handle all scalar types here
Expand All @@ -442,11 +426,30 @@ export class StoreReader {
}

// Returned value is an object, and the query has a sub-selection. Recurse.
return handleMissing(this.executeSelectionSet({
selectionSet: field.selectionSet,
rootValue: readStoreResult.result,
execContext,
}));
return this.combineExecResults(
readStoreResult,
this.executeSelectionSet({
selectionSet: field.selectionSet,
rootValue: readStoreResult.result,
execContext,
}),
);
}

private combineExecResults<T>(
...execResults: ExecResult<T>[]
): ExecResult<T> {
let missing: ExecResultMissingField[] = null;
execResults.forEach(execResult => {
if (execResult.missing) {
missing = missing || [];
missing.push(...execResult.missing);
}
});
return {
result: execResults.pop().result,
missing,
};
}

private executeSubSelectedArray(
Expand Down

0 comments on commit 523dd2d

Please sign in to comment.