Skip to content

Commit b22598b

Browse files
author
Boris
committed
refactor: query stuff to helpers
1 parent 785f67c commit b22598b

23 files changed

+104
-105
lines changed

example/src/schema/mutation/helpers/queueFind.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,19 @@ import { MutationError, ErrorCodeEnum } from './Error';
22
import { createBullConnection } from '../../../connectRedis';
33
import { Queue } from 'bullmq';
44

5-
export async function findQueue(prefix: string, queueName: string): Promise<Queue> {
5+
export async function findQueue(
6+
prefix: string,
7+
queueName: string,
8+
checkExistence: boolean = true
9+
): Promise<Queue> {
610
const connection = createBullConnection('custom');
7-
const queueExists = await connection.exists([prefix, queueName, 'meta'].join(':'));
811

9-
if (!queueExists) {
10-
throw new MutationError('Queue not found!', ErrorCodeEnum.QUEUE_NOT_FOUND);
12+
if (checkExistence) {
13+
const queueExists = await connection.exists([prefix, queueName, 'meta'].join(':'));
14+
15+
if (!queueExists) {
16+
throw new MutationError('Queue not found!', ErrorCodeEnum.QUEUE_NOT_FOUND);
17+
}
1118
}
1219

1320
const queue = new Queue(queueName, {

example/src/schema/mutation/helpers/queueGet.ts

Lines changed: 0 additions & 19 deletions
This file was deleted.

example/src/schema/mutation/jobAdd.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { SchemaComposer, ObjectTypeComposerFieldConfigAsObjectDefinition } from 'graphql-compose';
22
import { getJobTC } from '../types/job/Job';
3-
import { getQueue } from './helpers/queueGet';
3+
import { findQueue } from './helpers/queueFind';
44

55
export function createJobAddFC(
66
sc: SchemaComposer<any>
@@ -37,7 +37,7 @@ export function createJobAddFC(
3737
}),
3838
},
3939
resolve: async (_, { prefix, queueName, jobName, data, options }) => {
40-
const queue = getQueue(prefix, queueName);
40+
const queue = await findQueue(prefix, queueName);
4141
const job = await queue.add(jobName, data, options);
4242
return {
4343
job,

example/src/schema/mutation/jobAddBulk.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getQueue } from './helpers/queueGet';
1+
import { findQueue } from './helpers/queueFind';
22
import { SchemaComposer, ObjectTypeComposerFieldConfigAsObjectDefinition } from 'graphql-compose';
33
import { getJobTC } from '../types/job/Job';
44

@@ -44,7 +44,7 @@ export function createJobAddBulkFC(
4444
.getTypePlural(),
4545
},
4646
resolve: async (_, { prefix, queueName, jobs }) => {
47-
const queue = getQueue(prefix, queueName);
47+
const queue = await findQueue(prefix, queueName);
4848
const jobsRes = await queue.addBulk(jobs);
4949
return {
5050
jobs: jobsRes,

example/src/schema/mutation/jobAddCron.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getQueue } from './helpers/queueGet';
1+
import { findQueue } from './helpers/queueFind';
22
import { SchemaComposer, ObjectTypeComposerFieldConfigAsObjectDefinition } from 'graphql-compose';
33
import { getJobTC } from '../types/job/Job';
44

@@ -49,7 +49,7 @@ export function createJobAddCronFC(
4949
}),
5050
},
5151
resolve: async (_, { prefix, queueName, jobName, data, options }) => {
52-
const queue = getQueue(prefix, queueName);
52+
const queue = await findQueue(prefix, queueName);
5353
const job = await queue.add(jobName, data, options);
5454
return {
5555
job,

example/src/schema/mutation/jobAddEvery.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getQueue } from './helpers/queueGet';
1+
import { findQueue } from './helpers/queueFind';
22
import { SchemaComposer, ObjectTypeComposerFieldConfigAsObjectDefinition } from 'graphql-compose';
33
import { getJobTC } from '../types/job/Job';
44

@@ -48,7 +48,7 @@ export function createJobAddEveryFC(
4848
}),
4949
},
5050
resolve: async (_, { prefix, queueName, jobName, data, options }) => {
51-
const queue = getQueue(prefix, queueName);
51+
const queue = await findQueue(prefix, queueName);
5252
const job = await queue.add(jobName, data, options);
5353
return {
5454
job,

example/src/schema/mutation/jobDiscard.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { SchemaComposer, ObjectTypeComposerFieldConfigAsObjectDefinition } from 'graphql-compose';
22
import { MutationError, ErrorCodeEnum } from './helpers/Error';
33
import { getJobStatusEnumTC } from '../types';
4-
import { getQueue } from './helpers/queueGet';
4+
import { findQueue } from './helpers/queueFind';
55

66
export function createJobDiscardFC(
77
sc: SchemaComposer<any>
@@ -23,7 +23,7 @@ export function createJobDiscardFC(
2323
id: 'String!',
2424
},
2525
resolve: async (_, { prefix, queueName, id }) => {
26-
const queue = getQueue(prefix, queueName);
26+
const queue = await findQueue(prefix, queueName);
2727
const job = await queue.getJob(id);
2828
if (!job) throw new MutationError('Job not found!', ErrorCodeEnum.JOB_NOT_FOUND);
2929
await job.discard();

example/src/schema/mutation/jobLogAdd.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { SchemaComposer, ObjectTypeComposerFieldConfigAsObjectDefinition } from 'graphql-compose';
22
import { MutationError, ErrorCodeEnum } from './helpers/Error';
33
import { getJobStatusEnumTC } from '../types';
4-
import { getQueue } from './helpers/queueGet';
4+
import { findQueue } from './helpers/queueFind';
55

66
export function createJobLogAddFC(
77
sc: SchemaComposer<any>
@@ -24,7 +24,7 @@ export function createJobLogAddFC(
2424
row: 'String!',
2525
},
2626
resolve: async (_, { prefix, queueName, id, row }) => {
27-
const queue = getQueue(prefix, queueName);
27+
const queue = await findQueue(prefix, queueName);
2828
const job = await queue.getJob(id);
2929
if (!job) throw new MutationError('Job not found!', ErrorCodeEnum.JOB_NOT_FOUND);
3030
const logRes = await job.log(row);

example/src/schema/mutation/jobMoveToCompleted.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getQueue } from './helpers/queueGet';
1+
import { findQueue } from './helpers/queueFind';
22
import { SchemaComposer, ObjectTypeComposerFieldConfigAsObjectDefinition } from 'graphql-compose';
33
import { getJobTC } from '../types/job/Job';
44

@@ -22,7 +22,7 @@ export function jobMoveToCompletedFC(
2222
id: 'String!',
2323
},
2424
resolve: async (_, { prefix, queueName, id }) => {
25-
const queue = getQueue(prefix, queueName);
25+
const queue = await findQueue(prefix, queueName);
2626
const job = await queue.getJob(id);
2727
if (job) {
2828
await job.moveToCompleted({}, 'tokenmustbehere'); //TODO: нати где брать токен

example/src/schema/mutation/jobPromote.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { SchemaComposer, ObjectTypeComposerFieldConfigAsObjectDefinition } from 'graphql-compose';
22
import { MutationError, ErrorCodeEnum } from './helpers/Error';
33
import { getJobStatusEnumTC } from '../types';
4-
import { getQueue } from './helpers/queueGet';
4+
import { findQueue } from './helpers/queueFind';
55

66
export function createjobPromoteFC(
77
sc: SchemaComposer<any>
@@ -23,7 +23,7 @@ export function createjobPromoteFC(
2323
id: 'String!',
2424
},
2525
resolve: async (_, { prefix, queueName, id }) => {
26-
const queue = getQueue(prefix, queueName);
26+
const queue = await findQueue(prefix, queueName);
2727
const job = await queue.getJob(id);
2828
if (!job) throw new MutationError('Job not found!', ErrorCodeEnum.JOB_NOT_FOUND);
2929
await job.promote();

0 commit comments

Comments
 (0)