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 3c4836f
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/domain-services/flows/flow-search-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
type FlowPaged,
type FlowParkedParentSource,
type FlowSearchResult,
type FlowSearchResultNonPaginated,
type FlowSearchTotalAmountResult,
type FlowSortField,
} from './graphql/types';
Expand Down Expand Up @@ -532,4 +533,39 @@ export class FlowSearchService {
flowsCount: count,
};
}

async searchBatches(
models: Database,
args: SearchFlowsArgs
): Promise<FlowSearchResultNonPaginated> {
const flowSearchResponse = await this.search(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.search(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 };
}
}
13 changes: 13 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,16 @@ 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;

return await this.flowSearchService.searchBatches(context.models, args);
}
}

0 comments on commit 3c4836f

Please sign in to comment.