Skip to content

Commit

Permalink
Add id field on required Nodes
Browse files Browse the repository at this point in the history
- id has values from real PK table column
  • Loading branch information
thenav56 committed May 8, 2024
1 parent adcb1b2 commit 3d17a92
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
24 changes: 23 additions & 1 deletion django/apps/existing_database/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -449,12 +449,20 @@ class UserUserGroupMembershipType:
user_group_name: str
members_count: int

@strawberry.field
async def id(self, info: Info, root: UserGroup) -> strawberry.ID:
return f"{root.user_group_id}-{getattr(root, '_user_id', 'NA')}"


@strawberry_django.type(User)
class UserType:
user_id: strawberry.ID
username: strawberry.auto

@strawberry.field
async def id(self, info: Info, root: UserGroup) -> strawberry.ID:
return root.user_id

@strawberry.field
async def user_in_user_groups(
self,
Expand All @@ -470,6 +478,7 @@ async def user_in_user_groups(
).values("user_group_id")
)
.annotate(
_user_id=models.V(root.user_id, output_field=models.IntegerField()),
user_group_name=models.F("name"),
members_count=models.functions.Coalesce(
models.Subquery(
Expand Down Expand Up @@ -512,9 +521,14 @@ class ProjectType:
geom: str
organization_name: str

@strawberry.field
async def id(self, info: Info, root: UserGroup) -> strawberry.ID:
return root.project_id


@strawberry.type
class UserGroupUserMembershipType:
id: strawberry.ID
user_id: str
username: str
is_active: bool
Expand All @@ -535,6 +549,10 @@ class UserGroupType:
archived_at: strawberry.auto
is_archived: strawberry.auto

@strawberry.field
async def id(self, info: Info, root: UserGroup) -> strawberry.ID:
return root.user_group_id

# TODO: Make this a generic module
@strawberry.field
async def user_memberships(
Expand Down Expand Up @@ -579,10 +597,14 @@ def _subquery_generator(agg_func):
offset=pagination.offset,
get_count=lambda: qs.acount(),
queryset=[
UserGroupUserMembershipType(**item)
UserGroupUserMembershipType(
id=f"{item['user_id']}-{item.pop('user_group_id')}",
**item,
)
async for item in paginated_qs.values(
# NOTE: Defining manual select fields since DB doesn't have field id but Django assumes it has
"user_id",
"user_group_id",
"is_active",
# Annotate fields
"username",
Expand Down
5 changes: 5 additions & 0 deletions django/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ type ProjectType {
status: String
geom: String
organizationName: String
id: ID!
}

type ProjectTypeAreaStatsType {
Expand Down Expand Up @@ -207,6 +208,7 @@ type UserGroupType {
createdAt: DateTime
archivedAt: DateTime
isArchived: Boolean
id: ID!
userMemberships(pagination: OffsetPaginationInput!): UserGroupUserMembershipTypeCountList!
}

Expand All @@ -218,6 +220,7 @@ type UserGroupTypeCountList {
}

type UserGroupUserMembershipType {
id: ID!
userId: String!
username: String!
isActive: Boolean!
Expand Down Expand Up @@ -266,6 +269,7 @@ type UserStats {
type UserType {
userId: ID!
username: String
id: ID!
userInUserGroups(pagination: OffsetPaginationInput!): UserUserGroupMembershipTypeCountList!
}

Expand All @@ -289,6 +293,7 @@ type UserUserGroupMembershipType {
userGroupId: ID!
userGroupName: String!
membersCount: Int!
id: ID!
}

type UserUserGroupMembershipTypeCountList {
Expand Down

0 comments on commit 3d17a92

Please sign in to comment.