Skip to content

Commit 1772252

Browse files
authored
Merge pull request #651 from code0-tech/650-add-top-level-organizations-field
Add Query.organizations field
2 parents 65144a9 + 03c150e commit 1772252

File tree

8 files changed

+120
-0
lines changed

8 files changed

+120
-0
lines changed

app/finders/organizations_finder.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ def execute
55
organizations = base_scope
66
organizations = by_id(organizations)
77
organizations = by_name(organizations)
8+
organizations = by_namespace_member_user(organizations)
89

910
super(organizations)
1011
end
@@ -26,4 +27,15 @@ def by_name(organizations)
2627

2728
organizations.where(name: params[:name])
2829
end
30+
31+
def by_namespace_member_user(organizations)
32+
return organizations unless params.key?(:namespace_member_user)
33+
return Organization.none if params[:namespace_member_user].nil?
34+
35+
namespaces = NamespaceMember.where(user_id: params[:namespace_member_user][:id]).select(:namespace_id)
36+
37+
organizations.where(
38+
id: Namespace.where(id: namespaces, parent_type: 'Organization').select(:parent_id)
39+
)
40+
end
2941
end

app/graphql/types/query_type.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ class QueryType < Types::BaseObject
3434
require_one_of %i[id name]
3535
end
3636

37+
field :organizations, Types::OrganizationType.connection_type, null: false, description: 'Find organizations'
38+
3739
field :namespace, Types::NamespaceType, null: true, description: 'Find a namespace' do
3840
argument :id, Types::GlobalIdType[::Namespace], required: true, description: 'GlobalID of the target namespace'
3941
end
@@ -64,6 +66,10 @@ def organization(**args)
6466
OrganizationsFinder.new(**args, single: true).execute
6567
end
6668

69+
def organizations
70+
OrganizationsFinder.new(namespace_member_user: current_user).execute
71+
end
72+
6773
def namespace(id:)
6874
SagittariusSchema.object_from_id(id)
6975
end
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
title: OrganizationConnection
3+
---
4+
5+
The connection type for Organization.
6+
7+
## Fields without arguments
8+
9+
| Name | Type | Description |
10+
|------|------|-------------|
11+
| `count` | [`Int!`](../scalar/int.md) | Total count of collection. |
12+
| `edges` | [`[OrganizationEdge]`](../object/organizationedge.md) | A list of edges. |
13+
| `nodes` | [`[Organization]`](../object/organization.md) | A list of nodes. |
14+
| `pageInfo` | [`PageInfo!`](../object/pageinfo.md) | Information to aid in pagination. |
15+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
title: OrganizationEdge
3+
---
4+
5+
An edge in a connection.
6+
7+
## Fields without arguments
8+
9+
| Name | Type | Description |
10+
|------|------|-------------|
11+
| `cursor` | [`String!`](../scalar/string.md) | A cursor for use in pagination. |
12+
| `node` | [`Organization`](../object/organization.md) | The item at the end of the edge. |
13+

docs/graphql/object/query.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Root Query type
1212
| `currentAuthentication` | [`Authentication`](../union/authentication.md) | Get the currently logged in authentication |
1313
| `currentUser` | [`User`](../object/user.md) | Get the currently logged in user |
1414
| `globalRuntimes` | [`RuntimeConnection!`](../object/runtimeconnection.md) | Find runtimes |
15+
| `organizations` | [`OrganizationConnection!`](../object/organizationconnection.md) | Find organizations |
1516
| `users` | [`UserConnection!`](../object/userconnection.md) | Find users |
1617

1718
## Fields with arguments

spec/finders/organizations_finder_spec.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,23 @@
2626
it { is_expected.to contain_exactly(second_organization) }
2727
end
2828

29+
context 'when filtering by namespace member' do
30+
let(:user) { create(:user) }
31+
let(:params) { { namespace_member_user: user } }
32+
33+
before do
34+
create(:namespace_member, user: user, namespace: first_organization.ensure_namespace)
35+
end
36+
37+
it { is_expected.to contain_exactly(first_organization) }
38+
39+
context 'when filtered with null user' do
40+
let(:params) { { namespace_member_user: nil } }
41+
42+
it { is_expected.to be_empty }
43+
end
44+
end
45+
2946
context 'when setting limit' do
3047
let(:params) { { limit: 1 } }
3148

spec/graphql/types/query_type_spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
currentUser
1111
echo
1212
organization
13+
organizations
1314
users
1415
global_runtimes
1516
namespace
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# frozen_string_literal: true
2+
3+
require 'rails_helper'
4+
5+
RSpec.describe 'organization Query' do
6+
include GraphqlHelpers
7+
8+
subject(:query!) { post_graphql query, current_user: current_user }
9+
10+
let(:query) do
11+
<<~QUERY
12+
query {
13+
organizations {
14+
nodes {
15+
id
16+
name
17+
}
18+
}
19+
}
20+
QUERY
21+
end
22+
let(:current_user) { nil }
23+
let!(:first_organization) { create(:organization) }
24+
let!(:second_organization) { create(:organization) }
25+
26+
before do
27+
create(:organization) # organization where the user isn't a member
28+
end
29+
30+
context 'when anonymous' do
31+
it 'does not return organizations' do
32+
query!
33+
34+
expect(graphql_data_at(:organizations, :nodes)).to be_empty
35+
end
36+
end
37+
38+
context 'when logged in' do
39+
let(:current_user) { create(:user) }
40+
41+
before do
42+
create(:namespace_member, namespace: first_organization.ensure_namespace, user: current_user)
43+
create(:namespace_member, namespace: second_organization.ensure_namespace, user: current_user)
44+
end
45+
46+
it 'returns organizations where user is member' do
47+
query!
48+
49+
expect(graphql_data_at(:organizations, :nodes)).to contain_exactly(
50+
a_graphql_entity_for(first_organization),
51+
a_graphql_entity_for(second_organization)
52+
)
53+
end
54+
end
55+
end

0 commit comments

Comments
 (0)