Skip to content

Commit

Permalink
Add resolver to fetch all flows without pages
Browse files Browse the repository at this point in the history
  • Loading branch information
manelcecs committed Nov 28, 2023
1 parent c751f4c commit 2b291fe
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions src/domain-services/flows/graphql/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { SearchFlowsArgs, SearchFlowsArgsNonPaginated } from './args';
import {
FlowPaged,
FlowSearchResult,
FlowSearchResultNonPaginated,
FlowSearchTotalAmountResult,
} from './types';

Expand All @@ -31,4 +32,50 @@ export default class FlowResolver {
): Promise<FlowSearchTotalAmountResult> {
return await this.flowSearchService.searchTotalAmount(context.models, args);
}

@Query(() => FlowSearchResultNonPaginated)
async searchFlowsBatches(
@Ctx() context: Context,
@Args(() => SearchFlowsArgs, { validate: false })
args: SearchFlowsArgs
): Promise<FlowSearchResultNonPaginated> {
// Set default batch size to 1000
args.limit = args.limit > 0 ? args.limit : 1000;

const flowSearchResponse = await this.flowSearchService.search(
context.models,
args
);

const batchesMissing =
Math.round(flowSearchResponse.total / args.limit) - 1;
const flows: FlowPaged[] = flowSearchResponse.flows;

let hasNextPage = flowSearchResponse.hasNextPage;
let batchCount = 1;

let cursor = flowSearchResponse.endCursor;
let nextArgs: SearchFlowsArgs = { ...args, afterCursor: cursor };

let nextFlowSearchResponse: FlowSearchResult;
while (hasNextPage) {
batchCount++;

nextFlowSearchResponse = await this.flowSearchService.search(
context.models,
nextArgs
);

flows.push(...nextFlowSearchResponse.flows);

hasNextPage =
nextFlowSearchResponse.hasNextPage && batchCount <= batchesMissing;

cursor = nextFlowSearchResponse.endCursor;
// Update the cursor for the next iteration
nextArgs = { ...args, afterCursor: cursor };
}

return { flows, flowsCount: flows.length };
}
}

0 comments on commit 2b291fe

Please sign in to comment.