feat: Implement pagination logic and use with getExecutions query
This commit is contained in:

committed by
Ömer Faruk Aydın

parent
906a8d0644
commit
44623cc384
@@ -1,16 +1,22 @@
|
|||||||
import Context from '../../types/express/context';
|
import Context from '../../types/express/context';
|
||||||
|
import paginate from '../../helpers/pagination';
|
||||||
|
|
||||||
|
type Params = {
|
||||||
|
limit: number;
|
||||||
|
offset: number;
|
||||||
|
};
|
||||||
|
|
||||||
const getExecutions = async (
|
const getExecutions = async (
|
||||||
_parent: unknown,
|
_parent: unknown,
|
||||||
_params: unknown,
|
params: Params,
|
||||||
context: Context
|
context: Context
|
||||||
) => {
|
) => {
|
||||||
const executions = await context.currentUser
|
const executions = context.currentUser
|
||||||
.$relatedQuery('executions')
|
.$relatedQuery('executions')
|
||||||
.withGraphFetched('flow')
|
.withGraphFetched('flow')
|
||||||
.orderBy('created_at', 'asc');
|
.orderBy('created_at', 'desc');
|
||||||
|
|
||||||
return executions;
|
return paginate(executions, params.limit, params.offset);
|
||||||
};
|
};
|
||||||
|
|
||||||
export default getExecutions;
|
export default getExecutions;
|
||||||
|
@@ -7,7 +7,7 @@ type Query {
|
|||||||
getFlow(id: String!): Flow
|
getFlow(id: String!): Flow
|
||||||
getFlows: [Flow]
|
getFlows: [Flow]
|
||||||
getStepWithTestExecutions(stepId: String!): [Step]
|
getStepWithTestExecutions(stepId: String!): [Step]
|
||||||
getExecutions: [Execution]
|
getExecutions(limit: Int!, offset: Int!): ExecutionConnection
|
||||||
}
|
}
|
||||||
|
|
||||||
type Mutation {
|
type Mutation {
|
||||||
@@ -354,6 +354,20 @@ type User {
|
|||||||
updatedAt: String
|
updatedAt: String
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type PageInfo {
|
||||||
|
currentPage: Int!
|
||||||
|
totalPages: Int!
|
||||||
|
}
|
||||||
|
|
||||||
|
type ExecutionEdge {
|
||||||
|
node: Execution
|
||||||
|
}
|
||||||
|
|
||||||
|
type ExecutionConnection {
|
||||||
|
edges: [ExecutionEdge]
|
||||||
|
pageInfo: PageInfo
|
||||||
|
}
|
||||||
|
|
||||||
schema {
|
schema {
|
||||||
query: Query
|
query: Query
|
||||||
mutation: Mutation
|
mutation: Mutation
|
||||||
|
30
packages/backend/src/helpers/pagination.ts
Normal file
30
packages/backend/src/helpers/pagination.ts
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
import { QueryBuilder, Model } from 'objection';
|
||||||
|
|
||||||
|
const paginate = async (
|
||||||
|
query: QueryBuilder<Model, Model[]>,
|
||||||
|
limit: number,
|
||||||
|
offset: number
|
||||||
|
) => {
|
||||||
|
if (limit < 1 || limit > 100) {
|
||||||
|
throw new Error('Limit must be between 1 and 100');
|
||||||
|
}
|
||||||
|
|
||||||
|
const [records, count] = await Promise.all([
|
||||||
|
query.limit(limit).offset(offset),
|
||||||
|
query.resultSize(),
|
||||||
|
]);
|
||||||
|
|
||||||
|
return {
|
||||||
|
pageInfo: {
|
||||||
|
currentPage: Math.ceil(offset / limit + 1),
|
||||||
|
totalPages: Math.ceil(count / limit),
|
||||||
|
},
|
||||||
|
edges: records.map((record: Model) => {
|
||||||
|
return {
|
||||||
|
node: record,
|
||||||
|
};
|
||||||
|
}),
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
export default paginate;
|
Reference in New Issue
Block a user