Welcome to mirror list, hosted at ThFree Co, Russian Federation.

current_user_todos.rb « types « graphql « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 4c4cb516979887319418cc06e84be0f1b80b2bf9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# frozen_string_literal: true

# Interface to expose todos for the current_user on the `object`
module Types
  module CurrentUserTodos
    include BaseInterface

    field_class Types::BaseField

    field :current_user_todos, Types::TodoType.connection_type,
          description: 'To-do items for the current user.',
          null: false do
            argument :state, Types::TodoStateEnum,
                     description: 'State of the to-do items.',
                     required: false
          end

    def current_user_todos(state: nil)
      state ||= %i[done pending] # TodosFinder treats a `nil` state param as `pending`
      key = [state, unpresented.class.name]

      BatchLoader::GraphQL.for(unpresented).batch(default_value: [], key: key) do |targets, loader, args|
        state, klass_name = args[:key]

        targets_by_id = targets.index_by(&:id)
        ids = targets_by_id.keys

        results = TodosFinder.new(current_user, state: state, type: klass_name, target_id: ids).execute

        by_target_id = results.group_by(&:target_id)

        by_target_id.each do |target_id, todos|
          target = targets_by_id[target_id]
          todos.each { _1.target = target } # prevent extra loads
          loader.call(target, todos)
        end
      end
    end
  end
end