Let's say we have a bookshop and an author entity, to show the author their earnings stat, we want to check if the authenticated user is indeed the author themselves. So we have:
@UseGuards(GqlAuthGuard)
@ResolveField(() => [Eearning], { name: 'earnings' })
async getEarnings(
@Parent() author: Author,
@GqlUser() user: User,
) {
if (user.id !== author.id)
throw new UnauthorizedException(
'Each author can only view their own data',
);
// rest of the function implementation
}
We could query this:
query {
author(id: "2bd79-6d7f-76a332b06b") {
earnings {
sells
}
}
}
Now imagine we want to use a custom Guard instead of that if statement. Something like below:
@Injectable()
export class AutherGuard implements CanActivate {
canActivate(context: ExecutionContext): boolean {
const ctx = GqlExecutionContext.create(context);
// const artistId = ?
}
}
How can I access the id argument given to the author query when AutherGuard is used for the getEarnings handler?
author()receives anidargument for public info that are accessible to all users. (likeauthor.books). If I use the current author's id and they have entered anidother than theirid, I still want to show a proper error instead of just passively returning their data (rather than the data of the id they have entered)