From f6f4be6a492d08a7496c8f1520bab5be37c4c63c Mon Sep 17 00:00:00 2001 From: Hossein Mehrabi Date: Tue, 23 Jan 2024 19:27:54 +0330 Subject: [PATCH] fix: clear apollo cache upon webhook calls --- src/services/strapi/strapi.service.ts | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/services/strapi/strapi.service.ts b/src/services/strapi/strapi.service.ts index 6afa759..b3bf6c4 100644 --- a/src/services/strapi/strapi.service.ts +++ b/src/services/strapi/strapi.service.ts @@ -10,14 +10,17 @@ import { PostFiltersInput, SearchPostsDocument, } from '../../lib/strapi/strapi.generated' +import { getWebhookData } from '../../pages/api/webhook' import { ApiResponse } from '../../types/data.types' import { LPE } from '../../types/lpe.types' +import { isVercel } from '../../utils/env.utils' import { settle } from '../../utils/promise.utils' import { strapiTransformers } from './transformers/strapi.transformers' export class StrapiService { client: ApolloClient = null as any axios: Axios = null as any + lastUpdate = 0 constructor(apiUrl: string, graphqlUrl: string, apiKey: string) { this.axios = axios.create({ @@ -68,6 +71,26 @@ export class StrapiService { Authorization: `Bearer ${apiKey}`, }, }) + + if (!isVercel() && process.env.NODE_ENV !== 'development') { + this.checkForUpdate() + } else { + setInterval(this.clearCache.bind(this), 5000) + } + } + + clearCache = async () => { + await this.client.cache.reset() + } + + checkForUpdate = async () => { + const { lastUpdate } = await getWebhookData() + if (this.lastUpdate < lastUpdate) { + await this.clearCache() + this.lastUpdate = lastUpdate + } + + setTimeout(() => this.checkForUpdate.bind(this), 5000) } handleResponse = (