Add Pothos-generated User GraphQL schema and resolver

This commit adds the Pothos-generated User GraphQL schema and resolver. The schema is defined in the `src/graphql/graphql.schema.ts` file, and the resolver is defined in the `src/graphql/graphql.resolver.ts` file. The schema includes the `User` object type with fields for `id`, `name`, and `email`. The resolver includes a query field `users` that resolves to fetch all users from the Prisma database.
This commit is contained in:
2024-09-24 16:52:09 +07:00
parent 118e33884c
commit 4747fca4a4
12 changed files with 458 additions and 400 deletions

30
src/types/pothos.type.ts Normal file
View File

@@ -0,0 +1,30 @@
import { PrismaClient } from '@prisma/client';
import SchemaBuilder from '@pothos/core';
import PrismaPlugin from '@pothos/plugin-prisma';
const prisma = new PrismaClient();
export const builder = new SchemaBuilder({
plugins: [PrismaPlugin],
prisma: {
client: prisma,
},
});
// Define a basic query type
builder.queryType({
fields: (t) => ({
hello: t.string({
resolve: () => 'Hello, world!',
}),
}),
});
// Define a basic mutation type
builder.mutationType({
fields: (t) => ({
exampleMutation: t.boolean({
resolve: () => true,
}),
}),
});

View File

@@ -1,51 +1,30 @@
import { ObjectType, Field, Int } from '@nestjs/graphql';
import {
User as PrismaUser,
Role,
Order,
UploadedDocument,
Chat,
Feedback,
} from '@prisma/client';
import { builder } from '../types/pothos.type';
import { PrismaService } from '../prisma/prisma.service';
import { Module } from '@nestjs/common';
import { PrismaModule } from '../prisma/prisma.module';
@ObjectType()
export class User implements PrismaUser {
@Field(() => Int)
id: string;
@Module({
imports: [PrismaModule],
})
export class UserSchema {
constructor(private prisma: PrismaService) {
// Define the User GraphQL object type based on Prisma schema
builder.prismaObject('User' as never, {
fields: (t) => ({
id: t.exposeInt('id' as never),
name: t.exposeString('name' as never),
email: t.exposeString('email' as never),
}),
});
@Field()
name: string;
@Field()
email: string;
@Field()
password: string;
@Field()
phoneNumber: string;
@Field()
oauthToken: string | null;
@Field()
role: Role;
@Field()
createdAt: Date;
@Field()
updatedAt: Date;
@Field()
orders: Order[];
@Field()
feedbacks: Feedback[];
@Field()
chats: Chat[];
@Field()
documents: UploadedDocument[];
// Define the "users" query that resolves using Prisma
builder.queryField('users', (t) =>
t.prismaField({
type: ['User' as never],
resolve: async () => {
return this.prisma.user.findMany() as any;
},
}),
);
}
}