add.rb 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. module Gql::Mutations
  3. class Link::Add < BaseMutation
  4. include Gql::Concerns::HandlesPossibleObjects
  5. description 'Add a link between objects'
  6. argument :input, Gql::Types::Input::LinkInputType, required: true, description: 'The link data'
  7. field :link, Gql::Types::LinkType, null: true, description: 'The created link'
  8. possible_objects ::Ticket, ::KnowledgeBase::Answer::Translation
  9. def resolve(input:)
  10. source = fetch_object(input.source_id)
  11. target = fetch_object(input.target_id, permission: :update?)
  12. type = input.type
  13. begin
  14. ::Link.add(
  15. link_type: type,
  16. link_object_source: source.class.name,
  17. link_object_source_value: source.id,
  18. link_object_target: target.class.name,
  19. link_object_target_value: target.id
  20. )
  21. rescue ActiveRecord::RecordNotUnique
  22. return error_response({ message: __('Link already exists') })
  23. end
  24. {
  25. link: {
  26. item: source,
  27. type: type
  28. }
  29. }
  30. end
  31. end
  32. end