Commit 4d9b43f1 authored by Jan Provaznik's avatar Jan Provaznik

Merge branch '334993-graphql-support-for-fetching-descendant-groups' into 'master'

Add descendantGroups field to Group type

See merge request gitlab-org/gitlab!65255
parents ef96ae67 9a5dd5ea
......@@ -11,6 +11,7 @@
# parent: Group
# all_available: boolean (defaults to true)
# min_access_level: integer
# search: string
# exclude_group_ids: array of integers
# include_parent_descendants: boolean (defaults to false) - includes descendant groups when
# filtering by parent. The parent param must be present.
......@@ -33,6 +34,7 @@ class GroupsFinder < UnionFinder
item = by_parent(item)
item = by_custom_attributes(item)
item = exclude_group_ids(item)
item = by_search(item)
item
end
......@@ -94,6 +96,15 @@ class GroupsFinder < UnionFinder
end
# rubocop: enable CodeReuse/ActiveRecord
# rubocop: disable CodeReuse/ActiveRecord
def by_search(groups)
return groups unless params[:search].present?
search_in_descendant_groups = params[:parent].present? && include_parent_descendants?
groups.search(params[:search], include_parents: !search_in_descendant_groups)
end
# rubocop: enable CodeReuse/ActiveRecord
def owned_groups
current_user&.owned_groups || Group.none
end
......
# frozen_string_literal: true
module Resolvers
class GroupsResolver < BaseResolver
type Types::GroupType, null: true
argument :include_parent_descendants, GraphQL::Types::Boolean,
required: false,
description: 'List of descendant groups of the parent group.',
default_value: true
argument :owned, GraphQL::Types::Boolean,
required: false,
description: 'Limit result to groups owned by authenticated user.'
argument :search, GraphQL::Types::String,
required: false,
description: 'Search query for group name or group full path.'
alias_method :parent, :object
def resolve(**args)
return [] unless parent.present?
find_groups(args)
end
private
# rubocop: disable CodeReuse/ActiveRecord
def find_groups(args)
GroupsFinder
.new(context[:current_user], args.merge(parent: parent))
.execute
.reorder('name ASC')
end
# rubocop: enable CodeReuse/ActiveRecord
end
end
......@@ -149,6 +149,12 @@ module Types
complexity: 5,
resolver: ::Resolvers::TimelogResolver
field :descendant_groups, Types::GroupType.connection_type,
null: true,
description: 'List of descendant groups of this group.',
complexity: 5,
resolver: Resolvers::GroupsResolver
def avatar_url
object.avatar_url(only_path: false)
end
......
......@@ -5485,6 +5485,29 @@ The edge type for [`Event`](#event).
| <a id="eventedgecursor"></a>`cursor` | [`String!`](#string) | A cursor for use in pagination. |
| <a id="eventedgenode"></a>`node` | [`Event`](#event) | The item at the end of the edge. |
#### `GroupConnection`
The connection type for [`Group`](#group).
##### Fields
| Name | Type | Description |
| ---- | ---- | ----------- |
| <a id="groupconnectionedges"></a>`edges` | [`[GroupEdge]`](#groupedge) | A list of edges. |
| <a id="groupconnectionnodes"></a>`nodes` | [`[Group]`](#group) | A list of nodes. |
| <a id="groupconnectionpageinfo"></a>`pageInfo` | [`PageInfo!`](#pageinfo) | Information to aid in pagination. |
#### `GroupEdge`
The edge type for [`Group`](#group).
##### Fields
| Name | Type | Description |
| ---- | ---- | ----------- |
| <a id="groupedgecursor"></a>`cursor` | [`String!`](#string) | A cursor for use in pagination. |
| <a id="groupedgenode"></a>`node` | [`Group`](#group) | The item at the end of the edge. |
#### `GroupMemberConnection`
The connection type for [`GroupMember`](#groupmember).
......@@ -9385,6 +9408,24 @@ four standard [pagination arguments](#connection-pagination-arguments):
| <a id="groupcontainerrepositoriesname"></a>`name` | [`String`](#string) | Filter the container repositories by their name. |
| <a id="groupcontainerrepositoriessort"></a>`sort` | [`ContainerRepositorySort`](#containerrepositorysort) | Sort container repositories by this criteria. |
##### `Group.descendantGroups`
List of descendant groups of this group.
Returns [`GroupConnection`](#groupconnection).
This field returns a [connection](#connections). It accepts the
four standard [pagination arguments](#connection-pagination-arguments):
`before: String`, `after: String`, `first: Int`, `last: Int`.
###### Arguments
| Name | Type | Description |
| ---- | ---- | ----------- |
| <a id="groupdescendantgroupsincludeparentdescendants"></a>`includeParentDescendants` | [`Boolean`](#boolean) | List of descendant groups of the parent group. |
| <a id="groupdescendantgroupsowned"></a>`owned` | [`Boolean`](#boolean) | Limit result to groups owned by authenticated user. |
| <a id="groupdescendantgroupssearch"></a>`search` | [`String`](#string) | Search query for group name or group full path. |
##### `Group.epic`
Find a single epic.
......
......@@ -228,6 +228,38 @@ RSpec.describe GroupsFinder do
)
end
end
context 'with search' do
it 'does not search in full path' do
params[:search] = public_subgroup.path
expect(described_class.new(user, params).execute).to contain_exactly(public_subgroup)
end
end
end
context 'with search' do
let_it_be(:parent_group) { create(:group, :public, name: 'Parent Group') }
let_it_be(:test_group) { create(:group, :public, path: 'test-path') }
it 'returns all groups with matching title' do
expect(described_class.new(user, { search: 'parent' }).execute).to contain_exactly(parent_group)
end
it 'returns all groups with matching path' do
expect(described_class.new(user, { search: 'test' }).execute).to contain_exactly(test_group)
end
context 'with group descendants' do
let_it_be(:sub_group) { create(:group, :public, name: 'Sub Group', parent: parent_group) }
let(:params) { { search: parent_group.path } }
it 'searches in full path if descendant groups are not included' do
params[:include_parent_descendants] = false
expect(described_class.new(user, params).execute).to contain_exactly(parent_group, sub_group)
end
end
end
end
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Resolvers::GroupsResolver do
include GraphqlHelpers
describe '#resolve' do
let_it_be(:group) { create(:group, name: 'public-group') }
let_it_be(:private_group) { create(:group, :private, name: 'private-group') }
let_it_be(:subgroup1) { create(:group, parent: group, name: 'Subgroup') }
let_it_be(:subgroup2) { create(:group, parent: subgroup1, name: 'Test Subgroup 2') }
let_it_be(:private_subgroup1) { create(:group, :private, parent: private_group, name: 'Subgroup1') }
let_it_be(:private_subgroup2) { create(:group, :private, parent: private_subgroup1, name: 'Subgroup2') }
let_it_be(:user) { create(:user) }
before_all do
private_group.add_developer(user)
end
shared_examples 'access to all public descendant groups' do
it 'returns all public descendant groups of the parent group ordered by ASC name' do
is_expected.to eq([subgroup1, subgroup2])
end
end
shared_examples 'access to all public subgroups' do
it 'returns all public subgroups of the parent group' do
is_expected.to contain_exactly(subgroup1)
end
end
shared_examples 'returning empty results' do
it 'returns empty results' do
is_expected.to be_empty
end
end
context 'when parent group is public' do
subject { resolve(described_class, obj: group, args: params, ctx: { current_user: current_user }) }
context 'when `include_parent_descendants` is false' do
let(:params) { { include_parent_descendants: false } }
context 'when user is not logged in' do
let(:current_user) { nil }
it_behaves_like 'access to all public subgroups'
end
context 'when user is logged in' do
let(:current_user) { user }
it_behaves_like 'access to all public subgroups'
end
end
context 'when `include_parent_descendants` is true' do
let(:params) { { include_parent_descendants: true } }
context 'when user is not logged in' do
let(:current_user) { nil }
it_behaves_like 'access to all public descendant groups'
end
context 'when user is logged in' do
let(:current_user) { user }
it_behaves_like 'access to all public descendant groups'
context 'with owned argument set as true' do
before do
subgroup1.add_owner(current_user)
params[:owned] = true
end
it 'returns only descendant groups owned by the user' do
is_expected.to contain_exactly(subgroup1)
end
end
context 'with search argument' do
it 'returns only descendant groups with matching name or path' do
params[:search] = 'Test'
is_expected.to contain_exactly(subgroup2)
end
end
end
end
end
context 'when parent group is private' do
subject { resolve(described_class, obj: private_group, args: params, ctx: { current_user: current_user }) }
context 'when `include_parent_descendants` is true' do
let(:params) { { include_parent_descendants: true } }
context 'when user is not logged in' do
let(:current_user) { nil }
it_behaves_like 'returning empty results'
end
context 'when user is logged in' do
let(:current_user) { user }
it 'returns all private descendant groups' do
is_expected.to contain_exactly(private_subgroup1, private_subgroup2)
end
end
end
context 'when `include_parent_descendants` is false' do
let(:params) { { include_parent_descendants: false } }
context 'when user is not logged in' do
let(:current_user) { nil }
it_behaves_like 'returning empty results'
end
context 'when user is logged in' do
let(:current_user) { user }
it 'returns private subgroups' do
is_expected.to contain_exactly(private_subgroup1)
end
end
end
end
end
end
......@@ -8,11 +8,11 @@ RSpec.describe 'getting group information' do
include GraphqlHelpers
include UploadHelpers
let(:user1) { create(:user, can_create_group: false) }
let(:user2) { create(:user) }
let(:admin) { create(:admin) }
let(:public_group) { create(:group, :public) }
let(:private_group) { create(:group, :private) }
let_it_be(:user1) { create(:user, can_create_group: false) }
let_it_be(:user2) { create(:user) }
let_it_be(:admin) { create(:admin) }
let_it_be(:private_group) { create(:group, :private) }
let_it_be(:public_group) { create(:group, :public) }
# similar to the API "GET /groups/:id"
describe "Query group(fullPath)" do
......@@ -105,6 +105,20 @@ RSpec.describe 'getting group information' do
expect { post_multiplex(queries, current_user: admin) }
.to issue_same_number_of_queries_as { post_graphql(group_query(group1), current_user: admin) }
end
context "when querying group's descendant groups" do
let_it_be(:subgroup1) { create(:group, parent: public_group) }
let_it_be(:subgroup2) { create(:group, parent: subgroup1) }
let(:descendants) { [subgroup1, subgroup2] }
it 'returns all descendant groups user has access to' do
post_graphql(group_query(public_group), current_user: admin)
names = graphql_data['group']['descendantGroups']['nodes'].map { |n| n['name'] }
expect(names).to match_array(descendants.map(&:name))
end
end
end
context "when authenticated as admin" do
......
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