-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.js
88 lines (80 loc) · 2.25 KB
/
client.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
require("isomorphic-fetch");
class Client {
constructor({ hasuraSecret, targetURL }) {
this.secret = hasuraSecret;
this.url = targetURL;
}
async graphQL(query, variables) {
const result = await fetch(this.url, {
method: "POST",
body: JSON.stringify({ query, variables }),
headers: {
"x-hasura-admin-secret": this.secret,
},
});
return result.json();
}
async getFlowData(slug) {
const { data, errors } = await this.graphQL(
`query GetData($slug: String!) {
flows(where: {slug: {_eq: $slug}}) {
id
slug
team {
slug
}
data
publishedFlows: published_flows(limit: 1, order_by: {created_at: desc}) {
id
data
}
}
}
`,
{
slug: slug,
}
);
if (errors || !data) throw new Error(formatJSON({ data, errors }));
return data.flows;
}
async updateFlowAndPublishedFlow(flowId, liveFlowData, publishedFlowId, publishedFlowData) {
const { data, errors } = await this.graphQL(
`mutation UpdateFlowAndPublishedFlow ($flowId: uuid!, $liveData: jsonb!, $publishedFlowId: Int!, $publishedData: jsonb!) {
update_flows_by_pk(pk_columns: {id: $flowId}, _set: {data: $liveData}) {
id
}
update_published_flows_by_pk(pk_columns: {id: $publishedFlowId}, _set: {data: $publishedData}) {
id
}
}`,
{
flowId: flowId,
liveData: liveFlowData,
publishedFlowId: publishedFlowId,
publishedData: publishedFlowData,
}
);
if (errors || !data) throw new Error(formatJSON({ data, errors }));
return { ...data };
}
async updateFlow(flowId, liveFlowData) {
const { data, errors } = await this.graphQL(
`mutation UpdateFlow ($flowId: uuid!, $liveData: jsonb!) {
update_flows_by_pk(pk_columns: {id: $flowId}, _set: {data: $liveData}) {
id
}
}`,
{
flowId: flowId,
liveData: liveFlowData,
}
);
if (errors || !data) throw new Error(formatJSON({ data, errors }));
return { ...data };
}
}
function formatJSON({ data, errors }) {
return JSON.stringify({ data, errors }, null, 2);
}
module.exports = Client;