getFieldResolver.ts 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import type { EnumObjectManagerObjects } from '#shared/graphql/types.ts'
  3. import type FieldResolver from './FieldResolver.ts'
  4. import type {
  5. FieldResolverClass,
  6. FieldResolverModule,
  7. } from '../../types/resolver.ts'
  8. import type { ObjectAttribute } from '../../types/store.ts'
  9. const fieldResolverModules = import.meta.glob<FieldResolverModule>(
  10. ['./fields/*.ts', '!./__tests__/**/*.ts'],
  11. {
  12. eager: true,
  13. import: 'default',
  14. },
  15. )
  16. const fieldResolverClasses: Record<string, FieldResolverClass> = {}
  17. Object.entries(fieldResolverModules).forEach(([, resolverModule]) => {
  18. fieldResolverClasses[resolverModule.type] = resolverModule.resolver
  19. })
  20. const getFieldResolver = (
  21. object: EnumObjectManagerObjects,
  22. attribute: ObjectAttribute,
  23. ): FieldResolver => {
  24. if (!fieldResolverClasses[attribute.dataType]) {
  25. throw new Error(
  26. `No field resolver for type ${attribute.dataType} (${attribute.name})`,
  27. )
  28. }
  29. return new fieldResolverClasses[attribute.dataType](object, attribute)
  30. }
  31. export default getFieldResolver