diff --git a/CHANGELOG.md b/CHANGELOG.md index cc6244d9387..123b4e76433 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,9 @@ - Implement `useReactiveVar` hook for consuming reactive variables in React components.
[@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.
+ [@jcreighton](https://github.com/jcreighton) in [#6859](https://github.com/apollographql/apollo-client/pull/6859) + ## Apollo Client 3.1.3 ## Bug Fixes diff --git a/src/cache/inmemory/__tests__/writeToStore.ts b/src/cache/inmemory/__tests__/writeToStore.ts index 9e39a45b9b2..aae63df4703 100644 --- a/src/cache/inmemory/__tests__/writeToStore.ts +++ b/src/cache/inmemory/__tests__/writeToStore.ts @@ -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/); + }); }); diff --git a/src/cache/inmemory/writeToStore.ts b/src/cache/inmemory/writeToStore.ts index fea1d64bf5c..25feb11be28 100644 --- a/src/cache/inmemory/writeToStore.ts +++ b/src/cache/inmemory/writeToStore.ts @@ -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, @@ -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; }