Skip to content

Commit

Permalink
Merge pull request #6859 from apollographql/writeFragment-identity-error
Browse files Browse the repository at this point in the history
Throw if writeFragment cannot identify options.data when no options.id provided.
  • Loading branch information
benjamn authored Aug 21, 2020
2 parents d89d728 + ea5a356 commit 766d3ff
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 8 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
- Implement `useReactiveVar` hook for consuming reactive variables in React components. <br/>
[@benjamn](https://github.com/benjamn) in [#6867](https://github.com/apollographql/apollo-client/pull/6867)

- Throw if `writeFragment` cannot identify `options.data` when no `options.id` provided. <br/>
[@jcreighton](https://github.com/jcreighton) in [#6859](https://github.com/apollographql/apollo-client/pull/6859)

## Apollo Client 3.1.3

## Bug Fixes
Expand Down
13 changes: 13 additions & 0 deletions src/cache/inmemory/__tests__/writeToStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2325,4 +2325,17 @@ describe('writing to the store', () => {
},
});
});

it("should warn if it cannot identify the result object", () => {
const cache = new InMemoryCache;

expect(() => {
cache.writeFragment({
fragment: gql`fragment Count on Counter { count }`,
data: {
count: 1,
},
});
}).toThrowError(/Could not identify object/);
});
});
15 changes: 7 additions & 8 deletions src/cache/inmemory/writeToStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export class StoreWriter {
...variables,
};

const objOrRef = this.processSelectionSet({
const ref = this.processSelectionSet({
result: result || Object.create(null),
dataId,
selectionSet: operationDefinition.selectionSet,
Expand All @@ -105,15 +105,14 @@ export class StoreWriter {
},
});

const ref = isReference(objOrRef) ? objOrRef :
dataId && makeReference(dataId) || void 0;

if (ref) {
// Any IDs written explicitly to the cache (including ROOT_QUERY,
// most frequently) will be retained as reachable root IDs.
store.retain(ref.__ref);
if (!isReference(ref)) {
throw new InvariantError(`Could not identify object ${JSON.stringify(result)}`);
}

// Any IDs written explicitly to the cache (including ROOT_QUERY,
// most frequently) will be retained as reachable root IDs.
store.retain(ref.__ref);

return ref;
}

Expand Down

0 comments on commit 766d3ff

Please sign in to comment.