Skip to content

Commit 05de326

Browse files
committed
Standardize field resolvers to return empty results when anon
This is probably better as it is not the best to throw errors when resolving fields.
1 parent dbcf2a3 commit 05de326

File tree

6 files changed

+40
-19
lines changed

6 files changed

+40
-19
lines changed

src/components/changeset/changeset-aware.resolver.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { Info, Parent, ResolveField, Resolver } from '@nestjs/graphql';
22
import { stripIndent } from 'common-tags';
33
import {
4+
AnonSession,
45
Fields,
56
IsOnlyId,
6-
LoggedInSession,
77
Resource,
88
type Session,
99
} from '~/common';
@@ -55,8 +55,13 @@ export class ChangesetAwareResolver {
5555
})
5656
async changesetDiff(
5757
@Parent() object: ChangesetAware,
58-
@LoggedInSession() session: Session,
58+
@AnonSession() session: Session,
5959
): Promise<ChangesetDiff | null> {
60+
// TODO move to auth policy
61+
if (session.anonymous) {
62+
return null;
63+
}
64+
6065
const changeset = await this.changeset(object);
6166
if (!changeset) {
6267
return null;

src/components/changeset/changeset.resolver.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { Parent, Query, ResolveField, Resolver } from '@nestjs/graphql';
22
import {
3+
AnonSession,
34
type ID,
45
IdArg,
5-
LoggedInSession,
66
type ObjectView,
77
type Session,
88
} from '~/common';
@@ -28,7 +28,7 @@ export class ChangesetResolver {
2828
})
2929
async difference(
3030
@Parent() changeset: Changeset,
31-
@LoggedInSession() session: Session,
31+
@AnonSession() session: Session,
3232
@IdArg({
3333
name: 'resource',
3434
nullable: true,
@@ -37,6 +37,11 @@ export class ChangesetResolver {
3737
})
3838
parent?: ID,
3939
): Promise<ChangesetDiff> {
40+
// TODO move to auth policy
41+
if (session.anonymous) {
42+
return { added: [], removed: [], changed: [] };
43+
}
44+
4045
const diff = await this.repo.difference(changeset.id, parent);
4146
const load = (node: BaseNode, view?: ObjectView) =>
4247
this.resources.loadByBaseNode(node, view ?? { changeset: changeset.id });

src/components/comments/comment-thread.resolver.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
11
import { Args, Parent, Query, ResolveField, Resolver } from '@nestjs/graphql';
22
import { stripIndent } from 'common-tags';
3-
import {
4-
AnonSession,
5-
type ID,
6-
IdArg,
7-
ListArg,
8-
LoggedInSession,
9-
type Session,
10-
} from '~/common';
3+
import { AnonSession, type ID, IdArg, ListArg, type Session } from '~/common';
4+
import { loggedInSession as verifyLoggedIn } from '~/common/session';
115
import { Loader, type LoaderOf, ResourceLoader } from '~/core';
126
import { UserLoader } from '../user';
137
import { User } from '../user/dto';
@@ -47,9 +41,11 @@ export class CommentThreadResolver {
4741
async commentThreads(
4842
@IdArg({ name: 'resource' }) resourceId: ID,
4943
@ListArg(CommentThreadListInput) input: CommentThreadListInput,
50-
@LoggedInSession() session: Session,
44+
@AnonSession() session: Session,
5145
@Loader(CommentThreadLoader) commentThreads: LoaderOf<CommentThreadLoader>,
5246
): Promise<CommentThreadList> {
47+
// TODO move to auth policy
48+
verifyLoggedIn(session);
5349
const resource = await this.service.loadCommentable(resourceId);
5450
const list = await this.service.listThreads(resource, input, session);
5551
commentThreads.primeAll(list.items);

src/components/comments/commentable.resolver.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import { Info, Parent, Query, ResolveField, Resolver } from '@nestjs/graphql';
22
import {
3+
AnonSession,
34
Fields,
45
type ID,
56
IdArg,
67
IsOnly,
78
ListArg,
8-
LoggedInSession,
99
type Resource,
10+
SecuredList,
1011
type Session,
1112
} from '~/common';
1213
import { Loader, type LoaderOf } from '~/core';
@@ -31,10 +32,14 @@ export class CommentableResolver {
3132
async commentThreads(
3233
@Parent() parent: Commentable & Resource,
3334
@ListArg(CommentThreadListInput) input: CommentThreadListInput,
34-
@LoggedInSession() session: Session,
35+
@AnonSession() session: Session,
3536
@Loader(CommentThreadLoader) commentThreads: LoaderOf<CommentThreadLoader>,
3637
@Info(Fields, IsOnly(['total'])) onlyTotal: boolean,
3738
) {
39+
// TODO move to auth policy
40+
if (session.anonymous) {
41+
return { parent, ...SecuredList.Redacted };
42+
}
3843
if (onlyTotal) {
3944
const total = await this.service.getThreadCount(parent, session);
4045
return { total };

src/components/post/postable.resolver.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { Info, Parent, ResolveField, Resolver } from '@nestjs/graphql';
22
import { type GraphQLResolveInfo } from 'graphql';
33
import {
4+
AnonSession,
45
ListArg,
5-
LoggedInSession,
66
type Resource,
7+
SecuredList,
78
type Session,
89
} from '~/common';
910
import { Loader, type LoaderOf } from '~/core';
@@ -23,9 +24,14 @@ export class PostableResolver {
2324
@Info() info: GraphQLResolveInfo & {},
2425
@Parent() parent: Postable & Resource,
2526
@ListArg(PostListInput) input: PostListInput,
26-
@LoggedInSession() session: Session,
27+
@AnonSession() session: Session,
2728
@Loader(PostLoader) posts: LoaderOf<PostLoader>,
2829
): Promise<SecuredPostList> {
30+
// TODO move to auth policy
31+
if (session.anonymous) {
32+
return SecuredList.Redacted;
33+
}
34+
2935
const list = await this.service.securedList(
3036
{
3137
...parent,

src/components/product-progress/create-product-connection.resolver.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Parent, ResolveField, Resolver } from '@nestjs/graphql';
2-
import { LoggedInSession, type Session, Variant } from '~/common';
2+
import { AnonSession, type Session, Variant } from '~/common';
33
import { CreateProductOutput } from '../product/dto';
44
import { ProductProgressService } from './product-progress.service';
55

@@ -12,8 +12,12 @@ export class ProgressReportCreateProductConnectionResolver {
1212
})
1313
async availableVariants(
1414
@Parent() { product }: CreateProductOutput,
15-
@LoggedInSession() session: Session,
15+
@AnonSession() session: Session,
1616
): Promise<readonly Variant[]> {
17+
// TODO move to auth policy
18+
if (session.anonymous) {
19+
return [];
20+
}
1721
return await this.service.getAvailableVariantsForProduct(product, session);
1822
}
1923
}

0 commit comments

Comments
 (0)