Add SortOrder enum and enhance Service query with sorting and filtering options

This commit is contained in:
2024-10-06 12:09:58 +07:00
parent aa5817fb06
commit 121396aa8f
5 changed files with 50 additions and 3 deletions

View File

@@ -29,4 +29,9 @@ export const builder = new SchemaBuilder<{
}, },
}); });
export const SortOrder = builder.enumType('SortOrder', {
values: ['asc', 'desc'],
});
builder.addScalarType('DateTime', DateTimeResolver, {}); builder.addScalarType('DateTime', DateTimeResolver, {});

View File

@@ -1,4 +1,4 @@
import { builder, prisma } from './graphql.builder'; import { builder, prisma, SortOrder } from './graphql.builder';
builder.prismaObject('User', { builder.prismaObject('User', {
fields: (t) => ({ fields: (t) => ({
@@ -224,6 +224,42 @@ builder.queryType({
}), }),
services: t.prismaField({ services: t.prismaField({
type: ['Service'], 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) => { resolve: (query, root, args, ctx, info) => {
return prisma.service.findMany({ return prisma.service.findMany({
...query, ...query,

View File

@@ -3,7 +3,13 @@ import { AppModule } from './app.module';
async function bootstrap() { async function bootstrap() {
const app = await NestFactory.create(AppModule); 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); await app.listen(3000);
} }
bootstrap(); bootstrap();

View File