client.ts 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { ApolloClient } from '@apollo/client/core'
  3. import type { CacheInitializerModules } from '#shared/types/server/apollo/client.ts'
  4. import createCache from './cache.ts'
  5. import link from './link.ts'
  6. import type { NormalizedCacheObject } from '@apollo/client/core'
  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(currentFetchPolicy) {
  23. return currentFetchPolicy
  24. },
  25. },
  26. },
  27. })
  28. return apolloClient
  29. }
  30. export const getApolloClient = () => {
  31. return apolloClient
  32. }
  33. export const clearApolloClientStore = async () => {
  34. await apolloClient.clearStore()
  35. }