Commit 0a13f744 authored by Mark Chao's avatar Mark Chao

Merge branch '268222-add-issues-weight-to-graphql' into 'master'

Add issues collection weight to GraphQL

See merge request gitlab-org/gitlab!45415
parents 1bb87641 d4f4153f
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
module Types module Types
# rubocop: disable Graphql/AuthorizeTypes # rubocop: disable Graphql/AuthorizeTypes
class CountableConnectionType < GraphQL::Types::Relay::BaseConnection class CountableConnectionType < GraphQL::Types::Relay::BaseConnection
field :count, Integer, null: false, field :count, GraphQL::INT_TYPE, null: false,
description: 'Total count of collection' description: 'Total count of collection'
def count def count
......
# frozen_string_literal: true
module Types
# rubocop: disable Graphql/AuthorizeTypes
class IssueConnectionType < CountableConnectionType
end
end
Types::IssueConnectionType.prepend_if_ee('::EE::Types::IssueConnectionType')
...@@ -4,7 +4,7 @@ module Types ...@@ -4,7 +4,7 @@ module Types
class IssueType < BaseObject class IssueType < BaseObject
graphql_name 'Issue' graphql_name 'Issue'
connection_type_class(Types::CountableConnectionType) connection_type_class(Types::IssueConnectionType)
implements(Types::Notes::NoteableType) implements(Types::Notes::NoteableType)
implements(Types::CurrentUserTodos) implements(Types::CurrentUserTodos)
......
...@@ -7036,6 +7036,11 @@ type EpicIssueConnection { ...@@ -7036,6 +7036,11 @@ type EpicIssueConnection {
Information to aid in pagination. Information to aid in pagination.
""" """
pageInfo: PageInfo! pageInfo: PageInfo!
"""
Total weight of issues collection
"""
weight: Int!
} }
""" """
...@@ -9234,6 +9239,11 @@ type IssueConnection { ...@@ -9234,6 +9239,11 @@ type IssueConnection {
Information to aid in pagination. Information to aid in pagination.
""" """
pageInfo: PageInfo! pageInfo: PageInfo!
"""
Total weight of issues collection
"""
weight: Int!
} }
""" """
......
...@@ -19387,6 +19387,24 @@ ...@@ -19387,6 +19387,24 @@
}, },
"isDeprecated": false, "isDeprecated": false,
"deprecationReason": null "deprecationReason": null
},
{
"name": "weight",
"description": "Total weight of issues collection",
"args": [
],
"type": {
"kind": "NON_NULL",
"name": null,
"ofType": {
"kind": "SCALAR",
"name": "Int",
"ofType": null
}
},
"isDeprecated": false,
"deprecationReason": null
} }
], ],
"inputFields": null, "inputFields": null,
...@@ -25159,6 +25177,24 @@ ...@@ -25159,6 +25177,24 @@
}, },
"isDeprecated": false, "isDeprecated": false,
"deprecationReason": null "deprecationReason": null
},
{
"name": "weight",
"description": "Total weight of issues collection",
"args": [
],
"type": {
"kind": "NON_NULL",
"name": null,
"ofType": {
"kind": "SCALAR",
"name": "Int",
"ofType": null
}
},
"isDeprecated": false,
"deprecationReason": null
} }
], ],
"inputFields": null, "inputFields": null,
# frozen_string_literal: true
module EE
module Types
module IssueConnectionType
extend ActiveSupport::Concern
prepended do
field :weight, GraphQL::INT_TYPE, null: false, description: 'Total weight of issues collection'
end
def weight
# rubocop: disable CodeReuse/ActiveRecord
relation = object.items
if relation.respond_to?(:reorder)
relation = relation.reorder(nil)
result = relation.sum(:weight)
if relation.try(:group_values)&.present?
result.values.sum
else
result
end
else
relation.map(&:weight).compact.sum
end
# rubocop: enable CodeReuse/ActiveRecord
end
end
end
end
---
title: Add issues collection weight to GraphQL
merge_request: 45415
author:
type: added
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe GitlabSchema.types['IssueConnection'] do
describe '#weight' do
subject(:response) { GitlabSchema.execute(query, context: { current_user: current_user }) }
let(:query) do
%(
query{
project(fullPath:"#{project.full_path}"){
issues{
weight
}
}
}
)
end
let_it_be(:project) { create :project, :public }
let_it_be(:current_user) { create :admin }
before do
create :issue, project: project, weight: 2
create :issue, project: project, weight: 7
create :issue, project: project, weight: nil
end
it 'returns sum of all weights' do
expect(response.dig(*%w[data project issues weight])).to eq 9
end
end
end
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
require 'spec_helper' require 'spec_helper'
RSpec.describe GitlabSchema.types['IssueConnection'] do RSpec.describe GitlabSchema.types['MergeRequestConnection'] do
it 'has the expected fields' do it 'has the expected fields' do
expected_fields = %i[count page_info edges nodes] expected_fields = %i[count page_info edges nodes]
......
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