Commit 9faa870e authored by Thong Kuah's avatar Thong Kuah

Merge branch '63778-graphql-add-issue-due-date-sortring' into 'master'

GraphQL: Sort issues by due date

See merge request gitlab-org/gitlab!18094
parents b5d9664d 2b6184fd
......@@ -5,6 +5,9 @@ module Types
class IssueSortEnum < IssuableSortEnum
graphql_name 'IssueSort'
description 'Values for sorting issues'
value 'DUE_DATE_ASC', 'Due date by ascending order', value: 'due_date_asc'
value 'DUE_DATE_DESC', 'Due date by descending order', value: 'due_date_desc'
end
# rubocop: enable Graphql/AuthorizeTypes
end
......@@ -138,8 +138,8 @@ class Issue < ApplicationRecord
def self.sort_by_attribute(method, excluded_labels: [])
case method.to_s
when 'closest_future_date', 'closest_future_date_asc' then order_closest_future_date
when 'due_date', 'due_date_asc' then order_due_date_asc
when 'due_date_desc' then order_due_date_desc
when 'due_date', 'due_date_asc' then order_due_date_asc.with_order_id_desc
when 'due_date_desc' then order_due_date_desc.with_order_id_desc
when 'relative_position', 'relative_position_asc' then order_relative_position_asc.with_order_id_desc
else
super
......
---
title: 'Issues queried in GraphQL now sortable by due date'
merge_request: 18094
author:
type: added
......@@ -2676,6 +2676,16 @@ type IssuePermissions {
Values for sorting issues
"""
enum IssueSort {
"""
Due date by ascending order
"""
DUE_DATE_ASC
"""
Due date by descending order
"""
DUE_DATE_DESC
"""
Created at ascending order
"""
......
......@@ -13406,6 +13406,18 @@
"description": "Created at ascending order",
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "DUE_DATE_ASC",
"description": "Due date by ascending order",
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "DUE_DATE_DESC",
"description": "Due date by descending order",
"isDeprecated": false,
"deprecationReason": null
}
],
"possibleTypes": null
......
......@@ -72,9 +72,34 @@ describe Resolvers::IssuesResolver do
expect(resolve_issues(search: 'foo')).to contain_exactly(issue2)
end
it 'sort issues' do
describe 'sorting' do
context 'when sorting by created' do
it 'sorts issues ascending' do
expect(resolve_issues(sort: 'created_asc')).to eq [issue1, issue2]
end
it 'sorts issues descending' do
expect(resolve_issues(sort: 'created_desc')).to eq [issue2, issue1]
end
end
context 'when sorting by due date' do
let(:project) { create(:project) }
let!(:due_issue1) { create(:issue, project: project, due_date: 3.days.from_now) }
let!(:due_issue2) { create(:issue, project: project, due_date: nil) }
let!(:due_issue3) { create(:issue, project: project, due_date: 2.days.ago) }
let!(:due_issue4) { create(:issue, project: project, due_date: nil) }
it 'sorts issues ascending' do
expect(resolve_issues(sort: :due_date_asc)).to eq [due_issue3, due_issue1, due_issue4, due_issue2]
end
it 'sorts issues descending' do
expect(resolve_issues(sort: :due_date_desc)).to eq [due_issue1, due_issue3, due_issue4, due_issue2]
end
end
end
it 'returns issues user can see' do
project.add_guest(current_user)
......
# frozen_string_literal: true
require 'spec_helper'
describe GitlabSchema.types['IssueSort'] do
it { expect(described_class.graphql_name).to eq('IssueSort') }
it_behaves_like 'common sort values'
it 'exposes all the existing issue sort values' do
expect(described_class.values.keys).to include(*%w[DUE_DATE_ASC DUE_DATE_DESC])
end
end
......@@ -64,7 +64,7 @@ describe 'getting an issue list for a project' do
end
end
it "is expected to check permissions on the first issue only" do
it 'is expected to check permissions on the first issue only' do
allow(Ability).to receive(:allowed?).and_call_original
# Newest first, we only want to see the newest checked
expect(Ability).not_to receive(:allowed?).with(current_user, :read_issue, issues.first)
......@@ -116,4 +116,95 @@ describe 'getting an issue list for a project' do
end
end
end
describe 'sorting and pagination' do
let(:start_cursor) { graphql_data['project']['issues']['pageInfo']['startCursor'] }
let(:end_cursor) { graphql_data['project']['issues']['pageInfo']['endCursor'] }
context 'when sorting by due date' do
let(:sort_project) { create(:project, :public) }
let!(:due_issue1) { create(:issue, project: sort_project, due_date: 3.days.from_now) }
let!(:due_issue2) { create(:issue, project: sort_project, due_date: nil) }
let!(:due_issue3) { create(:issue, project: sort_project, due_date: 2.days.ago) }
let!(:due_issue4) { create(:issue, project: sort_project, due_date: nil) }
let!(:due_issue5) { create(:issue, project: sort_project, due_date: 1.day.ago) }
let(:params) { 'sort: DUE_DATE_ASC' }
def query(issue_params = params)
graphql_query_for(
'project',
{ 'fullPath' => sort_project.full_path },
<<~ISSUES
issues(#{issue_params}) {
pageInfo {
endCursor
}
edges {
node {
iid
dueDate
}
}
}
ISSUES
)
end
before do
post_graphql(query, current_user: current_user)
end
it_behaves_like 'a working graphql query'
context 'when ascending' do
it 'sorts issues' do
expect(grab_iids).to eq [due_issue3.iid, due_issue5.iid, due_issue1.iid, due_issue4.iid, due_issue2.iid]
end
context 'when paginating' do
let(:params) { 'sort: DUE_DATE_ASC, first: 2' }
it 'sorts issues' do
expect(grab_iids).to eq [due_issue3.iid, due_issue5.iid]
cursored_query = query("sort: DUE_DATE_ASC, after: \"#{end_cursor}\"")
post_graphql(cursored_query, current_user: current_user)
response_data = JSON.parse(response.body)['data']['project']['issues']['edges']
expect(grab_iids(response_data)).to eq [due_issue1.iid, due_issue4.iid, due_issue2.iid]
end
end
end
context 'when descending' do
let(:params) { 'sort: DUE_DATE_DESC' }
it 'sorts issues' do
expect(grab_iids).to eq [due_issue1.iid, due_issue5.iid, due_issue3.iid, due_issue4.iid, due_issue2.iid]
end
context 'when paginating' do
let(:params) { 'sort: DUE_DATE_DESC, first: 2' }
it 'sorts issues' do
expect(grab_iids).to eq [due_issue1.iid, due_issue5.iid]
cursored_query = query("sort: DUE_DATE_DESC, after: \"#{end_cursor}\"")
post_graphql(cursored_query, current_user: current_user)
response_data = JSON.parse(response.body)['data']['project']['issues']['edges']
expect(grab_iids(response_data)).to eq [due_issue3.iid, due_issue4.iid, due_issue2.iid]
end
end
end
end
end
def grab_iids(data = issues_data)
data.map do |issue|
issue.dig('node', 'iid').to_i
end
end
end
# frozen_string_literal: true
RSpec.shared_examples 'common sort values' do
it 'exposes all the existing common sort values' do
expect(described_class.values.keys).to include(*%w[updated_desc updated_asc created_desc created_asc])
end
end
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