From 8de46af300e672a4778902226c7f76a925e84970 Mon Sep 17 00:00:00 2001 From: Ly Tuan Kiet Date: Sun, 6 Oct 2024 13:41:56 +0700 Subject: [PATCH] Refactor GraphQL schema to enhance Service query with additional filtering and sorting options, and add a new Restful module --- src/app.module.ts | 2 -- src/graphql/schema.ts | 38 ++++++++++++++++++++++++++++++---- src/restful/restful.module.ts | 5 +++++ src/restful/restful.service.ts | 0 4 files changed, 39 insertions(+), 6 deletions(-) create mode 100644 src/restful/restful.module.ts create mode 100644 src/restful/restful.service.ts diff --git a/src/app.module.ts b/src/app.module.ts index 2255ffc..58f355a 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -1,7 +1,5 @@ import { Module } from '@nestjs/common'; import { GraphqlModule } from './graphql/graphql.module'; -import { PrismaModule } from './prisma/prisma.module'; -import { UsersModule } from './users/users.module'; import { ClerkModule } from './clerk/clerk.module'; @Module({ diff --git a/src/graphql/schema.ts b/src/graphql/schema.ts index 3bdbd18..664b046 100644 --- a/src/graphql/schema.ts +++ b/src/graphql/schema.ts @@ -1,4 +1,5 @@ import { builder, prisma, SortOrder } from './graphql.builder'; +import { Prisma } from '@prisma/client'; builder.prismaObject('User', { fields: (t) => ({ @@ -229,7 +230,7 @@ builder.queryType({ type: builder.inputType('ServiceWhereInput', { fields: (t) => ({ // search by name contains - name: t.string(), + nameContain: t.string(), // search by name starts with nameStartsWith: t.string(), // search by name ends with @@ -262,9 +263,26 @@ builder.queryType({ }, resolve: (query, root, args, ctx, info) => { return prisma.service.findMany({ + // process filters + where: { + name: { + contains: args.where?.nameContain as string, + startsWith: args.where?.nameStartsWith as string, + endsWith: args.where?.nameEndsWith as string, + }, + }, + // process order by + orderBy: { + rating: args.orderBy?.rating as Prisma.SortOrder, + price: args.orderBy?.price as Prisma.SortOrder, + }, + // process pagination + cursor: args.cursor as Prisma.ServiceWhereUniqueInput, + take: args.take as number, + skip: args.skip as number, ...query, }); - }, + }, }), service: t.prismaField({ type: 'Service', @@ -329,12 +347,24 @@ builder.queryType({ // type: 'User', // args: { // data: t.arg({ -// type: 'PrismaTypes.UserCreateInput', +// type: builder.inputType('UserCreateInput', { +// fields: (t) => ({ +// email: t.string({ required: true }), +// name: t.string(), +// // Include other fields as per your schema +// }), +// }), +// required: true, // Make the data argument required // }), // }, // resolve: (query, root, args, ctx, info) => { +// if (!args.data) { +// throw new Error('Data input is required'); +// } + // return prisma.user.create({ -// data: args.data, +// ...query, +// data: args.data as Prisma.UserCreateInput, // Explicit type casting to match Prisma's expectation // }); // }, // }), diff --git a/src/restful/restful.module.ts b/src/restful/restful.module.ts new file mode 100644 index 0000000..6d20dc8 --- /dev/null +++ b/src/restful/restful.module.ts @@ -0,0 +1,5 @@ +import { Module } from '@nestjs/common'; +@Module({ + imports: [], +}) +export class RestfulModule {} diff --git a/src/restful/restful.service.ts b/src/restful/restful.service.ts new file mode 100644 index 0000000..e69de29