From 583f62b6476a02969985f31e670d1358a313ecd8 Mon Sep 17 00:00:00 2001 From: Jenn Creighton Date: Tue, 18 Aug 2020 16:00:03 -0400 Subject: [PATCH 1/4] Throw when writeFragment cannot identify object --- src/cache/inmemory/__tests__/writeToStore.ts | 13 +++++++++++++ src/cache/inmemory/writeToStore.ts | 4 ++++ 2 files changed, 17 insertions(+) diff --git a/src/cache/inmemory/__tests__/writeToStore.ts b/src/cache/inmemory/__tests__/writeToStore.ts index 9e39a45b9b2..5cb33f81a58 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("writeFragment 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(/writeFragment could not identify object/); + }); }); diff --git a/src/cache/inmemory/writeToStore.ts b/src/cache/inmemory/writeToStore.ts index fea1d64bf5c..6a5ca420a7a 100644 --- a/src/cache/inmemory/writeToStore.ts +++ b/src/cache/inmemory/writeToStore.ts @@ -105,6 +105,10 @@ export class StoreWriter { }, }); + if (typeof dataId === 'undefined' && !isReference(objOrRef)) { + throw new InvariantError("writeFragment could not identify object"); + } + const ref = isReference(objOrRef) ? objOrRef : dataId && makeReference(dataId) || void 0; From 112228f436ccc71ea227cc67ebfba2ccf6d98981 Mon Sep 17 00:00:00 2001 From: Jenn Creighton Date: Thu, 20 Aug 2020 19:19:03 -0400 Subject: [PATCH 2/4] Warn if object cannot be identified --- src/cache/inmemory/__tests__/writeToStore.ts | 4 ++-- src/cache/inmemory/writeToStore.ts | 17 ++++++----------- 2 files changed, 8 insertions(+), 13 deletions(-) diff --git a/src/cache/inmemory/__tests__/writeToStore.ts b/src/cache/inmemory/__tests__/writeToStore.ts index 5cb33f81a58..aae63df4703 100644 --- a/src/cache/inmemory/__tests__/writeToStore.ts +++ b/src/cache/inmemory/__tests__/writeToStore.ts @@ -2326,7 +2326,7 @@ describe('writing to the store', () => { }); }); - it("writeFragment should warn if it cannot identify the result object", () => { + it("should warn if it cannot identify the result object", () => { const cache = new InMemoryCache; expect(() => { @@ -2336,6 +2336,6 @@ describe('writing to the store', () => { count: 1, }, }); - }).toThrowError(/writeFragment could not identify object/); + }).toThrowError(/Could not identify object/); }); }); diff --git a/src/cache/inmemory/writeToStore.ts b/src/cache/inmemory/writeToStore.ts index 6a5ca420a7a..486dc7d63fb 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,18 +105,13 @@ export class StoreWriter { }, }); - if (typeof dataId === 'undefined' && !isReference(objOrRef)) { - throw new InvariantError("writeFragment could not identify object"); + if (!isReference(ref)) { + throw new InvariantError("Could not identify object"); } - 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); - } + // 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; } From a2f90dfc9cd63fa050c3c359e43e2fc9d33d152a Mon Sep 17 00:00:00 2001 From: Jenn Creighton Date: Fri, 21 Aug 2020 11:44:56 -0400 Subject: [PATCH 3/4] Add stringified result to error message --- src/cache/inmemory/writeToStore.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cache/inmemory/writeToStore.ts b/src/cache/inmemory/writeToStore.ts index 486dc7d63fb..25feb11be28 100644 --- a/src/cache/inmemory/writeToStore.ts +++ b/src/cache/inmemory/writeToStore.ts @@ -106,7 +106,7 @@ export class StoreWriter { }); if (!isReference(ref)) { - throw new InvariantError("Could not identify object"); + throw new InvariantError(`Could not identify object ${JSON.stringify(result)}`); } // Any IDs written explicitly to the cache (including ROOT_QUERY, From ea5a3560bf936dbc7906d33c123672823341a371 Mon Sep 17 00:00:00 2001 From: Ben Newman Date: Fri, 21 Aug 2020 14:46:02 -0400 Subject: [PATCH 4/4] Mention PR #6859 in CHANGELOG.md. --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) 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