From d788ff731f1d02372a1a79e3ffb27df456cafc6e Mon Sep 17 00:00:00 2001 From: Piyush Garg Date: Wed, 18 Oct 2023 23:26:47 +0530 Subject: [PATCH] Added Pagination --- functions/graphql/form/interfaces.ts | 2 ++ functions/graphql/form/resolver.ts | 6 +++++- functions/graphql/form/types.ts | 2 ++ services/form.ts | 17 ++++++++++++++++- 4 files changed, 25 insertions(+), 2 deletions(-) diff --git a/functions/graphql/form/interfaces.ts b/functions/graphql/form/interfaces.ts index 1005049..cc46f84 100644 --- a/functions/graphql/form/interfaces.ts +++ b/functions/graphql/form/interfaces.ts @@ -64,6 +64,8 @@ export interface GetFormResponsesByFormIdInput { export interface GetFormResponsesByProjectId { projectId: string + itemsPerPage?: number + cursor?: string } export interface GetPublicFormDataInput { diff --git a/functions/graphql/form/resolver.ts b/functions/graphql/form/resolver.ts index b5a0714..0355ab7 100644 --- a/functions/graphql/form/resolver.ts +++ b/functions/graphql/form/resolver.ts @@ -41,7 +41,11 @@ const queries = { ctx: ServerContext ) => { ensureAuthenticated(ctx) - return FormService.getFormResponsesByProjectId(input.projectId, ctx) + const { projectId, itemsPerPage = 10, cursor } = input + return FormService.getFormResponsesByProjectId(projectId, ctx, { + cursor, + itemsPerPage, + }) }, getPublicFormData: async ( _: any, diff --git a/functions/graphql/form/types.ts b/functions/graphql/form/types.ts index e712c98..5119d3e 100644 --- a/functions/graphql/form/types.ts +++ b/functions/graphql/form/types.ts @@ -171,5 +171,7 @@ export const types = `#graphql input GetFormResponsesByProjectIdInput { projectId: ID! + itemsPerPage: Int + cursor: String } ` diff --git a/services/form.ts b/services/form.ts index 7cdfafe..91f863f 100644 --- a/services/form.ts +++ b/services/form.ts @@ -3,6 +3,11 @@ import AccessDeniedError from '../errors/AccessDeniedError' import { UpdateFormData } from '../functions/graphql/form/interfaces' import { ServerContext } from '../functions/graphql/interfaces' +interface GetFormResponsesByProjectIdOptions { + itemsPerPage?: number + cursor?: string +} + class FormService { public static createForm = prismaClient.form.create @@ -76,9 +81,11 @@ class FormService { public static getFormResponsesByProjectId( projectId: string, - ctx: ServerContext + ctx: ServerContext, + options?: GetFormResponsesByProjectIdOptions ) { if (!ctx.user?.id) throw new AccessDeniedError() + return prismaClient.formResponse.findMany({ where: { form: { @@ -88,6 +95,14 @@ class FormService { }, }, }, + cursor: options?.cursor + ? { + id: options?.cursor, + } + : undefined, + take: options?.itemsPerPage ?? 10, + skip: 1, // Skip the cursor + orderBy: { createdAt: 'desc' }, }) }