diff --git a/terraso_backend/apps/graphql/schema/schema.graphql b/terraso_backend/apps/graphql/schema/schema.graphql index f23741234..90bb0378d 100644 --- a/terraso_backend/apps/graphql/schema/schema.graphql +++ b/terraso_backend/apps/graphql/schema/schema.graphql @@ -30,7 +30,7 @@ type Query { ): GroupAssociationNode! groups(offset: Int, before: String, after: String, first: Int, last: Int, name: String, name_Icontains: String, name_Istartswith: String, slug: String, slug_Icontains: String, description_Icontains: String, memberships_Email: String, associatedLandscapes_IsDefaultLandscapeGroup: Boolean, associatedLandscapes_Isnull: Boolean, associatedLandscapes_IsPartnership: Boolean): GroupNodeConnection landscapes(offset: Int, before: String, after: String, first: Int, last: Int, name_Icontains: String, description_Icontains: String, slug: String, slug_Icontains: String, website_Icontains: String, location_Icontains: String): LandscapeNodeConnection - users(offset: Int, before: String, after: String, first: Int, last: Int, email: String, email_Icontains: String, firstName_Icontains: String, lastName_Icontains: String): UserNodeConnection + users(offset: Int, before: String, after: String, first: Int, last: Int, email: String, email_Icontains: String, firstName_Icontains: String, lastName_Icontains: String, project: String): UserNodeConnection landscapeGroups(offset: Int, before: String, after: String, first: Int, last: Int, landscape: ID, landscape_Slug_Icontains: String, group: ID, group_Slug_Icontains: String, isDefaultLandscapeGroup: Boolean, isPartnership: Boolean): LandscapeGroupNodeConnection memberships(offset: Int, before: String, after: String, first: Int, last: Int, group: ID, group_In: [ID], group_Slug_Icontains: String, group_Slug_In: [String], user: ID, user_In: [ID], userRole: CoreMembershipUserRoleChoices, user_Email_Icontains: String, user_Email_In: [String], membershipStatus: CoreMembershipMembershipStatusChoices): MembershipNodeConnection groupAssociations(offset: Int, before: String, after: String, first: Int, last: Int, parentGroup: ID, childGroup: ID, parentGroup_Slug_Icontains: String, childGroup_Slug_Icontains: String): GroupAssociationNodeConnection diff --git a/terraso_backend/apps/graphql/schema/users.py b/terraso_backend/apps/graphql/schema/users.py index c648103e1..7da5c1874 100644 --- a/terraso_backend/apps/graphql/schema/users.py +++ b/terraso_backend/apps/graphql/schema/users.py @@ -16,8 +16,10 @@ import graphene import rules import structlog +from django_filters import FilterSet from graphene import relay from graphene_django import DjangoObjectType +from graphene_django.filter import TypedFilter from apps.auth.services import JWTService from apps.core.models import User, UserPreference @@ -35,19 +37,27 @@ logger = structlog.get_logger(__name__) -class UserNode(DjangoObjectType): - id = graphene.ID(source="pk", required=True) +class UserFilter(FilterSet): + project = TypedFilter(field_name="collaboration_memberships__membership_list__project") class Meta: model = User - filter_fields = { + fields = { "email": ["exact", "icontains"], "first_name": ["icontains"], "last_name": ["icontains"], } - fields = ("email", "first_name", "last_name", "profile_image", "memberships", "preferences") + + +class UserNode(DjangoObjectType): + id = graphene.ID(source="pk", required=True) + + class Meta: + model = User + filterset_class = UserFilter interfaces = (relay.Node,) connection_class = TerrasoConnection + fields = ("email", "first_name", "last_name", "profile_image", "memberships", "preferences") class UserPreferenceNode(DjangoObjectType): diff --git a/terraso_backend/tests/graphql/test_users.py b/terraso_backend/tests/graphql/test_users.py index 90ca151ed..71e46b2b1 100644 --- a/terraso_backend/tests/graphql/test_users.py +++ b/terraso_backend/tests/graphql/test_users.py @@ -74,3 +74,23 @@ def test_users_query_has_total_count(client_query, users): total_count = response.json()["data"]["users"]["totalCount"] assert total_count == len(users) + + +def test_users_query_in_project(client_query, project, project_user): + response = client_query( + """ + query userInProject($projectId: String!, $email: String!) { + users(project: $projectId, email: $email) { + edges { + node { + id + } + } + } + } + """, + variables={"projectId": str(project.id), "email": project_user.email}, + ) + contents = response.json() + assert "errors" not in contents + assert contents["data"]["users"]["edges"][0]["node"]["id"] == str(project_user.id)