From c4de70f9e6210ff6361f3c03aee622e73c061b28 Mon Sep 17 00:00:00 2001 From: Ivan Artemiev <29709626+iartemiev@users.noreply.github.com> Date: Fri, 12 Mar 2021 10:03:23 -0500 Subject: [PATCH] fix(@aws-amplify/datastore): handle sync query unauthorized --- .../datastore/src/sync/processors/sync.ts | 36 +++++++++++++------ 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/packages/datastore/src/sync/processors/sync.ts b/packages/datastore/src/sync/processors/sync.ts index 7d660ce66b7..a52a5e13656 100644 --- a/packages/datastore/src/sync/processors/sync.ts +++ b/packages/datastore/src/sync/processors/sync.ts @@ -142,14 +142,14 @@ class SyncProcessor { variables, }); } catch (error) { - if (this.partialDataFeatureFlagEnabled()) { - const hasItems = Boolean( - error && - error.data && - error.data[opName] && - error.data[opName].items - ); + const hasItems = Boolean( + error && + error.data && + error.data[opName] && + error.data[opName].items + ); + if (this.partialDataFeatureFlagEnabled()) { if (hasItems) { const result = error; result.data[opName].items = result.data[opName].items.filter( @@ -178,12 +178,26 @@ class SyncProcessor { ); if (unauthorized) { const result = error; - result.data[opName].items = result.data[opName].items.filter( - item => item !== null - ); + + const opResultDefaults = { + items: [], + nextToken: null, + startedAt: null, + }; + + if (hasItems) { + result.data[opName].items = result.data[opName].items.filter( + item => item !== null + ); + } else { + result.data[opName] = { + ...opResultDefaults, + ...result.data[opName], + }; + } logger.warn( 'queryError', - 'User is unauthorized, some items could not be returned.' + `User is unauthorized to query ${opName}, some items could not be returned.` ); return result; } else {