Module: Types::CurrentUserTodos

Includes:
BaseInterface
Defined in:
app/graphql/types/current_user_todos.rb

Instance Method Summary collapse

Instance Method Details

#current_user_todos(state: nil) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'app/graphql/types/current_user_todos.rb', line 18

def current_user_todos(state: nil)
  state ||= %i[done pending] # TodosFinder treats a `nil` state param as `pending`
  target_type_name = unpresented.try(:todoable_target_type_name) || unpresented.class.name
  key = [state, target_type_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