From 121396aa8ff719f04a4333c4a47c88328666c2db Mon Sep 17 00:00:00 2001 From: Ly Tuan Kiet Date: Sun, 6 Oct 2024 12:09:58 +0700 Subject: [PATCH] Add SortOrder enum and enhance Service query with sorting and filtering options --- src/graphql/graphql.builder.ts | 5 ++++ src/graphql/graphql.module.ts | 2 +- src/graphql/schema.ts | 38 +++++++++++++++++++++++++++++- src/main.ts | 8 ++++++- src/middlewares/cors.middleware.ts | 0 5 files changed, 50 insertions(+), 3 deletions(-) create mode 100644 src/middlewares/cors.middleware.ts diff --git a/src/graphql/graphql.builder.ts b/src/graphql/graphql.builder.ts index 1a68d2a..b794fc4 100644 --- a/src/graphql/graphql.builder.ts +++ b/src/graphql/graphql.builder.ts @@ -29,4 +29,9 @@ export const builder = new SchemaBuilder<{ }, }); +export const SortOrder = builder.enumType('SortOrder', { + values: ['asc', 'desc'], +}); + + builder.addScalarType('DateTime', DateTimeResolver, {}); \ No newline at end of file diff --git a/src/graphql/graphql.module.ts b/src/graphql/graphql.module.ts index 5d1f06a..2ba996d 100644 --- a/src/graphql/graphql.module.ts +++ b/src/graphql/graphql.module.ts @@ -22,4 +22,4 @@ export class GraphqlModule { .apply(GraphQLValidationMiddleware) // Apply the custom middleware .forRoutes('graphql'); // Ensure it only applies to the /graphql endpoint } -} +} \ No newline at end of file diff --git a/src/graphql/schema.ts b/src/graphql/schema.ts index b337344..3bdbd18 100644 --- a/src/graphql/schema.ts +++ b/src/graphql/schema.ts @@ -1,4 +1,4 @@ -import { builder, prisma } from './graphql.builder'; +import { builder, prisma, SortOrder } from './graphql.builder'; builder.prismaObject('User', { fields: (t) => ({ @@ -224,6 +224,42 @@ builder.queryType({ }), services: t.prismaField({ type: ['Service'], + args: { + where: t.arg({ + type: builder.inputType('ServiceWhereInput', { + fields: (t) => ({ + // search by name contains + name: t.string(), + // search by name starts with + nameStartsWith: t.string(), + // search by name ends with + nameEndsWith: t.string(), + }), + }), + }), + orderBy: t.arg({ + type: builder.inputType('ServiceOrderByInput', { + fields: (t) => ({ + rating: t.field({ + type: SortOrder, + }), + price: t.field({ + type: SortOrder, + }), + }), + }), + }), + cursor: t.arg({ + type: builder.inputType('ServiceWhereUniqueInput', { + fields: (t) => ({ + // Define fields to match your `ServiceWhereUniqueInput` structure. + name: t.string(), + }), + }), + }), + take: t.arg.int(), + skip: t.arg.int(), + }, resolve: (query, root, args, ctx, info) => { return prisma.service.findMany({ ...query, diff --git a/src/main.ts b/src/main.ts index 430b9ee..c3d8346 100644 --- a/src/main.ts +++ b/src/main.ts @@ -3,7 +3,13 @@ import { AppModule } from './app.module'; async function bootstrap() { const app = await NestFactory.create(AppModule); - + app.enableCors({ + origin: '*', + methods: 'GET,HEAD,PUT,PATCH,POST,DELETE', + preflightContinue: false, + optionsSuccessStatus: 204, + credentials: true, + }); await app.listen(3000); } bootstrap(); diff --git a/src/middlewares/cors.middleware.ts b/src/middlewares/cors.middleware.ts new file mode 100644 index 0000000..e69de29