Skip to content

Commit

Permalink
fix: clear apollo cache upon webhook calls
Browse files Browse the repository at this point in the history
  • Loading branch information
jeangovil committed Jan 23, 2024
1 parent 6d4f93f commit f6f4be6
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/services/strapi/strapi.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<any> = null as any
axios: Axios = null as any
lastUpdate = 0

constructor(apiUrl: string, graphqlUrl: string, apiKey: string) {
this.axios = axios.create({
Expand Down Expand Up @@ -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 = <T>(
Expand Down

0 comments on commit f6f4be6

Please sign in to comment.