Skip to content

Commit 591bf4a

Browse files
committed
fix(web): refetch notifications for sidebar when new notifications arrive
1 parent 557cedd commit 591bf4a

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

web/helpers/apollo-cache/index.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { InMemoryCache, type InMemoryCacheConfig } from '@apollo/client/core/index.js';
2+
import type { NotificationOverview } from '~/composables/gql/graphql';
23
import { NotificationType } from '../../composables/gql/typename';
34
import { mergeAndDedup } from './merge';
45

@@ -56,6 +57,34 @@ const defaultCacheConfig: InMemoryCacheConfig = {
5657
});
5758
},
5859
},
60+
overview: {
61+
/**
62+
* Busts notification cache when new unread notifications are detected.
63+
*
64+
* This allows incoming notifications to appear in the sidebar without reloading the page.
65+
*
66+
* @param existing - Existing overview data in cache
67+
* @param incoming - New overview data from server
68+
* @param context - Apollo context containing cache instance
69+
* @returns The overview data to be cached
70+
*/
71+
merge(
72+
existing: Partial<NotificationOverview> | undefined,
73+
incoming: Partial<NotificationOverview> | undefined,
74+
{ cache }
75+
) {
76+
const hasNewUnreads =
77+
isDefined(existing?.unread?.total) &&
78+
isDefined(incoming?.unread?.total) &&
79+
existing.unread.total < incoming.unread.total;
80+
81+
if (hasNewUnreads) {
82+
cache.evict({ fieldName: 'notifications' });
83+
cache.gc();
84+
}
85+
return incoming;
86+
},
87+
},
5988
},
6089
},
6190
Mutation: {

0 commit comments

Comments
 (0)