Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-ce
Commits
ce6bfec3
Commit
ce6bfec3
authored
Jan 29, 2020
by
Patrick Derichs
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add TodosRestoreMany GraphQL mutation
Also adding specs, updating schema and docs.
parent
ba882c0d
Changes
9
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
422 additions
and
0 deletions
+422
-0
app/graphql/mutations/todos/restore_many.rb
app/graphql/mutations/todos/restore_many.rb
+75
-0
app/graphql/types/mutation_type.rb
app/graphql/types/mutation_type.rb
+1
-0
app/models/todo.rb
app/models/todo.rb
+2
-0
changelogs/unreleased/31914-graphql-todo-bulk-restore-pd.yml
changelogs/unreleased/31914-graphql-todo-bulk-restore-pd.yml
+5
-0
doc/api/graphql/reference/gitlab_schema.graphql
doc/api/graphql/reference/gitlab_schema.graphql
+36
-0
doc/api/graphql/reference/gitlab_schema.json
doc/api/graphql/reference/gitlab_schema.json
+149
-0
doc/api/graphql/reference/index.md
doc/api/graphql/reference/index.md
+10
-0
spec/graphql/mutations/todos/restore_many_spec.rb
spec/graphql/mutations/todos/restore_many_spec.rb
+114
-0
spec/models/todo_spec.rb
spec/models/todo_spec.rb
+30
-0
No files found.
app/graphql/mutations/todos/restore_many.rb
0 → 100644
View file @
ce6bfec3
# 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
app/graphql/types/mutation_type.rb
View file @
ce6bfec3
...
...
@@ -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
...
...
app/models/todo.rb
View file @
ce6bfec3
...
...
@@ -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
)
}
...
...
changelogs/unreleased/31914-graphql-todo-bulk-restore-pd.yml
0 → 100644
View file @
ce6bfec3
---
title
:
Add GraphQL mutation to restore multiple todos
merge_request
:
23950
author
:
type
:
added
doc/api/graphql/reference/gitlab_schema.graphql
View file @
ce6bfec3
...
...
@@ -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
"""
...
...
doc/api/graphql/reference/gitlab_schema.json
View file @
ce6bfec3
...
...
@@ -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"
,
...
...
doc/api/graphql/reference/index.md
View file @
ce6bfec3
...
...
@@ -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
...
...
spec/graphql/mutations/todos/restore_many_spec.rb
0 → 100644
View file @
ce6bfec3
# 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
spec/models/todo_spec.rb
View file @
ce6bfec3
...
...
@@ -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
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment