116 lines
3.4 KiB
TypeScript
116 lines
3.4 KiB
TypeScript
import { Inject, Injectable } from '@nestjs/common';
|
|
import {
|
|
Pothos,
|
|
PothosRef,
|
|
PothosSchema,
|
|
SchemaBuilderToken,
|
|
} from '@smatch-corp/nestjs-pothos';
|
|
import { Builder } from '../Graphql/graphql.builder';
|
|
import { PrismaService } from '../Prisma/prisma.service';
|
|
import { clerkClient } from '@clerk/express';
|
|
@Injectable()
|
|
export class UserSchema extends PothosSchema {
|
|
constructor(
|
|
@Inject(SchemaBuilderToken) private readonly builder: Builder,
|
|
private readonly prisma: PrismaService,
|
|
) {
|
|
super();
|
|
}
|
|
|
|
// Types section
|
|
@PothosRef()
|
|
user() {
|
|
return this.builder.prismaObject('User', {
|
|
description: 'A user in the system.',
|
|
fields: (t) => ({
|
|
id: t.exposeID('id'),
|
|
name: t.exposeString('name'),
|
|
email: t.exposeString('email'),
|
|
phoneNumber: t.exposeString('phoneNumber'),
|
|
oauthToken: t.exposeString('oauthToken', { nullable: true }),
|
|
role: t.exposeString('role'),
|
|
createdAt: t.expose('createdAt', { type: 'DateTime' }),
|
|
updatedAt: t.expose('updatedAt', { type: 'DateTime' }),
|
|
center: t.relation('center'),
|
|
}),
|
|
});
|
|
}
|
|
|
|
// Query section
|
|
@Pothos()
|
|
init(): void {
|
|
this.builder.queryFields((t) => ({
|
|
users: t.prismaField({
|
|
description:
|
|
'Retrieve a list of users with optional filtering, ordering, and pagination.',
|
|
type: [this.user()],
|
|
args: this.builder.generator.findManyArgs('User'),
|
|
resolve: async (query, root, args, ctx, info) => {
|
|
return await this.prisma.user.findMany({
|
|
...query,
|
|
take: args.take ?? 10,
|
|
skip: args.skip ?? 0,
|
|
orderBy: args.orderBy ?? undefined,
|
|
where: args.filter ?? undefined,
|
|
});
|
|
},
|
|
}),
|
|
|
|
user: t.prismaField({
|
|
description: 'Retrieve a single user by their unique identifier.',
|
|
type: this.user(),
|
|
args: this.builder.generator.findUniqueArgs('User'),
|
|
resolve: async (query, root, args, ctx, info) => {
|
|
return await this.prisma.user.findUnique({
|
|
...query,
|
|
where: args.where,
|
|
});
|
|
},
|
|
}),
|
|
userBySession: t.prismaField({
|
|
description: 'Retrieve a single user by their session ID.',
|
|
type: this.user(),
|
|
args: {
|
|
sessionId: t.arg({ type: 'String', required: true }),
|
|
},
|
|
resolve: async (query, root, args, ctx, info) => {
|
|
// check if the token is valid
|
|
const session = await clerkClient.sessions.getSession(args.sessionId);
|
|
console.log(session);
|
|
return await this.prisma.user.findFirst({
|
|
...query,
|
|
where: {
|
|
id: session.userId,
|
|
},
|
|
});
|
|
},
|
|
}),
|
|
}));
|
|
|
|
// Mutation section
|
|
this.builder.mutationFields((t) => ({
|
|
updateUser: t.prismaField({
|
|
description: 'Update an existing user.',
|
|
type: this.user(),
|
|
args: {
|
|
input: t.arg({
|
|
type: this.builder.generator.getUpdateInput('User'),
|
|
required: true,
|
|
}),
|
|
where: t.arg({
|
|
type: this.builder.generator.getWhereUnique('User'),
|
|
required: true,
|
|
}),
|
|
},
|
|
resolve: async (query, root, args, ctx, info) => {
|
|
return await this.prisma.user.update({
|
|
...query,
|
|
where: args.where,
|
|
data: args.input,
|
|
});
|
|
},
|
|
}),
|
|
}));
|
|
}
|
|
}
|