Skip to content

Commit 9cc1914

Browse files
authored
Merge pull request #933 from mapswipe/feature/graphql-add-id
Feature/graphql add
2 parents f015fd3 + 063ef84 commit 9cc1914

File tree

5 files changed

+49
-7
lines changed

5 files changed

+49
-7
lines changed

community-dashboard/app/components/ItemSelectInput/index.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ const USERS = gql`
3636
query UserOptions($search: String, $offset: Int!, $limit: Int!) {
3737
users(filters: { search: $search }, pagination: { limit: $limit, offset: $offset }) {
3838
items {
39+
id
3940
userId
4041
username
4142
}
@@ -50,6 +51,7 @@ const USER_GROUPS = gql`
5051
query UserGroupOptions($search: String, $offset: Int!, $limit: Int!) {
5152
userGroups(filters: { search: $search }, pagination: { limit: $limit, offset: $offset }) {
5253
items {
54+
id
5355
isArchived
5456
userGroupId
5557
name

community-dashboard/app/views/UserGroupDashboard/index.tsx

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,14 @@ const EXPORT_LIMIT = 500;
3232
const USER_GROUP_STATS = gql`
3333
query UserGroupStats($pk: ID!, $limit: Int!, $offset: Int!) {
3434
userGroup(pk: $pk) {
35+
id
3536
userGroupId
3637
name
3738
description
3839
userMemberships(pagination: { limit: $limit, offset: $offset }) {
3940
count
4041
items {
42+
id
4143
userId
4244
username
4345
isActive
@@ -48,6 +50,7 @@ const USER_GROUP_STATS = gql`
4850
}
4951
}
5052
userGroupStats(userGroupId: $pk) {
53+
id
5154
stats {
5255
totalContributors
5356
totalSwipes
@@ -65,6 +68,7 @@ const USER_GROUP_STATS = gql`
6568
const FILTERED_USER_GROUP_STATS = gql`
6669
query FilteredUserGroupStats($pk: ID!, $fromDate: DateTime! $toDate: DateTime!) {
6770
userGroupStats(userGroupId: $pk) {
71+
id
6872
filteredStats(dateRange: { fromDate: $fromDate, toDate: $toDate}) {
6973
userStats {
7074
totalMappingProjects
@@ -111,11 +115,13 @@ const USER_MEMBERSHIPS_EXPORT = gql`
111115
$offset: Int!,
112116
) {
113117
userGroup(pk: $pk) {
118+
id
114119
userMemberships(pagination: { limit: $limit, offset: $offset }) {
115120
count
116121
limit
117122
offset
118123
items {
124+
id
119125
userId
120126
username
121127
isActive
@@ -215,11 +221,9 @@ function UserGroupDashboard(props: Props) {
215221
onCompleted: (response) => {
216222
const result = response?.userGroup?.userMemberships;
217223
const userMembershipsCount = response?.userGroup?.userMemberships?.count ?? 0;
224+
const newUserMembershipsData = [...userMembershipsData, ...(result?.items ?? [])];
218225

219-
setUserMembershipsData((prevValue) => [...prevValue, ...result?.items ?? []]);
220-
setOffset((prevValue) => prevValue + EXPORT_LIMIT);
221-
222-
if (userMembershipsData?.length < userMembershipsCount) {
226+
if (newUserMembershipsData?.length < userMembershipsCount) {
223227
setExportPending(true);
224228
exportUserMembership({
225229
variables: userGroupId ? ({
@@ -230,10 +234,10 @@ function UserGroupDashboard(props: Props) {
230234
});
231235
}
232236

233-
if (userMembershipsData?.length === userMembershipsCount) {
237+
if (newUserMembershipsData.length === userMembershipsCount) {
234238
const userGroupData = [
235239
['User', 'Total swipes', 'Project contributed', 'Time spent(mins)'],
236-
...(userMembershipsData?.map((user) => (
240+
...(newUserMembershipsData?.map((user) => (
237241
[
238242
user.username,
239243
user.totalSwipes,
@@ -263,6 +267,8 @@ function UserGroupDashboard(props: Props) {
263267
window.URL.revokeObjectURL(objUrl);
264268
setExportPending(false);
265269
}
270+
setOffset((prevValue) => prevValue + EXPORT_LIMIT);
271+
setUserMembershipsData(() => newUserMembershipsData);
266272
},
267273
onError: (err) => {
268274
// NOTE: we don't show any alert on failure and success for now

django/apps/existing_database/test_queries.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,7 @@ def test_user_query(self):
309309
query = """
310310
query MyQuery($userId: ID!, $pagination: OffsetPaginationInput!) {
311311
user(pk: $userId) {
312+
id
312313
userId
313314
username
314315
userInUserGroups(pagination: $pagination) {
@@ -391,6 +392,7 @@ def test_user_query(self):
391392
assert resp == {
392393
"data": {
393394
"user": {
395+
"id": user.user_id,
394396
"userId": user.user_id,
395397
"username": user.username,
396398
"userInUserGroups": {

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: User) -> 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.Value(root.user_id, output_field=models.CharField()),
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)