Skip to content

Commit 20d0f64

Browse files
committed
allow custom prisma context name
1 parent e263499 commit 20d0f64

File tree

2 files changed

+33
-27
lines changed

2 files changed

+33
-27
lines changed

packages/graphql-authentication-prisma/README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,10 @@ const server = new GraphQLServer({
5050
...req,
5151
db: new Prisma({...}),
5252
graphqlAuthentication: graphqlAuthenticationConfig({
53-
adapter: new GraphqlAuthenticationPrismaAdapter(),
53+
adapter: new GraphqlAuthenticationPrismaAdapter({
54+
// Optional, defaults to 'db'
55+
prismaContextName: 'db'
56+
}),
5457
// Required, used for signing JWT tokens
5558
secret: 'wheredidthesodago',
5659
// Optional, for sending emails with email-templates (https://www.npmjs.com/package/email-templates)
Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,75 @@
11
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';
113

124
export class GraphqlAuthenticationPrismaAdapter
135
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) {
1617
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.`
1821
);
1922
}
20-
return ctx.db;
23+
return db;
2124
}
2225

23-
findUserById(ctx: Context, id: ID, info?: any) {
26+
findUserById(ctx: object, id: ID, info?: any) {
2427
return this.db(ctx).query.user({ where: { id } }, info);
2528
}
26-
findUserByEmail(ctx: Context, email: string, info?: any) {
29+
findUserByEmail(ctx: object, email: string, info?: any) {
2730
return this.db(ctx).query.user(
2831
{
2932
where: { email: email }
3033
},
3134
info
3235
);
3336
}
34-
userExistsByEmail(ctx: Context, email: string) {
37+
userExistsByEmail(ctx: object, email: string) {
3538
return this.db(ctx).exists.User({ email });
3639
}
37-
private createUser(ctx: Context, data: any) {
40+
private createUser(ctx: object, data: any) {
3841
return this.db(ctx).mutation.createUser({
3942
data
4043
});
4144
}
42-
createUserBySignup(ctx: Context, data: any) {
45+
createUserBySignup(ctx: object, data: any) {
4346
return this.createUser(ctx, data);
4447
}
45-
createUserByInvite(ctx: Context, data: any) {
48+
createUserByInvite(ctx: object, data: any) {
4649
return this.createUser(ctx, data);
4750
}
48-
private updateUser(ctx: Context, userId: ID, data: any) {
51+
private updateUser(ctx: object, userId: ID, data: any) {
4952
return this.db(ctx).mutation.updateUser({
5053
where: { id: userId },
5154
data
5255
});
5356
}
54-
updateUserConfirmToken(ctx: Context, userId: ID, data: any) {
57+
updateUserConfirmToken(ctx: object, userId: ID, data: any) {
5558
return this.updateUser(ctx, userId, data);
5659
}
57-
updateUserLastLogin(ctx: Context, userId: ID, data: any) {
60+
updateUserLastLogin(ctx: object, userId: ID, data: any) {
5861
return this.updateUser(ctx, userId, data);
5962
}
60-
updateUserPassword(ctx: Context, userId: ID, data: any) {
63+
updateUserPassword(ctx: object, userId: ID, data: any) {
6164
return this.updateUser(ctx, userId, data);
6265
}
63-
updateUserResetToken(ctx: Context, userId: ID, data: any) {
66+
updateUserResetToken(ctx: object, userId: ID, data: any) {
6467
return this.updateUser(ctx, userId, data);
6568
}
66-
updateUserInfo(ctx: Context, userId: ID, data: any) {
69+
updateUserInfo(ctx: object, userId: ID, data: any) {
6770
return this.updateUser(ctx, userId, data);
6871
}
69-
updateUserCompleteInvite(ctx: Context, userId: ID, data: any) {
72+
updateUserCompleteInvite(ctx: object, userId: ID, data: any) {
7073
return this.updateUser(ctx, userId, data);
7174
}
7275
}

0 commit comments

Comments
 (0)