Skip to content

Commit

Permalink
Merge pull request #1591 from Giveth/optimize-qfRounds-query
Browse files Browse the repository at this point in the history
optimize qfRounds query
  • Loading branch information
RamRamez authored May 28, 2024
2 parents a94d6f9 + cb70a39 commit 7554f78
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 8 deletions.
28 changes: 23 additions & 5 deletions src/repositories/qfRoundRepository.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,34 @@
import { Field, Float, Int, ObjectType, registerEnumType } from 'type-graphql';
import { QfRound } from '../entities/qfRound';
import { AppDataSource } from '../orm';
import { QfArchivedRoundsOrderBy } from '../resolvers/qfRoundResolver';
import {
QfArchivedRoundsOrderBy,
QfRoundsArgs,
} from '../resolvers/qfRoundResolver';

const qfRoundEstimatedMatchingParamsCacheDuration = Number(
process.env.QF_ROUND_ESTIMATED_MATCHING_CACHE_DURATION || 60000,
);

export const findAllQfRounds = async (): Promise<QfRound[]> => {
return QfRound.createQueryBuilder('qf_round')
.addOrderBy('qf_round.id', 'DESC')
.getMany();
export const findAllQfRounds = async ({
slug,
activeOnly,
}: QfRoundsArgs): Promise<QfRound[]> => {
const query = QfRound.createQueryBuilder('qf_round').addOrderBy(
'qf_round.id',
'DESC',
);
if (slug) {
query.where('slug = :slug', { slug });
}
if (activeOnly) {
query.andWhere('"isActive" = true');
}
if (slug || activeOnly) {
const res = await query.getOne();
return res ? [res] : [];
}
return query.getMany();
};

export enum QfArchivedRoundsSortType {
Expand Down
17 changes: 15 additions & 2 deletions src/resolvers/qfRoundResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,24 @@ class QfArchivedRoundsArgs {
orderBy: QfArchivedRoundsOrderBy;
}

@Service()
@ArgsType()
export class QfRoundsArgs {
@Field(_type => String, { nullable: true })
slug?: string;

@Field(_type => Boolean, { nullable: true })
activeOnly?: boolean;
}

@Resolver(_of => User)
export class QfRoundResolver {
@Query(_returns => [QfRound], { nullable: true })
async qfRounds() {
return findAllQfRounds();
async qfRounds(
@Args()
{ slug, activeOnly }: QfRoundsArgs,
) {
return findAllQfRounds({ slug, activeOnly });
}

@Query(_returns => [QFArchivedRounds], { nullable: true })
Expand Down
1 change: 0 additions & 1 deletion test/graphqlQueries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1056,7 +1056,6 @@ export const fetchProjectBySlugQuery = `
sortingField
createdAt
updatedAt
}
givbackFactor
projectPower {
Expand Down

0 comments on commit 7554f78

Please sign in to comment.