Skip to content

Commit fb2b02c

Browse files
authored
feat: add queue.jobs sorting order ASC, DESC
1 parent 80ef7e1 commit fb2b02c

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

src/types/queue/Queue.jobs.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Queue } from 'bullmq';
22
import { SchemaComposer, ObjectTypeComposerFieldConfigDefinition } from 'graphql-compose';
33
import { getJobStatusEnumTC } from '../scalars/JobStatusEnum';
4+
import { getOrderEnumTC, OrderEnum } from '../scalars/OrderEnum';
45
import { getJobTC } from '../job/Job';
56
import { Options } from '../../definitions';
67

@@ -20,10 +21,13 @@ export function createJobsFC(
2021
type: 'Int',
2122
defaultValue: 20,
2223
},
23-
// TODO: add sorting
24+
order: {
25+
type: getOrderEnumTC(sc, opts),
26+
defaultValue: OrderEnum.DESC,
27+
},
2428
},
25-
resolve: async (queue: Queue, { status, start, end }) => {
26-
return queue.getJobs([status], start, end, false);
29+
resolve: async (queue: Queue, { status, start, end, order }) => {
30+
return queue.getJobs([status], start, end, order === OrderEnum.ASC);
2731
},
2832
};
2933
}

src/types/scalars/OrderEnum.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { SchemaComposer } from 'graphql-compose';
2+
import { Options } from '../../definitions';
3+
4+
export enum OrderEnum {
5+
ASC = 'asc',
6+
DESC = 'desc',
7+
}
8+
9+
export function getOrderEnumTC(sc: SchemaComposer<any>, opts: Options) {
10+
const { typePrefix } = opts;
11+
return sc.getOrCreateETC(`${typePrefix}OrderEnum`, (etc) => {
12+
etc.addFields({
13+
ASC: { value: 'asc' },
14+
DESC: { value: 'desc' },
15+
});
16+
});
17+
}

0 commit comments

Comments
 (0)