Skip to content

Commit

Permalink
feat: add queue.jobs sorting order ASC, DESC
Browse files Browse the repository at this point in the history
  • Loading branch information
intpro authored May 17, 2020
1 parent 80ef7e1 commit fb2b02c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/types/queue/Queue.jobs.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Queue } from 'bullmq';
import { SchemaComposer, ObjectTypeComposerFieldConfigDefinition } from 'graphql-compose';
import { getJobStatusEnumTC } from '../scalars/JobStatusEnum';
import { getOrderEnumTC, OrderEnum } from '../scalars/OrderEnum';
import { getJobTC } from '../job/Job';
import { Options } from '../../definitions';

Expand All @@ -20,10 +21,13 @@ export function createJobsFC(
type: 'Int',
defaultValue: 20,
},
// TODO: add sorting
order: {
type: getOrderEnumTC(sc, opts),
defaultValue: OrderEnum.DESC,
},
},
resolve: async (queue: Queue, { status, start, end }) => {
return queue.getJobs([status], start, end, false);
resolve: async (queue: Queue, { status, start, end, order }) => {
return queue.getJobs([status], start, end, order === OrderEnum.ASC);
},
};
}
17 changes: 17 additions & 0 deletions src/types/scalars/OrderEnum.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { SchemaComposer } from 'graphql-compose';
import { Options } from '../../definitions';

export enum OrderEnum {
ASC = 'asc',
DESC = 'desc',
}

export function getOrderEnumTC(sc: SchemaComposer<any>, opts: Options) {
const { typePrefix } = opts;
return sc.getOrCreateETC(`${typePrefix}OrderEnum`, (etc) => {
etc.addFields({
ASC: { value: 'asc' },
DESC: { value: 'desc' },
});
});
}

0 comments on commit fb2b02c

Please sign in to comment.