diff --git a/packages/backend/src/graphql/mutations/delete-step.ts b/packages/backend/src/graphql/mutations/delete-step.ts index 387ed1ab..b3725982 100644 --- a/packages/backend/src/graphql/mutations/delete-step.ts +++ b/packages/backend/src/graphql/mutations/delete-step.ts @@ -1,6 +1,6 @@ -import { GraphQLString, GraphQLNonNull, GraphQLBoolean } from 'graphql'; -import Step from '../../models/step'; +import { GraphQLString, GraphQLNonNull } from 'graphql'; import RequestWithCurrentUser from '../../types/express/request-with-current-user'; +import stepType from '../types/step'; type Params = { id: string; @@ -10,19 +10,39 @@ const deleteStepResolver = async ( params: Params, req: RequestWithCurrentUser ) => { - await req.currentUser + const step = await req.currentUser .$relatedQuery('steps') - .delete() + .withGraphFetched('flow') .findOne({ - id: params.id, + 'steps.id': params.id, }) .throwIfNotFound(); - return; + await step.$query().delete(); + + const nextSteps = await step.flow + .$relatedQuery('steps') + .where('position', '>', step.position); + + const nextStepQueries = nextSteps.map(async (nextStep) => { + await nextStep.$query().patch({ + ...nextStep, + position: nextStep.position - 1, + }); + }); + + await Promise.all(nextStepQueries); + + step.flow = await step.flow + .$query() + .withGraphJoined('steps') + .orderBy('steps.position', 'asc'); + + return step; }; const deleteStep = { - type: GraphQLBoolean, + type: stepType, args: { id: { type: GraphQLNonNull(GraphQLString) }, }, diff --git a/packages/backend/src/graphql/root-query.ts b/packages/backend/src/graphql/root-query.ts index 08cdb318..2059f18e 100644 --- a/packages/backend/src/graphql/root-query.ts +++ b/packages/backend/src/graphql/root-query.ts @@ -16,8 +16,8 @@ const rootQuery = new GraphQLObjectType({ getAppConnections, testConnection, getFlow, - getFlows - } + getFlows, + }, }); export default rootQuery; diff --git a/packages/backend/src/graphql/types/flow.ts b/packages/backend/src/graphql/types/flow.ts index 4665b111..50170145 100644 --- a/packages/backend/src/graphql/types/flow.ts +++ b/packages/backend/src/graphql/types/flow.ts @@ -5,17 +5,21 @@ import { GraphQLString, GraphQLBoolean, } from 'graphql'; -import StepType from './step'; const flowType = new GraphQLObjectType({ name: 'Flow', - fields: { - id: { type: GraphQLString }, - name: { type: GraphQLString }, - active: { type: GraphQLBoolean }, - steps: { - type: GraphQLList(StepType), - }, + fields: () => { + // eslint-disable-next-line @typescript-eslint/no-var-requires + const StepType = require('./step').default; + + return { + id: { type: GraphQLString }, + name: { type: GraphQLString }, + active: { type: GraphQLBoolean }, + steps: { + type: GraphQLList(StepType), + }, + }; }, }); diff --git a/packages/backend/src/graphql/types/step.ts b/packages/backend/src/graphql/types/step.ts index 018edce7..18ff6f17 100644 --- a/packages/backend/src/graphql/types/step.ts +++ b/packages/backend/src/graphql/types/step.ts @@ -9,23 +9,29 @@ import ConnectionType from './connection'; const stepType = new GraphQLObjectType({ name: 'Step', - fields: { - id: { type: GraphQLString }, - previousStepId: { type: GraphQLString }, - key: { type: GraphQLString }, - appKey: { type: GraphQLString }, - type: { - type: new GraphQLEnumType({ - name: 'StepEnumType', - values: { - trigger: { value: 'trigger' }, - action: { value: 'action' }, - }, - }), - }, - parameters: { type: GraphQLString }, - connection: { type: ConnectionType }, - position: { type: GraphQLInt }, + fields: () => { + // eslint-disable-next-line @typescript-eslint/no-var-requires + const FlowType = require('./flow').default; + + return { + id: { type: GraphQLString }, + previousStepId: { type: GraphQLString }, + key: { type: GraphQLString }, + appKey: { type: GraphQLString }, + type: { + type: new GraphQLEnumType({ + name: 'StepEnumType', + values: { + trigger: { value: 'trigger' }, + action: { value: 'action' }, + }, + }), + }, + parameters: { type: GraphQLString }, + connection: { type: ConnectionType }, + flow: { type: FlowType }, + position: { type: GraphQLInt }, + }; }, }); diff --git a/packages/backend/src/models/step.ts b/packages/backend/src/models/step.ts index 43073fb8..b6f63637 100644 --- a/packages/backend/src/models/step.ts +++ b/packages/backend/src/models/step.ts @@ -17,6 +17,7 @@ class Step extends Base { position: number; parameters: string; connection?: Connection; + flow?: Flow; static tableName = 'steps';