From d25d327ef79275fcb7a4b623d71b85bdfd4b33da Mon Sep 17 00:00:00 2001 From: Faruk AYDIN Date: Thu, 6 Jan 2022 22:08:46 +0300 Subject: [PATCH] feat: Implement update step graphQL mutation --- .../src/graphql/mutations/update-step.ts | 48 +++++++++++++++++++ packages/backend/src/graphql/root-mutation.ts | 2 + packages/backend/src/models/step.ts | 2 +- 3 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 packages/backend/src/graphql/mutations/update-step.ts diff --git a/packages/backend/src/graphql/mutations/update-step.ts b/packages/backend/src/graphql/mutations/update-step.ts new file mode 100644 index 00000000..f89a4695 --- /dev/null +++ b/packages/backend/src/graphql/mutations/update-step.ts @@ -0,0 +1,48 @@ +import { GraphQLInt, GraphQLString, GraphQLNonNull } from 'graphql'; +import Flow from '../../models/flow'; +import Step from '../../models/step'; +import flowType from '../types/flow'; +import stepType from '../types/step'; +import availableAppsEnumType from '../types/available-apps-enum-type'; +import RequestWithCurrentUser from '../../types/express/request-with-current-user'; + +type Params = { + id: number, + flowId: number, + key: string, + appKey: string, + connectionId: number +} +const updateStepResolver = async (params: Params, req: RequestWithCurrentUser) => { + const flow = await Flow.query().findOne({ + user_id: req.currentUser.id, + id: params.flowId + }).throwIfNotFound(); + + let step = await Step.query().findOne({ + flow_id: flow.id, + id: params.id + }).throwIfNotFound(); + + step = await step.$query().patchAndFetch({ + ...step, + key: params.key, + appKey: params.appKey, + connectionId: params.connectionId + }) + + return step; +} + +const updateStep = { + type: stepType, + args: { + id: { type: GraphQLNonNull(GraphQLInt) }, + flowId: { type: GraphQLNonNull(GraphQLInt) }, + key: { type: GraphQLString }, + appKey: { type: availableAppsEnumType }, + }, + resolve: (_: any, params: Params, req: RequestWithCurrentUser) => updateStepResolver(params, req) +}; + +export default updateStep; diff --git a/packages/backend/src/graphql/root-mutation.ts b/packages/backend/src/graphql/root-mutation.ts index 98a0bb4f..9096d649 100644 --- a/packages/backend/src/graphql/root-mutation.ts +++ b/packages/backend/src/graphql/root-mutation.ts @@ -8,6 +8,7 @@ import deleteConnection from './mutations/delete-connection'; import createFlow from './mutations/create-flow'; import updateFlow from './mutations/update-flow'; import createStep from './mutations/create-step'; +import updateStep from './mutations/update-step'; import executeStep from './mutations/execute-step'; import login from './mutations/login'; @@ -23,6 +24,7 @@ const rootMutation = new GraphQLObjectType({ createFlow, updateFlow, createStep, + updateStep, executeStep, login } diff --git a/packages/backend/src/models/step.ts b/packages/backend/src/models/step.ts index 53fa39e6..1428364c 100644 --- a/packages/backend/src/models/step.ts +++ b/packages/backend/src/models/step.ts @@ -29,7 +29,7 @@ class Step extends Base { appKey: { type: 'string', minLength: 1, maxLength: 255 }, type: { type: "string", enum: ["action", "trigger"] }, connectionId: { type: 'integer' }, - parameters: { type: 'object' }, + parameters: { type: ['object', null] }, } }