-
Notifications
You must be signed in to change notification settings - Fork 12
/
index.ts
36 lines (32 loc) · 877 Bytes
/
index.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
36
import { rule, shield, allow } from 'graphql-shield';
import { getUserId } from '../utils';
import { User } from '../generated/prisma-client';
import { Ctx } from '../types';
const rules = {
isGuest: rule()(async (): Promise<boolean> => true),
isUser: rule()(
async (parent, args, context: Ctx): Promise<boolean> => {
const userId = await getUserId(context);
return !!userId;
}
),
isAdmin: rule({ cache: 'strict' })(
async (parent, args, context: Ctx): Promise<boolean> => {
const userId = await getUserId(context);
const user: User = await context.prisma.user({ id: userId });
return user.role === 'ADMIN';
}
),
};
export const permissions = shield({
Query: {
// Global
'*': rules.isAdmin,
profile: rules.isUser,
},
Mutation: {
'*': rules.isAdmin,
login: allow,
signup: allow,
},
});