1# frozen_string_literal: true
2
3module Resolvers
4  class BaseIssuesResolver < BaseResolver
5    prepend IssueResolverArguments
6
7    argument :sort, Types::IssueSortEnum,
8              description: 'Sort issues by this criteria.',
9              required: false,
10              default_value: :created_desc
11    argument :state, Types::IssuableStateEnum,
12              required: false,
13              description: 'Current state of this issue.'
14
15    type Types::IssueType.connection_type, null: true
16
17    NON_STABLE_CURSOR_SORTS = %i[priority_asc priority_desc
18                                 popularity_asc popularity_desc
19                                 label_priority_asc label_priority_desc
20                                 milestone_due_asc milestone_due_desc].freeze
21
22    def continue_issue_resolve(parent, finder, **args)
23      issues = Gitlab::Graphql::Loaders::IssuableLoader.new(parent, finder).batching_find_all { |q| apply_lookahead(q) }
24
25      if non_stable_cursor_sort?(args[:sort])
26        # Certain complex sorts are not supported by the stable cursor pagination yet.
27        # In these cases, we use offset pagination, so we return the correct connection.
28        offset_pagination(issues)
29      else
30        issues
31      end
32    end
33
34    private
35
36    def unconditional_includes
37      [
38        {
39          project: [:project_feature, :group]
40        },
41        :author
42      ]
43    end
44
45    def preloads
46      {
47        alert_management_alert: [:alert_management_alert],
48        labels: [:labels],
49        assignees: [:assignees],
50        timelogs: [:timelogs],
51        customer_relations_contacts: { customer_relations_contacts: [:group] }
52      }
53    end
54
55    def non_stable_cursor_sort?(sort)
56      NON_STABLE_CURSOR_SORTS.include?(sort)
57    end
58  end
59end
60
61Resolvers::BaseIssuesResolver.prepend_mod_with('Resolvers::BaseIssuesResolver')
62