Skip to content

Commit 2865e55

Browse files
thenav56puranban
authored andcommitted
Add id field on required Nodes
- id has values from real PK table column
1 parent f015fd3 commit 2865e55

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

django/apps/existing_database/types.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,10 @@ def __post_init__(self, user_group_id):
394394
user_group_id=user_group_id
395395
)
396396

397+
@strawberry.field
398+
async def id(self) -> strawberry.ID:
399+
return self._user_group_id
400+
397401
@strawberry.field
398402
async def stats(self) -> UserGroupStatsType:
399403
agg_data = await self.qs.aaggregate(
@@ -449,12 +453,20 @@ class UserUserGroupMembershipType:
449453
user_group_name: str
450454
members_count: int
451455

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

453461
@strawberry_django.type(User)
454462
class UserType:
455463
user_id: strawberry.ID
456464
username: strawberry.auto
457465

466+
@strawberry.field
467+
async def id(self, info: Info, root: UserGroup) -> strawberry.ID:
468+
return root.user_id
469+
458470
@strawberry.field
459471
async def user_in_user_groups(
460472
self,
@@ -470,6 +482,7 @@ async def user_in_user_groups(
470482
).values("user_group_id")
471483
)
472484
.annotate(
485+
_user_id=models.V(root.user_id, output_field=models.IntegerField()),
473486
user_group_name=models.F("name"),
474487
members_count=models.functions.Coalesce(
475488
models.Subquery(
@@ -512,9 +525,14 @@ class ProjectType:
512525
geom: str
513526
organization_name: str
514527

528+
@strawberry.field
529+
async def id(self, info: Info, root: UserGroup) -> strawberry.ID:
530+
return root.project_id
531+
515532

516533
@strawberry.type
517534
class UserGroupUserMembershipType:
535+
id: strawberry.ID
518536
user_id: str
519537
username: str
520538
is_active: bool
@@ -535,6 +553,10 @@ class UserGroupType:
535553
archived_at: strawberry.auto
536554
is_archived: strawberry.auto
537555

556+
@strawberry.field
557+
async def id(self, info: Info, root: UserGroup) -> strawberry.ID:
558+
return root.user_group_id
559+
538560
# TODO: Make this a generic module
539561
@strawberry.field
540562
async def user_memberships(
@@ -579,10 +601,14 @@ def _subquery_generator(agg_func):
579601
offset=pagination.offset,
580602
get_count=lambda: qs.acount(),
581603
queryset=[
582-
UserGroupUserMembershipType(**item)
604+
UserGroupUserMembershipType(
605+
id=f"{item['user_id']}-{item.pop('user_group_id')}",
606+
**item,
607+
)
583608
async for item in paginated_qs.values(
584609
# NOTE: Defining manual select fields since DB doesn't have field id but Django assumes it has
585610
"user_id",
611+
"user_group_id",
586612
"is_active",
587613
# Annotate fields
588614
"username",

django/schema.graphql

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ type ProjectType {
7575
status: String
7676
geom: String
7777
organizationName: String
78+
id: ID!
7879
}
7980

8081
type ProjectTypeAreaStatsType {
@@ -182,6 +183,7 @@ input UserGroupOrder {
182183
}
183184

184185
type UserGroupStats {
186+
id: ID!
185187
stats: UserGroupStatsType!
186188

187189
"""Stats from last 30 days"""
@@ -207,6 +209,7 @@ type UserGroupType {
207209
createdAt: DateTime
208210
archivedAt: DateTime
209211
isArchived: Boolean
212+
id: ID!
210213
userMemberships(pagination: OffsetPaginationInput!): UserGroupUserMembershipTypeCountList!
211214
}
212215

@@ -218,6 +221,7 @@ type UserGroupTypeCountList {
218221
}
219222

220223
type UserGroupUserMembershipType {
224+
id: ID!
221225
userId: String!
222226
username: String!
223227
isActive: Boolean!
@@ -266,6 +270,7 @@ type UserStats {
266270
type UserType {
267271
userId: ID!
268272
username: String
273+
id: ID!
269274
userInUserGroups(pagination: OffsetPaginationInput!): UserUserGroupMembershipTypeCountList!
270275
}
271276

@@ -289,6 +294,7 @@ type UserUserGroupMembershipType {
289294
userGroupId: ID!
290295
userGroupName: String!
291296
membersCount: Int!
297+
id: ID!
292298
}
293299

294300
type UserUserGroupMembershipTypeCountList {

0 commit comments

Comments
 (0)