getFieldResolver.ts 1.1 KB

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