|
1 | 1 | import { Prisma, User } from './generated/prisma';
|
2 |
| -import { |
3 |
| - GraphqlAuthenticationAdapter, |
4 |
| - ID, |
5 |
| - Context as _Context |
6 |
| -} from 'graphql-authentication'; |
7 |
| - |
8 |
| -interface Context extends _Context { |
9 |
| - db?: Prisma; |
10 |
| -} |
| 2 | +import { GraphqlAuthenticationAdapter, ID } from 'graphql-authentication'; |
11 | 3 |
|
12 | 4 | export class GraphqlAuthenticationPrismaAdapter
|
13 | 5 | implements GraphqlAuthenticationAdapter {
|
14 |
| - private db(ctx: Context) { |
15 |
| - if (!ctx.db) { |
| 6 | + prismaContextName = 'db'; |
| 7 | + |
| 8 | + constructor(options: { prismaContextName?: string }) { |
| 9 | + if (options.prismaContextName) { |
| 10 | + this.prismaContextName = options.prismaContextName; |
| 11 | + } |
| 12 | + } |
| 13 | + |
| 14 | + private db(ctx: object) { |
| 15 | + const db: Prisma = ctx[this.prismaContextName]; |
| 16 | + if (!db) { |
16 | 17 | throw new Error(
|
17 |
| - 'The Prisma binding is not attached to the `db` property on your context.' |
| 18 | + `The Prisma binding is not attached to the \`${ |
| 19 | + this.prismaContextName |
| 20 | + }\` property on your context.` |
18 | 21 | );
|
19 | 22 | }
|
20 |
| - return ctx.db; |
| 23 | + return db; |
21 | 24 | }
|
22 | 25 |
|
23 |
| - findUserById(ctx: Context, id: ID, info?: any) { |
| 26 | + findUserById(ctx: object, id: ID, info?: any) { |
24 | 27 | return this.db(ctx).query.user({ where: { id } }, info);
|
25 | 28 | }
|
26 |
| - findUserByEmail(ctx: Context, email: string, info?: any) { |
| 29 | + findUserByEmail(ctx: object, email: string, info?: any) { |
27 | 30 | return this.db(ctx).query.user(
|
28 | 31 | {
|
29 | 32 | where: { email: email }
|
30 | 33 | },
|
31 | 34 | info
|
32 | 35 | );
|
33 | 36 | }
|
34 |
| - userExistsByEmail(ctx: Context, email: string) { |
| 37 | + userExistsByEmail(ctx: object, email: string) { |
35 | 38 | return this.db(ctx).exists.User({ email });
|
36 | 39 | }
|
37 |
| - private createUser(ctx: Context, data: any) { |
| 40 | + private createUser(ctx: object, data: any) { |
38 | 41 | return this.db(ctx).mutation.createUser({
|
39 | 42 | data
|
40 | 43 | });
|
41 | 44 | }
|
42 |
| - createUserBySignup(ctx: Context, data: any) { |
| 45 | + createUserBySignup(ctx: object, data: any) { |
43 | 46 | return this.createUser(ctx, data);
|
44 | 47 | }
|
45 |
| - createUserByInvite(ctx: Context, data: any) { |
| 48 | + createUserByInvite(ctx: object, data: any) { |
46 | 49 | return this.createUser(ctx, data);
|
47 | 50 | }
|
48 |
| - private updateUser(ctx: Context, userId: ID, data: any) { |
| 51 | + private updateUser(ctx: object, userId: ID, data: any) { |
49 | 52 | return this.db(ctx).mutation.updateUser({
|
50 | 53 | where: { id: userId },
|
51 | 54 | data
|
52 | 55 | });
|
53 | 56 | }
|
54 |
| - updateUserConfirmToken(ctx: Context, userId: ID, data: any) { |
| 57 | + updateUserConfirmToken(ctx: object, userId: ID, data: any) { |
55 | 58 | return this.updateUser(ctx, userId, data);
|
56 | 59 | }
|
57 |
| - updateUserLastLogin(ctx: Context, userId: ID, data: any) { |
| 60 | + updateUserLastLogin(ctx: object, userId: ID, data: any) { |
58 | 61 | return this.updateUser(ctx, userId, data);
|
59 | 62 | }
|
60 |
| - updateUserPassword(ctx: Context, userId: ID, data: any) { |
| 63 | + updateUserPassword(ctx: object, userId: ID, data: any) { |
61 | 64 | return this.updateUser(ctx, userId, data);
|
62 | 65 | }
|
63 |
| - updateUserResetToken(ctx: Context, userId: ID, data: any) { |
| 66 | + updateUserResetToken(ctx: object, userId: ID, data: any) { |
64 | 67 | return this.updateUser(ctx, userId, data);
|
65 | 68 | }
|
66 |
| - updateUserInfo(ctx: Context, userId: ID, data: any) { |
| 69 | + updateUserInfo(ctx: object, userId: ID, data: any) { |
67 | 70 | return this.updateUser(ctx, userId, data);
|
68 | 71 | }
|
69 |
| - updateUserCompleteInvite(ctx: Context, userId: ID, data: any) { |
| 72 | + updateUserCompleteInvite(ctx: object, userId: ID, data: any) { |
70 | 73 | return this.updateUser(ctx, userId, data);
|
71 | 74 | }
|
72 | 75 | }
|
0 commit comments