diff --git a/src/requests/flow.ts b/src/requests/flow.ts index da788cd0..3b75d286 100644 --- a/src/requests/flow.ts +++ b/src/requests/flow.ts @@ -2,7 +2,7 @@ import type { GraphQLClient } from "graphql-request"; import { gql } from "graphql-request"; import { capitalize } from "lodash"; -import type { FlowGraph } from "../types"; +import type { FlowGraph, FlowStatus } from "../types"; export class FlowClient { protected client: GraphQLClient; @@ -26,6 +26,10 @@ export class FlowClient { return publishFlow(this.client, args); } + async setStatus(args: { flow: { id: string }; status: FlowStatus }) { + return setStatus(this.client, args); + } + /** * Only used in test environments */ @@ -264,3 +268,44 @@ export async function _destroyPublishedFlow( ); return Boolean(response.delete_published_flows_by_pk?.id); } + +interface SetFlowStatus { + flow: { + id: string; + status: FlowStatus; + }; +} + +async function setStatus( + client: GraphQLClient, + args: { flow: { id: string }; status: FlowStatus }, +) { + try { + const { flow } = await client.request<SetFlowStatus>( + gql` + mutation SetFlowStatus( + $flowId: uuid! + $status: flow_status_enum_enum! + ) { + flow: update_flows_by_pk( + pk_columns: { id: $flowId } + _set: { status: $status } + ) { + id + status + } + } + `, + { + flowId: args.flow.id, + status: args.status, + }, + ); + + return flow; + } catch (error) { + new Error( + `Failed to update flow status to "${args.status}". Error: ${error}`, + ); + } +} diff --git a/src/types/flow.ts b/src/types/flow.ts index 310cbeae..16d6f9fa 100644 --- a/src/types/flow.ts +++ b/src/types/flow.ts @@ -36,3 +36,5 @@ export interface NormalizedNode extends IndexedNode { } export type NormalizedFlow = Array<NormalizedNode>; + +export type FlowStatus = "online" | "offline";