From 5a177b330a175521bbef2aaf86c602e3ac725674 Mon Sep 17 00:00:00 2001 From: Ali BARIN Date: Fri, 5 Aug 2022 13:40:03 +0200 Subject: [PATCH 1/2] feat: add paging capability in getFlows query --- .../backend/src/graphql/queries/get-flows.ts | 23 +++++++++++++------ packages/backend/src/graphql/schema.graphql | 11 ++++++++- 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/packages/backend/src/graphql/queries/get-flows.ts b/packages/backend/src/graphql/queries/get-flows.ts index 10e3c68a..5f849393 100644 --- a/packages/backend/src/graphql/queries/get-flows.ts +++ b/packages/backend/src/graphql/queries/get-flows.ts @@ -1,22 +1,31 @@ import Context from '../../types/express/context'; +import paginate from '../../helpers/pagination'; type Params = { appKey?: string; + limit: number; + offset: number; }; const getFlows = async (_parent: unknown, params: Params, context: Context) => { - const flowsQuery = context.currentUser - .$relatedQuery('flows') - .withGraphJoined('[steps.[connection]]') - .orderBy('created_at', 'desc'); + const userStepsQuery = context.currentUser + .$relatedQuery('steps') + .select('flow_id') + .distinctOn('flow_id'); if (params.appKey) { - flowsQuery.where('steps.app_key', params.appKey); + userStepsQuery.where('app_key', params.appKey); } - const flows = await flowsQuery; + const flowsQuery = context.currentUser + .$relatedQuery('flows') + .withGraphFetched('[steps.[connection]]') + .whereIn( + 'flows.id', + userStepsQuery + ); - return flows; + return paginate(flowsQuery, params.limit, params.offset); }; export default getFlows; diff --git a/packages/backend/src/graphql/schema.graphql b/packages/backend/src/graphql/schema.graphql index 3d5eeffa..8762c962 100644 --- a/packages/backend/src/graphql/schema.graphql +++ b/packages/backend/src/graphql/schema.graphql @@ -4,7 +4,7 @@ type Query { getConnectedApps(name: String): [App] testConnection(id: String!): Connection getFlow(id: String!): Flow - getFlows(appKey: String): [Flow] + getFlows(limit: Int!, offset: Int!, appKey: String): FlowConnection getStepWithTestExecutions(stepId: String!): [Step] getExecutions(limit: Int!, offset: Int!): ExecutionConnection getExecutionSteps( @@ -189,6 +189,15 @@ type Field { clickToCopy: Boolean } +type FlowConnection { + edges: [FlowEdge] + pageInfo: PageInfo +} + +type FlowEdge { + node: Flow +} + type Flow { id: String name: String From dc4899c240aa4554eb0c7e49cfaa902ec5cf96d4 Mon Sep 17 00:00:00 2001 From: Faruk AYDIN Date: Sat, 6 Aug 2022 15:03:43 +0300 Subject: [PATCH 2/2] refactor: Adjust flow query to use joinRelated --- .../backend/src/graphql/queries/get-flows.ts | 27 ++++++++----------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/packages/backend/src/graphql/queries/get-flows.ts b/packages/backend/src/graphql/queries/get-flows.ts index 5f849393..db6e03ac 100644 --- a/packages/backend/src/graphql/queries/get-flows.ts +++ b/packages/backend/src/graphql/queries/get-flows.ts @@ -1,5 +1,6 @@ import Context from '../../types/express/context'; import paginate from '../../helpers/pagination'; +import Flow from '../../models/flow'; type Params = { appKey?: string; @@ -8,22 +9,16 @@ type Params = { }; const getFlows = async (_parent: unknown, params: Params, context: Context) => { - const userStepsQuery = context.currentUser - .$relatedQuery('steps') - .select('flow_id') - .distinctOn('flow_id'); - - if (params.appKey) { - userStepsQuery.where('app_key', params.appKey); - } - - const flowsQuery = context.currentUser - .$relatedQuery('flows') - .withGraphFetched('[steps.[connection]]') - .whereIn( - 'flows.id', - userStepsQuery - ); + const flowsQuery = Flow.query() + .joinRelated('steps') + .withGraphFetched('steps.[connection]') + .where('flows.user_id', context.currentUser.id) + .andWhere((builder) => { + if (params.appKey) { + builder.where('steps.app_key', params.appKey); + } + }) + .orderBy('updated_at', 'desc'); return paginate(flowsQuery, params.limit, params.offset); };