search.rb 1.4 KB

1234567891011121314151617181920212223242526272829303132333435
  1. # Copyright (C) 2012-2025 Zammad Foundation, https://zammad-foundation.org/
  2. module Gql::Queries
  3. class Search < BaseQuery
  4. description 'Generic object search'
  5. argument :search, String, description: 'What to search for'
  6. argument :only_in, Gql::Types::Enum::SearchableModelsType, description: 'Which model to search in, e.g. Ticket'
  7. argument :order_by, String, required: false, description: 'Set a custom order by'
  8. argument :order_direction, Gql::Types::Enum::OrderDirectionType, required: false, description: 'Set a custom order direction'
  9. argument :limit, Integer, required: false, description: 'How many entries to find at maximum'
  10. argument :offset, Integer, required: false, description: 'Offset to use for pagination'
  11. type Gql::Types::SearchResultType, null: false
  12. def resolve(search:, only_in:, order_by: nil, order_direction: nil, offset: 0, limit: 10)
  13. search_result = Service::Search.new(
  14. current_user: context.current_user,
  15. query: search,
  16. objects: [only_in],
  17. options: { offset:, limit:, sort_by: [order_by].compact, order_by: [order_direction].compact }
  18. ).execute.result[only_in]
  19. return { total_count: 0, items: [] } if !search_result
  20. {
  21. total_count: search_result[:total_count],
  22. items: search_result[:objects],
  23. }
  24. end
  25. end
  26. end