Commit 2740bda6 authored by Donald Cook's avatar Donald Cook Committed by Alex Kalderimis

Add top-level GraphQL query for single board list

- Add specs
- Update docs
- Fixes N+1 issue in List.title

Changelog: added
parent 4e4ce830
# frozen_string_literal: true
module Resolvers
class BoardListResolver < BaseResolver.single
include Gitlab::Graphql::Authorize::AuthorizeResource
include BoardItemFilterable
type Types::BoardListType, null: true
description 'Find an issue board list.'
authorize :read_issue_board_list
argument :id, Types::GlobalIDType[List],
required: true,
description: 'Global ID of the list.'
argument :issue_filters, Types::Boards::BoardIssueInputType,
required: false,
description: 'Filters applied when getting issue metadata in the board list.'
def resolve(id: nil, issue_filters: {})
context.scoped_set!(:issue_filters, item_filters(issue_filters))
Gitlab::Graphql::Lazy.with_value(find_list(id: id)) do |list|
list if authorized_resource?(list)
end
end
private
def find_list(id:)
GitlabSchema.object_from_id(id, expected_type: ::List)
end
end
end
......@@ -47,6 +47,19 @@ module Types
.metadata
end
end
# board lists have a data dependency on label - so we batch load them here
def title
if object.association(:label).loaded? && object.label_id.present?
object.title
else
loader = Gitlab::Graphql::Loaders::BatchModelLoader.new(Label, object.label_id)
Gitlab::Graphql::Lazy.with_value(loader.find) do |label|
object.label = label
object.title
end
end
end
end
# rubocop: enable Graphql/AuthorizeTypes
end
......
......@@ -136,6 +136,10 @@ module Types
complexity: 5,
resolver: ::Resolvers::TimelogResolver
field :board_list, ::Types::BoardListType,
null: true,
resolver: Resolvers::BoardListResolver
def design_management
DesignManagementObject.new(nil)
end
......
# frozen_string_literal: true
class ListPolicy < BasePolicy # rubocop:disable Gitlab/NamespacedClass
delegate { @subject.board.resource_parent }
end
......@@ -36,6 +36,19 @@ in [Removed Items](../removed_items.md).
The `Query` type contains the API's top-level entry points for all executable queries.
### `Query.boardList`
Find an issue board list.
Returns [`BoardList`](#boardlist).
#### Arguments
| Name | Type | Description |
| ---- | ---- | ----------- |
| <a id="queryboardlistid"></a>`id` | [`ListID!`](#listid) | Global ID of the list. |
| <a id="queryboardlistissuefilters"></a>`issueFilters` | [`BoardIssueInput`](#boardissueinput) | Filters applied when getting issue metadata in the board list. |
### `Query.ciApplicationSettings`
CI related settings that apply to the entire instance.
......
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Resolvers::BoardListResolver do
include GraphqlHelpers
include Gitlab::Graphql::Laziness
let_it_be(:guest) { create(:user) }
let_it_be(:unauth_user) { create(:user) }
let_it_be(:group) { create(:group, :private) }
let_it_be(:group_label) { create(:group_label, group: group, name: 'Development') }
let_it_be(:board) { create(:board, resource_parent: group) }
let_it_be(:label_list) { create(:list, board: board, label: group_label) }
describe '#resolve' do
subject { resolve_board_list(args: { id: global_id_of(label_list) }, current_user: current_user) }
context 'with unauthorized user' do
let(:current_user) { unauth_user }
it { is_expected.to be_nil }
end
context 'when authorized' do
let(:current_user) { guest }
before do
group.add_guest(guest)
end
it { is_expected.to eq label_list }
end
end
def resolve_board_list(args: {}, current_user: user)
force(resolve(described_class, obj: nil, args: args, ctx: { current_user: current_user }))
end
end
......@@ -27,6 +27,7 @@ RSpec.describe GitlabSchema.types['Query'] do
runner
runners
timelogs
board_list
]
expect(described_class).to have_graphql_fields(*expected_fields).at_least
......@@ -136,4 +137,14 @@ RSpec.describe GitlabSchema.types['Query'] do
is_expected.to have_graphql_resolver(Resolvers::TimelogResolver)
end
end
describe 'boardList field' do
subject { described_class.fields['boardList'] }
it 'finds a board list by its gid' do
is_expected.to have_graphql_arguments(:id, :issue_filters)
is_expected.to have_graphql_type(Types::BoardListType)
is_expected.to have_graphql_resolver(Resolvers::BoardListResolver)
end
end
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe 'Querying a Board list' do
include GraphqlHelpers
let_it_be(:current_user) { create(:user) }
let_it_be(:project) { create(:project) }
let_it_be(:board) { create(:board, resource_parent: project) }
let_it_be(:label) { create(:label, project: project, name: 'foo') }
let_it_be(:list) { create(:list, board: board, label: label) }
let_it_be(:issue1) { create(:issue, project: project, labels: [label]) }
let_it_be(:issue2) { create(:issue, project: project, labels: [label], assignees: [current_user]) }
let(:filters) { {} }
let(:query) do
graphql_query_for(
:board_list,
{ id: list.to_global_id.to_s, issueFilters: filters },
%w[title issuesCount]
)
end
subject { graphql_data['boardList'] }
before do
post_graphql(query, current_user: current_user)
end
context 'when the user has access to the list' do
before_all do
project.add_guest(current_user)
end
it_behaves_like 'a working graphql query'
it { is_expected.to include({ 'issuesCount' => 2, 'title' => list.title }) }
context 'with matching issue filters' do
let(:filters) { { assigneeUsername: current_user.username } }
it 'filters issues metadata' do
is_expected.to include({ 'issuesCount' => 1, 'title' => list.title })
end
end
context 'with unmatching issue filters' do
let(:filters) { { assigneeUsername: 'foo' } }
it 'filters issues metadata' do
is_expected.to include({ 'issuesCount' => 0, 'title' => list.title })
end
end
end
context 'when the user does not have access to the list' do
it { is_expected.to be_nil }
end
context 'when ID argument is missing' do
let(:query) do
graphql_query_for('boardList', {}, 'title')
end
it 'raises an exception' do
expect(graphql_errors).to include(a_hash_including('message' => "Field 'boardList' is missing required arguments: id"))
end
end
context 'when list ID is not found' do
let(:query) do
graphql_query_for('boardList', { id: "gid://gitlab/List/#{non_existing_record_id}" }, 'title')
end
it { is_expected.to be_nil }
end
it 'does not have an N+1 performance issue' do
a, b = create_list(:list, 2, board: board)
ctx = { current_user: current_user }
project.add_guest(current_user)
baseline = graphql_query_for(:board_list, { id: global_id_of(a) }, 'title')
query = <<~GQL
query {
a: #{query_graphql_field(:board_list, { id: global_id_of(a) }, 'title')}
b: #{query_graphql_field(:board_list, { id: global_id_of(b) }, 'title')}
}
GQL
control = ActiveRecord::QueryRecorder.new do
run_with_clean_state(baseline, context: ctx)
end
expect { run_with_clean_state(query, context: ctx) }.not_to exceed_query_limit(control)
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