cache.ts 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { InMemoryCache } from '@apollo/client/core'
  3. import type { InMemoryCacheConfig } from '@apollo/client/cache/inmemory/types'
  4. import type { ImportGlobEagerDefault } from '#shared/types/utils.ts'
  5. import type {
  6. CacheInitializerModules,
  7. RegisterInMemoryCacheConfig,
  8. } from '#shared/types/server/apollo/client.ts'
  9. let cacheConfig: InMemoryCacheConfig = {}
  10. const cacheInitializerModules: CacheInitializerModules = import.meta.glob(
  11. './cache/initializer/*.ts',
  12. { eager: true },
  13. )
  14. const registerInitializeModules = (
  15. additionalCacheInitializerModules: CacheInitializerModules = {},
  16. ) => {
  17. const allCacheInitializerModules = Object.assign(
  18. cacheInitializerModules,
  19. additionalCacheInitializerModules,
  20. )
  21. Object.values(allCacheInitializerModules).forEach(
  22. (module: ImportGlobEagerDefault<RegisterInMemoryCacheConfig>) => {
  23. const register = module.default
  24. cacheConfig = register(cacheConfig)
  25. },
  26. )
  27. }
  28. const createCache = (
  29. additionalCacheInitializerModules: CacheInitializerModules = {},
  30. ): InMemoryCache => {
  31. registerInitializeModules(additionalCacheInitializerModules)
  32. return new InMemoryCache(cacheConfig)
  33. }
  34. export default createCache