cache.ts 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { InMemoryCache } from '@apollo/client/core'
  3. import type {
  4. CacheInitializerModules,
  5. RegisterInMemoryCacheConfig,
  6. } from '#shared/types/server/apollo/client.ts'
  7. import type { ImportGlobEagerDefault } from '#shared/types/utils.ts'
  8. import type { InMemoryCacheConfig } from '@apollo/client/cache/inmemory/types'
  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