Commit 30c68de4 authored by Michael Kozono's avatar Michael Kozono

Merge branch '29713-graphql-add-issue-weight-sorting' into 'master'

GraphQL: Sort issues by weight

See merge request gitlab-org/gitlab!19721
parents 570f33a2 9a21b605
......@@ -12,3 +12,5 @@ module Types
end
# rubocop: enable Graphql/AuthorizeTypes
end
Types::IssueSortEnum.prepend_if_ee('::EE::Types::IssueSortEnum')
---
title: Graphql query for issues can now be sorted by weight
merge_request: 19721
author:
type: added
......@@ -2537,6 +2537,16 @@ enum IssueSort {
"""
RELATIVE_POSITION_ASC
"""
Weight by ascending order
"""
WEIGHT_ASC
"""
Weight by descending order
"""
WEIGHT_DESC
"""
Created at ascending order
"""
......
......@@ -13772,6 +13772,18 @@
"description": "Relative position by ascending order",
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "WEIGHT_ASC",
"description": "Weight by ascending order",
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "WEIGHT_DESC",
"description": "Weight by descending order",
"isDeprecated": false,
"deprecationReason": null
}
],
"possibleTypes": null
......
# frozen_string_literal: true
module EE
module Types
module IssueSortEnum
extend ActiveSupport::Concern
prepended do
value 'WEIGHT_ASC', 'Weight by ascending order', value: 'weight_asc'
value 'WEIGHT_DESC', 'Weight by descending order', value: 'weight_desc'
end
end
end
end
# frozen_string_literal: true
require 'spec_helper'
describe Resolvers::IssuesResolver do
include GraphqlHelpers
let_it_be(:current_user) { create(:user) }
let(:project) { create(:project) }
context "with a project" do
describe '#resolve' do
describe 'sorting' do
context 'when sorting by weight' do
let!(:weight_issue1) { create(:issue, project: project, weight: 5) }
let!(:weight_issue2) { create(:issue, project: project, weight: nil) }
let!(:weight_issue3) { create(:issue, project: project, weight: 1) }
let!(:weight_issue4) { create(:issue, project: project, weight: nil) }
before do
project.add_developer(current_user)
end
it 'sorts issues ascending' do
expect(resolve_issues(sort: :weight_asc)).to eq [weight_issue3, weight_issue1, weight_issue4, weight_issue2]
end
it 'sorts issues descending' do
expect(resolve_issues(sort: :weight_desc)).to eq [weight_issue1, weight_issue3, weight_issue4, weight_issue2]
end
end
end
end
end
def resolve_issues(args = {}, context = { current_user: current_user })
resolve(described_class, obj: project, args: args, ctx: context)
end
end
# 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 EE issue sort values' do
expect(described_class.values.keys).to include(*%w[WEIGHT_ASC WEIGHT_DESC])
end
end
# frozen_string_literal: true
require 'spec_helper'
describe 'getting an issue list for a project' do
include GraphqlHelpers
let(:project) { create(:project, :repository, :public) }
let(:current_user) { create(:user) }
let(:issues_data) { graphql_data['project']['issues']['edges'] }
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 weight' do
let(:sort_project) { create(:project, :public) }
let!(:weight_issue1) { create(:issue, project: sort_project, weight: 5) }
let!(:weight_issue2) { create(:issue, project: sort_project, weight: nil) }
let!(:weight_issue3) { create(:issue, project: sort_project, weight: 1) }
let!(:weight_issue4) { create(:issue, project: sort_project, weight: nil) }
let!(:weight_issue5) { create(:issue, project: sort_project, weight: 3) }
let(:params) { 'sort: WEIGHT_ASC' }
def query(issue_params = params)
graphql_query_for(
'project',
{ 'fullPath' => sort_project.full_path },
<<~ISSUES
issues(#{issue_params}) {
pageInfo {
endCursor
}
edges {
node {
iid
weight
}
}
}
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 [weight_issue3.iid, weight_issue5.iid, weight_issue1.iid, weight_issue4.iid, weight_issue2.iid]
end
context 'when paginating' do
let(:params) { 'sort: WEIGHT_ASC, first: 2' }
it 'sorts issues' do
expect(grab_iids).to eq [weight_issue3.iid, weight_issue5.iid]
cursored_query = query("sort: WEIGHT_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 [weight_issue1.iid, weight_issue4.iid, weight_issue2.iid]
end
end
end
context 'when descending' do
let(:params) { 'sort: WEIGHT_DESC' }
it 'sorts issues' do
expect(grab_iids).to eq [weight_issue1.iid, weight_issue5.iid, weight_issue3.iid, weight_issue4.iid, weight_issue2.iid]
end
context 'when paginating' do
let(:params) { 'sort: WEIGHT_DESC, first: 2' }
it 'sorts issues' do
expect(grab_iids).to eq [weight_issue1.iid, weight_issue5.iid]
cursored_query = query("sort: WEIGHT_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 [weight_issue3.iid, weight_issue4.iid, weight_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
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