Skip to content

Commit

Permalink
Membership: Query memberships of an organization
Browse files Browse the repository at this point in the history
  • Loading branch information
ansmonjol committed Sep 6, 2022
1 parent 9339280 commit 53048e2
Show file tree
Hide file tree
Showing 5 changed files with 213 additions and 0 deletions.
25 changes: 25 additions & 0 deletions app/graphql/resolvers/memberships_resolver.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# frozen_string_literal: true

module Resolvers
class MembershipsResolver < GraphQL::Schema::Resolver
include AuthenticableApiUser
include RequiredOrganization

description 'Query memberships of an organization'

argument :page, Integer, required: false
argument :limit, Integer, required: false

type Types::MembershipType.collection_type, null: false

def resolve(page: nil, limit: nil)
validate_organization!

current_organization
.memberships
.active
.page(page)
.per(limit)
end
end
end
1 change: 1 addition & 0 deletions app/graphql/types/query_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@ class QueryType < Types::BaseObject
field :wallet, resolver: Resolvers::WalletResolver
field :wallet_transactions, resolver: Resolvers::WalletTransactionsResolver
field :wallet_transaction, resolver: Resolvers::WalletTransactionResolver
field :memberships, resolver: Resolvers::MembershipsResolver
end
end
15 changes: 15 additions & 0 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -2726,6 +2726,16 @@ type Membership {
userId: Int!
}

type MembershipCollection {
collection: [Membership!]!
metadata: CollectionMetadata!
}

enum MembershipStatus {
active
revoked
}

type Mutation {
"""
Add or update Stripe API keys to the organization
Expand Down Expand Up @@ -3269,6 +3279,11 @@ type Query {
"""
events(limit: Int, page: Int): EventCollection

"""
Query memberships of an organization
"""
memberships(limit: Int, page: Int): MembershipCollection!

"""
Query a single plan of an organization
"""
Expand Down
121 changes: 121 additions & 0 deletions schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -8889,6 +8889,86 @@
"inputFields": null,
"enumValues": null
},
{
"kind": "OBJECT",
"name": "MembershipCollection",
"description": null,
"interfaces": [

],
"possibleTypes": null,
"fields": [
{
"name": "collection",
"description": null,
"type": {
"kind": "NON_NULL",
"name": null,
"ofType": {
"kind": "LIST",
"name": null,
"ofType": {
"kind": "NON_NULL",
"name": null,
"ofType": {
"kind": "OBJECT",
"name": "Membership",
"ofType": null
}
}
}
},
"isDeprecated": false,
"deprecationReason": null,
"args": [

]
},
{
"name": "metadata",
"description": null,
"type": {
"kind": "NON_NULL",
"name": null,
"ofType": {
"kind": "OBJECT",
"name": "CollectionMetadata",
"ofType": null
}
},
"isDeprecated": false,
"deprecationReason": null,
"args": [

]
}
],
"inputFields": null,
"enumValues": null
},
{
"kind": "ENUM",
"name": "MembershipStatus",
"description": null,
"interfaces": null,
"possibleTypes": null,
"fields": null,
"inputFields": null,
"enumValues": [
{
"name": "active",
"description": null,
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "revoked",
"description": null,
"isDeprecated": false,
"deprecationReason": null
}
]
},
{
"kind": "OBJECT",
"name": "Mutation",
Expand Down Expand Up @@ -11378,6 +11458,47 @@
}
]
},
{
"name": "memberships",
"description": "Query memberships of an organization",
"type": {
"kind": "NON_NULL",
"name": null,
"ofType": {
"kind": "OBJECT",
"name": "MembershipCollection",
"ofType": null
}
},
"isDeprecated": false,
"deprecationReason": null,
"args": [
{
"name": "page",
"description": null,
"type": {
"kind": "SCALAR",
"name": "Int",
"ofType": null
},
"defaultValue": null,
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "limit",
"description": null,
"type": {
"kind": "SCALAR",
"name": "Int",
"ofType": null
},
"defaultValue": null,
"isDeprecated": false,
"deprecationReason": null
}
]
},
{
"name": "plan",
"description": "Query a single plan of an organization",
Expand Down
51 changes: 51 additions & 0 deletions spec/graphql/resolvers/memberships_resolver_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe Resolvers::MembershipsResolver, type: :graphql do
let(:query) do
<<~GQL
query {
memberships(limit: 5) {
collection { id }
metadata { currentPage, totalCount }
}
}
GQL
end

let(:membership) { create(:membership) }
let(:organization) { membership.organization }

it 'returns a list of memberships' do
result = execute_graphql(
current_user: membership.user,
current_organization: organization,
query: query,
)

memberships_response = result['data']['memberships']

aggregate_failures do
expect(memberships_response['collection'].count).to eq(organization.memberships.count)
expect(memberships_response['collection'].first['id']).to eq(membership.id)

expect(memberships_response['metadata']['currentPage']).to eq(1)
expect(memberships_response['metadata']['totalCount']).to eq(1)
end
end

context 'without current organization' do
it 'returns an error' do
result = execute_graphql(
current_user: membership.user,
query: query,
)

expect_graphql_error(
result: result,
message: 'Missing organization id',
)
end
end
end

0 comments on commit 53048e2

Please sign in to comment.