Skip to content

Commit

Permalink
Log non-fatal error when fields are missing from written results.
Browse files Browse the repository at this point in the history
Fixes #8331 and #6915, and should help with the underlying cause of
#7436 (comment)
  • Loading branch information
benjamn committed Jun 22, 2021
1 parent 72735a3 commit 39967b2
Showing 1 changed file with 20 additions and 11 deletions.
31 changes: 20 additions & 11 deletions src/cache/inmemory/writeToStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import {
Reference,
isReference,
shouldInclude,
hasDirectives,
cloneDeep,
addTypenameToDocument,
} from '../../utilities';

import { NormalizedCache, ReadMergeModifyContext, MergeTree } from './types';
Expand All @@ -44,6 +44,7 @@ export interface WriteContext extends ReadMergeModifyContext {
mergeTree: MergeTree;
selections: Set<SelectionNode>;
}>;
clientOnly: boolean;
};

interface ProcessSelectionSetOptions {
Expand Down Expand Up @@ -86,6 +87,7 @@ export class StoreWriter {
fragmentMap: createFragmentMap(getFragmentDefinitions(query)),
overwrite: !!overwrite,
incomingById: new Map,
clientOnly: false,
};

const ref = this.processSelectionSet({
Expand Down Expand Up @@ -231,7 +233,13 @@ export class StoreWriter {
const resultFieldKey = resultKeyNameFromField(selection);
const value = result[resultFieldKey];

if (typeof value !== 'undefined') {
const wasClientOnly = context.clientOnly;
context.clientOnly = wasClientOnly || !!(
selection.directives &&
selection.directives.some(d => d.name.value === "client")
);

if (value !== void 0) {
const storeFieldName = policies.getStoreFieldName({
typename,
fieldName: selection.name.value,
Expand Down Expand Up @@ -303,17 +311,18 @@ export class StoreWriter {
});

} else if (
policies.usingPossibleTypes &&
!hasDirectives(["defer", "client"], selection)
!context.clientOnly &&
!addTypenameToDocument.added(selection)
) {
throw new InvariantError(
`Missing field '${resultFieldKey}' in ${JSON.stringify(
result,
null,
2,
).substring(0, 100)}`,
);
invariant.error(`Missing field '${
resultKeyNameFromField(selection)
}' while writing result ${
JSON.stringify(result, null, 2)
}`.substring(0, 1000));
}

context.clientOnly = wasClientOnly;

} else {
// This is not a field, so it must be a fragment, either inline or named
const fragment = getFragmentFromSelection(
Expand Down

0 comments on commit 39967b2

Please sign in to comment.