MutationHandler.ts 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. // Copyright (C) 2012-2022 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
  19. .mutate(variables)
  20. .then((result) => {
  21. if (result?.data) {
  22. const { errors } = Object.values(result.data)[0] as {
  23. errors: UserError[]
  24. }
  25. if (errors) {
  26. const userErrors = new UserError(errors)
  27. return reject(userErrors)
  28. }
  29. }
  30. return resolve(result?.data || null)
  31. })
  32. .catch(() => reject(this.operationError().value))
  33. })
  34. }
  35. public called(): Ref<boolean> {
  36. return this.operationResult.called
  37. }
  38. }