forked from accounts-js/accounts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DarthsResolver.ts
35 lines (31 loc) · 1.3 KB
/
DarthsResolver.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import { User } from '@accounts/types';
import { ExecutionContext, UseGuards, UseInterceptors } from '@nestjs/common';
import { Query, Resolver } from '@nestjs/graphql';
import { AccountsSessionRequest, AuthGuard, AuthValidator } from '../../lib';
import { AccountsErrorInterceptor } from '../../lib/interceptors/AccountsError.interceptor';
import { getGQLContext, GQLParam, isGQLParam } from '../../lib/utils/GraphQLUtils';
const IsDarthVader = (user: User) => user.username === 'darth_vader';
const TalkingToLuke = (_: User, _context: ExecutionContext, params: AccountsSessionRequest | GQLParam) =>
isGQLParam(params) ? !!getGQLContext<any>(params).talkingToLuke : !!params.body.talkingToLuke;
@Resolver()
@UseInterceptors(AccountsErrorInterceptor)
@UseGuards(AuthGuard)
@AuthValidator(IsDarthVader)
export class DarthsResolver {
@Query(_ => String)
darthsSecret() {
return 'I am Anakin Skywalker';
}
@Query(_ => String)
@UseGuards(AuthGuard)
@AuthValidator(TalkingToLuke, () => true)
darthsDeepestSecret() {
return 'Luke, I am your father';
}
@Query(_ => String)
@UseGuards(AuthGuard)
@AuthValidator(() => process.env.NODE_ENV === 'development') // Auth Validator can do anything, even if it's not user related
devOnly() {
return 'secret dev stuff';
}
}