diff --git a/src/apollo.ts b/src/apollo.ts index e404401c..fa9739df 100644 --- a/src/apollo.ts +++ b/src/apollo.ts @@ -3,6 +3,8 @@ import { setContext } from '@apollo/client/link/context' import { persistCache } from 'apollo3-cache-persist' import { getAuth } from 'firebase/auth' +import type { SpeedResultsQueryVariables } from './graphql/generated/graphql' + const httpLink = createHttpLink({ uri: import.meta.env.VITE_GRAPHQL_URL }) @@ -22,6 +24,26 @@ const cache = new InMemoryCache({ User: { merge (existing, incoming, { mergeObjects }) { return mergeObjects(existing, incoming) + }, + fields: { + speedResults: { + keyArgs: false, + merge (existing: any[] = [], incoming: any[], { readField, args }) { + const merged = existing.slice(0) + const existingIds = new Set(merged.map(doc => readField('id', doc))) + + incoming = incoming.filter(doc => !existingIds.has(readField("id", doc))) + const afterIndex = merged.findIndex(doc => (args as SpeedResultsQueryVariables).startAfter === readField("createdAt", doc)) + + + if (afterIndex >= 0) { + merged.splice(afterIndex + 1, 0, ...incoming) + } else { + merged.push(...incoming) + } + return merged + } + } } } } diff --git a/src/components/NavHeader.vue b/src/components/NavHeader.vue index 4e59ec55..302d4ee8 100644 --- a/src/components/NavHeader.vue +++ b/src/components/NavHeader.vue @@ -7,7 +7,7 @@