Skip to content

Commit

Permalink
fix: Restore missing membership_lists filter
Browse files Browse the repository at this point in the history
This can be done be reusing the filterset class with
DjangoFilterConnectionField. It would be worthwhile checking if this
work can be encapsulated in a resuable function, as there is now a lot
of custom code whose function is not obvious.
  • Loading branch information
David Code Howard committed Sep 21, 2023
1 parent e43899e commit a7c38d6
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 6 deletions.
24 changes: 22 additions & 2 deletions terraso_backend/apps/collaboration/graphql/memberships.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,33 @@
from graphene import relay
from graphene_django import DjangoObjectType

from apps.graphql.schema.commons import TerrasoConnection

from ..models import Membership, MembershipList

# from apps.graphql.schema.commons import TerrasoConnection


logger = structlog.get_logger(__name__)


# TODO: trying to import this from apps.graphql.schema.commons causes a circular import
# Created an issue to move the module to apps.graphql.commons, as that seems simplest
# https://github.com/techmatters/terraso-backend/issues/820
class TerrasoConnection(graphene.Connection):
class Meta:
abstract = True

total_count = graphene.Int(required=True)

def resolve_total_count(self, info, **kwargs):
queryset = self.iterable
return queryset.count()

@classmethod
def __init_subclass_with_meta__(cls, **options):
options["strict_types"] = options.pop("strict_types", True)
super().__init_subclass_with_meta__(**options)


class MembershipListNodeMixin:
id = graphene.ID(source="pk", required=True)
account_membership = graphene.Field("apps.collaboration.graphql.CollaborationMembershipNode")
Expand Down
20 changes: 19 additions & 1 deletion terraso_backend/apps/graphql/schema/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,7 @@ type StoryMapNode implements Node {

type ProjectMembershipListNode implements Node {
membershipType: CollaborationMembershipListMembershipTypeChoices!
memberships: ProjectMembershipNode!
memberships(offset: Int, before: String, after: String, first: Int, last: Int, user: ID, user_In: [ID], userRole: UserRole, user_Email_Icontains: String, user_Email_In: [String], membershipStatus: CollaborationMembershipMembershipStatusChoices, user_Email_Not: String): ProjectMembershipNodeConnection
id: ID!
accountMembership: CollaborationMembershipNode
membershipsCount: Int
Expand All @@ -576,6 +576,24 @@ enum CollaborationMembershipListMembershipTypeChoices {
CLOSED
}

type ProjectMembershipNodeConnection {
"""Pagination data for this connection."""
pageInfo: PageInfo!

"""Contains the nodes in this connection."""
edges: [ProjectMembershipNodeEdge!]!
totalCount: Int!
}

"""A Relay edge containing a `ProjectMembershipNode` and its cursor."""
type ProjectMembershipNodeEdge {
"""The item at the end of the edge"""
node: ProjectMembershipNode!

"""A cursor for use in pagination"""
cursor: String!
}

type ProjectMembershipNode implements Node {
membershipList: ProjectMembershipListNode!
user: UserNode
Expand Down
11 changes: 8 additions & 3 deletions terraso_backend/apps/project_management/graphql/projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@
from django_filters import FilterSet
from graphene import relay
from graphene_django import DjangoObjectType
from graphene_django.filter import TypedFilter
from graphene_django.filter import DjangoFilterConnectionField, TypedFilter

from apps.audit_logs import api as log_api
from apps.collaboration.graphql.memberships import (
CollaborationMembershipFilterSet as MembershipFilterSet,
)
from apps.collaboration.graphql.memberships import (
CollaborationMembershipNode as MembershipNode,
)
Expand Down Expand Up @@ -76,11 +79,13 @@ def resolve_user_role(self, info):


class ProjectMembershipListNode(DjangoObjectType, MembershipListNodeMixin):
memberships = graphene.Field(ProjectMembershipNode, required=True)

class Meta(MembershipListNodeMixin.Meta):
pass

memberships = DjangoFilterConnectionField(
ProjectMembershipNode, filterset_class=MembershipFilterSet
)


class ProjectFilterSet(FilterSet):
member = TypedFilter(field_name="membership_list__memberships__user")
Expand Down

0 comments on commit a7c38d6

Please sign in to comment.