Skip to content

Commit

Permalink
Added Pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
piyushgarg-dev committed Oct 18, 2023
1 parent a3fdf69 commit d788ff7
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 2 deletions.
2 changes: 2 additions & 0 deletions functions/graphql/form/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ export interface GetFormResponsesByFormIdInput {

export interface GetFormResponsesByProjectId {
projectId: string
itemsPerPage?: number
cursor?: string
}

export interface GetPublicFormDataInput {
Expand Down
6 changes: 5 additions & 1 deletion functions/graphql/form/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions functions/graphql/form/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,5 +171,7 @@ export const types = `#graphql
input GetFormResponsesByProjectIdInput {
projectId: ID!
itemsPerPage: Int
cursor: String
}
`
17 changes: 16 additions & 1 deletion services/form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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: {
Expand All @@ -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' },
})
}

Expand Down

0 comments on commit d788ff7

Please sign in to comment.