add.rb 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. # Copyright (C) 2012-2025 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. link = ::Link.add(
  14. link_type: type,
  15. link_object_source: source.class.name,
  16. link_object_source_value: source.id,
  17. link_object_target: target.class.name,
  18. link_object_target_value: target.id
  19. )
  20. if !link.valid?
  21. return error_response({ message: link.errors.full_messages.first })
  22. end
  23. {
  24. link: {
  25. item: source,
  26. type: type
  27. }
  28. }
  29. end
  30. end
  31. end