refactor: Adjust flow query to use joinRelated

This commit is contained in:
Faruk AYDIN
2022-08-06 15:03:43 +03:00
parent 5a177b330a
commit dc4899c240

View File

@@ -1,5 +1,6 @@
import Context from '../../types/express/context'; import Context from '../../types/express/context';
import paginate from '../../helpers/pagination'; import paginate from '../../helpers/pagination';
import Flow from '../../models/flow';
type Params = { type Params = {
appKey?: string; appKey?: string;
@@ -8,22 +9,16 @@ type Params = {
}; };
const getFlows = async (_parent: unknown, params: Params, context: Context) => { const getFlows = async (_parent: unknown, params: Params, context: Context) => {
const userStepsQuery = context.currentUser const flowsQuery = Flow.query()
.$relatedQuery('steps') .joinRelated('steps')
.select('flow_id') .withGraphFetched('steps.[connection]')
.distinctOn('flow_id'); .where('flows.user_id', context.currentUser.id)
.andWhere((builder) => {
if (params.appKey) { if (params.appKey) {
userStepsQuery.where('app_key', params.appKey); builder.where('steps.app_key', params.appKey);
} }
})
const flowsQuery = context.currentUser .orderBy('updated_at', 'desc');
.$relatedQuery('flows')
.withGraphFetched('[steps.[connection]]')
.whereIn(
'flows.id',
userStepsQuery
);
return paginate(flowsQuery, params.limit, params.offset); return paginate(flowsQuery, params.limit, params.offset);
}; };