MutationHandler.ts 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. // Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. import type { Ref } from 'vue'
  3. import type { UseMutationReturn } from '@vue/apollo-composable'
  4. import type { OperationVariables } from '@apollo/client/core'
  5. import UserError from '@shared/errors/UserError'
  6. import type { OperationMutationResult } from '@shared/types/server/apollo/handler'
  7. import BaseHandler from './BaseHandler'
  8. export default class MutationHandler<
  9. TResult = OperationMutationResult,
  10. TVariables = OperationVariables,
  11. > extends BaseHandler<
  12. TResult,
  13. TVariables,
  14. UseMutationReturn<TResult, TVariables>
  15. > {
  16. public async send(variables?: TVariables): Promise<Maybe<TResult>> {
  17. return new Promise((resolve, reject) => {
  18. this.operationResult.mutate(variables).then((result) => {
  19. if (!result) {
  20. return reject(this.operationError().value)
  21. }
  22. if (result.data) {
  23. const { errors } = Object.values(result.data)[0] as {
  24. errors: UserError[]
  25. }
  26. if (errors) {
  27. const userErrors = new UserError(errors)
  28. return reject(userErrors)
  29. }
  30. }
  31. return resolve(result.data || null)
  32. })
  33. })
  34. }
  35. public called(): Ref<boolean> {
  36. return this.operationResult.called
  37. }
  38. }