Skip to content

Commit dbcf2a3

Browse files
committed
Standardize GQL queries to return empty results when anon
1 parent 8a11cc5 commit dbcf2a3

File tree

4 files changed

+23
-6
lines changed

4 files changed

+23
-6
lines changed

src/components/notifications/notification.resolver.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Args, Mutation, Query, Resolver } from '@nestjs/graphql';
2-
import { ListArg, LoggedInSession, type Session } from '~/common';
2+
import { AnonSession, ListArg, LoggedInSession, type Session } from '~/common';
33
import {
44
MarkNotificationReadArgs,
55
Notification,
@@ -14,9 +14,13 @@ export class NotificationResolver {
1414

1515
@Query(() => NotificationList)
1616
async notifications(
17-
@LoggedInSession() session: Session,
17+
@AnonSession() session: Session,
1818
@ListArg(NotificationListInput) input: NotificationListInput,
1919
): Promise<NotificationList> {
20+
// TODO move to DB layer?
21+
if (session.anonymous) {
22+
return { items: [], total: 0, totalUnread: 0, hasMore: false };
23+
}
2024
return await this.service.list(input, session);
2125
}
2226

src/components/pin/pin.resolver.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Args, Mutation, Query, Resolver } from '@nestjs/graphql';
22
import {
3+
AnonSession,
34
type ID,
45
IdArg,
56
ListArg,
@@ -19,12 +20,16 @@ export class PinResolver {
1920
'Returns whether or not the requesting user has pinned the resource ID',
2021
})
2122
async isPinned(
22-
@LoggedInSession() session: Session,
23+
@AnonSession() session: Session,
2324
@IdArg({
2425
description: 'A resource ID',
2526
})
2627
id: ID,
2728
): Promise<boolean> {
29+
// TODO move to DB layer?
30+
if (session.anonymous) {
31+
return false;
32+
}
2833
return await this.pins.isPinned(id, session);
2934
}
3035

src/components/project/financial-approver/financial-approver.resolver.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
ResolveField,
77
Resolver,
88
} from '@nestjs/graphql';
9-
import { LoggedInSession, type Session } from '~/common';
9+
import { AnonSession, LoggedInSession, type Session } from '~/common';
1010
import { Loader, type LoaderOf } from '~/core';
1111
import { Privileges } from '../../authorization';
1212
import { UserLoader } from '../../user';
@@ -30,8 +30,12 @@ export class FinancialApproverResolver {
3030
nullable: true,
3131
})
3232
types: readonly ProjectType[] | undefined,
33-
@LoggedInSession() _: Session, // require login
33+
@AnonSession() session: Session,
3434
): Promise<readonly FinancialApprover[]> {
35+
// TODO move to auth policy
36+
if (session.anonymous) {
37+
return [];
38+
}
3539
return await this.repo.read(types);
3640
}
3741

src/components/user/user.resolver.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,13 @@ export class UserResolver {
136136
nullable: true,
137137
})
138138
async userByEmail(
139-
@LoggedInSession() session: Session,
139+
@AnonSession() session: Session,
140140
@Args() { email }: CheckEmailArgs,
141141
): Promise<User | null> {
142+
// TODO move to auth policy?
143+
if (session.anonymous) {
144+
return null;
145+
}
142146
return await this.userService.getUserByEmailAddress(email, session);
143147
}
144148

0 commit comments

Comments
 (0)