Commit ce6bfec3 authored by Patrick Derichs's avatar Patrick Derichs

Add TodosRestoreMany GraphQL mutation

Also adding specs, updating schema and docs.
parent ba882c0d
# frozen_string_literal: true
module Mutations
module Todos
class RestoreMany < ::Mutations::Todos::Base
graphql_name 'TodoRestoreMany'
MAX_UPDATE_AMOUNT = 50
argument :ids,
[GraphQL::ID_TYPE],
required: true,
description: 'The global ids of the todos to restore (a maximum of 50 is supported at once)'
field :updated_ids, [GraphQL::ID_TYPE],
null: false,
description: 'The ids of the updated todo items'
def resolve(ids:)
check_update_amount_limit!(ids)
todos = authorized_find_all_pending_by_current_user(model_ids_of(ids))
updated_ids = restore(todos)
{
updated_ids: gids_of(updated_ids),
errors: errors_on_objects(todos)
}
end
private
def gids_of(ids)
ids.map { |id| ::URI::GID.build(app: GlobalID.app, model_name: Todo.name, model_id: id, params: nil).to_s }
end
def model_ids_of(ids)
ids.map do |gid|
parsed_gid = ::URI::GID.parse(gid)
parsed_gid.model_id.to_i if accessible_todo?(parsed_gid)
end.compact
end
def accessible_todo?(gid)
gid.app == GlobalID.app && todo?(gid)
end
def todo?(gid)
GlobalID.parse(gid)&.model_class&.ancestors&.include?(Todo)
end
def raise_too_many_todos_requested_error
raise Gitlab::Graphql::Errors::ArgumentError, 'Too many todos requested.'
end
def check_update_amount_limit!(ids)
raise_too_many_todos_requested_error if ids.size > MAX_UPDATE_AMOUNT
end
def errors_on_objects(todos)
todos.flat_map { |todo| errors_on_object(todo) }
end
def authorized_find_all_pending_by_current_user(ids)
return Todo.none if ids.blank? || current_user.nil?
Todo.for_ids(ids).for_user(current_user).done
end
def restore(todos)
TodoService.new.mark_todos_as_pending(todos, current_user)
end
end
end
end
......@@ -25,6 +25,7 @@ module Types
mount_mutation Mutations::Todos::MarkDone
mount_mutation Mutations::Todos::Restore
mount_mutation Mutations::Todos::MarkAllDone
mount_mutation Mutations::Todos::RestoreMany
mount_mutation Mutations::Snippets::Destroy
mount_mutation Mutations::Snippets::Update
mount_mutation Mutations::Snippets::Create
......
......@@ -51,10 +51,12 @@ class Todo < ApplicationRecord
validates :project, presence: true, unless: :group_id
validates :group, presence: true, unless: :project_id
scope :for_ids, -> (ids) { where(id: ids) }
scope :pending, -> { with_state(:pending) }
scope :done, -> { with_state(:done) }
scope :for_action, -> (action) { where(action: action) }
scope :for_author, -> (author) { where(author: author) }
scope :for_user, -> (user) { where(user: user) }
scope :for_project, -> (projects) { where(project: projects) }
scope :for_undeleted_projects, -> { joins(:project).merge(Project.without_deleted) }
scope :for_group, -> (group) { where(group: group) }
......
---
title: Add GraphQL mutation to restore multiple todos
merge_request: 23950
author:
type: added
......@@ -4540,6 +4540,7 @@ type Mutation {
removeAwardEmoji(input: RemoveAwardEmojiInput!): RemoveAwardEmojiPayload
todoMarkDone(input: TodoMarkDoneInput!): TodoMarkDonePayload
todoRestore(input: TodoRestoreInput!): TodoRestorePayload
todoRestoreMany(input: TodoRestoreManyInput!): TodoRestoreManyPayload
todosMarkAllDone(input: TodosMarkAllDoneInput!): TodosMarkAllDonePayload
toggleAwardEmoji(input: ToggleAwardEmojiInput!): ToggleAwardEmojiPayload
updateEpic(input: UpdateEpicInput!): UpdateEpicPayload
......@@ -7002,6 +7003,41 @@ input TodoRestoreInput {
id: ID!
}
"""
Autogenerated input type of TodoRestoreMany
"""
input TodoRestoreManyInput {
"""
A unique identifier for the client performing the mutation.
"""
clientMutationId: String
"""
The global ids of the todos to restore (a maximum of 50 is supported at once)
"""
ids: [ID!]!
}
"""
Autogenerated return type of TodoRestoreMany
"""
type TodoRestoreManyPayload {
"""
A unique identifier for the client performing the mutation.
"""
clientMutationId: String
"""
Reasons why the mutation failed.
"""
errors: [String!]!
"""
The ids of the updated todo items
"""
updatedIds: [ID!]!
}
"""
Autogenerated return type of TodoRestore
"""
......
......@@ -18780,6 +18780,33 @@
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "todoRestoreMany",
"description": null,
"args": [
{
"name": "input",
"description": null,
"type": {
"kind": "NON_NULL",
"name": null,
"ofType": {
"kind": "INPUT_OBJECT",
"name": "TodoRestoreManyInput",
"ofType": null
}
},
"defaultValue": null
}
],
"type": {
"kind": "OBJECT",
"name": "TodoRestoreManyPayload",
"ofType": null
},
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "todosMarkAllDone",
"description": null,
......@@ -21664,6 +21691,128 @@
"enumValues": null,
"possibleTypes": null
},
{
"kind": "OBJECT",
"name": "TodoRestoreManyPayload",
"description": "Autogenerated return type of TodoRestoreMany",
"fields": [
{
"name": "clientMutationId",
"description": "A unique identifier for the client performing the mutation.",
"args": [
],
"type": {
"kind": "SCALAR",
"name": "String",
"ofType": null
},
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "errors",
"description": "Reasons why the mutation failed.",
"args": [
],
"type": {
"kind": "NON_NULL",
"name": null,
"ofType": {
"kind": "LIST",
"name": null,
"ofType": {
"kind": "NON_NULL",
"name": null,
"ofType": {
"kind": "SCALAR",
"name": "String",
"ofType": null
}
}
}
},
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "updatedIds",
"description": "The ids of the updated todo items",
"args": [
],
"type": {
"kind": "NON_NULL",
"name": null,
"ofType": {
"kind": "LIST",
"name": null,
"ofType": {
"kind": "NON_NULL",
"name": null,
"ofType": {
"kind": "SCALAR",
"name": "ID",
"ofType": null
}
}
}
},
"isDeprecated": false,
"deprecationReason": null
}
],
"inputFields": null,
"interfaces": [
],
"enumValues": null,
"possibleTypes": null
},
{
"kind": "INPUT_OBJECT",
"name": "TodoRestoreManyInput",
"description": "Autogenerated input type of TodoRestoreMany",
"fields": null,
"inputFields": [
{
"name": "ids",
"description": "The global ids of the todos to restore (a maximum of 50 is supported at once)",
"type": {
"kind": "NON_NULL",
"name": null,
"ofType": {
"kind": "LIST",
"name": null,
"ofType": {
"kind": "NON_NULL",
"name": null,
"ofType": {
"kind": "SCALAR",
"name": "ID",
"ofType": null
}
}
}
},
"defaultValue": null
},
{
"name": "clientMutationId",
"description": "A unique identifier for the client performing the mutation.",
"type": {
"kind": "SCALAR",
"name": "String",
"ofType": null
},
"defaultValue": null
}
],
"interfaces": null,
"enumValues": null,
"possibleTypes": null
},
{
"kind": "OBJECT",
"name": "DestroySnippetPayload",
......
......@@ -1120,6 +1120,16 @@ Autogenerated return type of TodoMarkDone
| `errors` | String! => Array | Reasons why the mutation failed. |
| `todo` | Todo! | The requested todo |
## TodoRestoreManyPayload
Autogenerated return type of TodoRestoreMany
| Name | Type | Description |
| --- | ---- | ---------- |
| `clientMutationId` | String | A unique identifier for the client performing the mutation. |
| `errors` | String! => Array | Reasons why the mutation failed. |
| `updatedIds` | ID! => Array | The ids of the updated todo items |
## TodoRestorePayload
Autogenerated return type of TodoRestore
......
# frozen_string_literal: true
require 'spec_helper'
describe Mutations::Todos::RestoreMany do
let_it_be(:current_user) { create(:user) }
let_it_be(:author) { create(:user) }
let_it_be(:other_user) { create(:user) }
let_it_be(:todo1) { create(:todo, user: current_user, author: author, state: :done) }
let_it_be(:todo2) { create(:todo, user: current_user, author: author, state: :pending) }
let_it_be(:other_user_todo) { create(:todo, user: other_user, author: author, state: :done) }
let(:mutation) { described_class.new(object: nil, context: { current_user: current_user }) }
describe '#resolve' do
it 'restores a single todo' do
result = restore_mutation([todo1])
expect(todo1.reload.state).to eq('pending')
expect(todo2.reload.state).to eq('pending')
expect(other_user_todo.reload.state).to eq('done')
todo_ids = result[:updated_ids]
expect(todo_ids.size).to eq(1)
expect(todo_ids.first).to eq(todo1.to_global_id.to_s)
end
it 'handles a todo which is already pending as expected' do
result = restore_mutation([todo2])
expect_states_were_not_changed
expect(result[:updated_ids]).to eq([])
end
it 'ignores requests for todos which do not belong to the current user' do
restore_mutation([other_user_todo])
expect_states_were_not_changed
end
it 'ignores invalid GIDs' do
expect { mutation.resolve(ids: ['invalid_gid']) }.to raise_error(URI::BadURIError)
expect_states_were_not_changed
end
it 'restores multiple todos' do
todo4 = create(:todo, user: current_user, author: author, state: :done)
result = restore_mutation([todo1, todo4, todo2])
expect(result[:updated_ids].size).to eq(2)
returned_todo_ids = result[:updated_ids]
expect(returned_todo_ids).to contain_exactly(todo1.to_global_id.to_s, todo4.to_global_id.to_s)
expect(todo1.reload.state).to eq('pending')
expect(todo2.reload.state).to eq('pending')
expect(todo4.reload.state).to eq('pending')
expect(other_user_todo.reload.state).to eq('done')
end
it 'fails if one todo does not belong to the current user' do
restore_mutation([todo1, todo2, other_user_todo])
expect(todo1.reload.state).to eq('pending')
expect(todo2.reload.state).to eq('pending')
expect(other_user_todo.reload.state).to eq('done')
end
it 'fails if too many todos are requested for update' do
expect { restore_mutation([todo1] * 51) }.to raise_error(Gitlab::Graphql::Errors::ArgumentError)
end
it 'does not update todos from another app' do
todo4 = create(:todo)
todo4_gid = ::URI::GID.parse("gid://otherapp/Todo/#{todo4.id}")
result = mutation.resolve(ids: [todo4_gid.to_s])
expect(result[:updated_ids]).to be_empty
expect_states_were_not_changed
end
it 'does not update todos from another model' do
todo4 = create(:todo)
todo4_gid = ::URI::GID.parse("gid://#{GlobalID.app}/Project/#{todo4.id}")
result = mutation.resolve(ids: [todo4_gid.to_s])
expect(result[:updated_ids]).to be_empty
expect_states_were_not_changed
end
end
def restore_mutation(todos)
mutation.resolve(ids: todos.map { |todo| global_id_of(todo) } )
end
def global_id_of(todo)
todo.to_global_id.to_s
end
def expect_states_were_not_changed
expect(todo1.reload.state).to eq('done')
expect(todo2.reload.state).to eq('pending')
expect(other_user_todo.reload.state).to eq('done')
end
end
......@@ -313,6 +313,36 @@ describe Todo do
end
end
describe '.for_ids' do
it 'returns the expected todos' do
todo1 = create(:todo)
todo2 = create(:todo)
todo3 = create(:todo)
create(:todo)
expect(described_class.for_ids([todo2.id, todo1.id, todo3.id])).to contain_exactly(todo1, todo2, todo3)
end
it 'returns an empty collection when no ids are given' do
create(:todo)
expect(described_class.for_ids([])).to be_empty
end
end
describe '.for_user' do
it 'returns the expected todos' do
user1 = create(:user)
user2 = create(:user)
todo1 = create(:todo, user: user1)
todo2 = create(:todo, user: user1)
create(:todo, user: user2)
expect(described_class.for_user(user1)).to contain_exactly(todo1, todo2)
end
end
describe '.any_for_target?' do
it 'returns true if there are todos for a given target' do
todo = create(:todo)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment