client.ts 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import type { NormalizedCacheObject } from '@apollo/client/core'
  3. import { ApolloClient } from '@apollo/client/core'
  4. import type { CacheInitializerModules } from '#shared/types/server/apollo/client.ts'
  5. import link from './link.ts'
  6. import createCache from './cache.ts'
  7. let apolloClient: ApolloClient<NormalizedCacheObject>
  8. export const createApolloClient = (
  9. cacheInitializerModules: CacheInitializerModules = {},
  10. ) => {
  11. const cache = createCache(cacheInitializerModules)
  12. apolloClient = new ApolloClient({
  13. connectToDevTools: process.env.NODE_ENV !== 'production',
  14. link,
  15. cache,
  16. queryDeduplication: true,
  17. defaultOptions: {
  18. // always refresh query results from the server
  19. // https://www.apollographql.com/docs/react/data/queries/#setting-a-fetch-policy
  20. watchQuery: {
  21. fetchPolicy: 'cache-and-network',
  22. nextFetchPolicy: 'cache-and-network',
  23. },
  24. },
  25. })
  26. return apolloClient
  27. }
  28. export const getApolloClient = () => {
  29. return apolloClient
  30. }
  31. export const clearApolloClientStore = async () => {
  32. await apolloClient.clearStore()
  33. }