-
Notifications
You must be signed in to change notification settings - Fork 23
/
graphql.js
232 lines (205 loc) · 6.72 KB
/
graphql.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
module.exports = function(RED) {
// https://github.com/axios/axios
var axios = require("axios");
var mustache = require("mustache");
var vers = "2.2.0";
function isReadable(value) {
return typeof value === 'object' && typeof value._read === 'function' && typeof value._readableState === 'object'
}
function safeJSONStringify(input, maxDepth) {
var output,
refs = [],
refsPaths = [];
maxDepth = maxDepth || 5;
function recursion(input, path, depth) {
var output = {},
pPath,
refIdx;
path = path || "";
depth = depth || 0;
depth++;
if (maxDepth && depth > maxDepth) {
return "{depth over " + maxDepth + "}";
}
for (var p in input) {
pPath = (path ? path + "." : "") + p;
if (typeof input[p] === "function") {
output[p] = "{function}";
} else if (input[p] && Buffer.isBuffer(input[p])) {
output[p] = "[object Buffer]";
} else if (input[p] && isReadable(input[p])) {
output[p] = "[object Readable]";
} else if (typeof input[p] === "object") {
refIdx = refs.indexOf(input[p]);
if (-1 !== refIdx) {
output[p] = "{reference to " + refsPaths[refIdx] + "}";
} else {
refs.push(input[p]);
refsPaths.push(pPath);
output[p] = recursion(input[p], pPath, depth);
}
} else {
output[p] = input[p];
}
}
return output;
}
if (typeof input === "object") {
output = recursion(input);
} else {
output = input;
}
return JSON.stringify(output);
}
function GraphqlNode(config) {
RED.nodes.createNode(this, config);
var node = this;
RED.log.debug("--- GraphqlNode v" + vers + " ---");
RED.log.debug("GraphqlNode node: " + safeJSONStringify(node));
RED.log.trace("GraphqlNode config: " + safeJSONStringify(config));
node.endpoint = config.endpoint;
node.token = config.token
RED.log.debug("node.endpoint: " + node.endpoint);
RED.log.debug("node.token: " + node.token)
}
RED.nodes.registerType("graphql-server", GraphqlNode, {
credentials: {
token: { type: "password" },
}
});
function GraphqlExecNode(config) {
RED.nodes.createNode(this, config);
var node = this;
node.graphqlConfig = RED.nodes.getNode(config.graphql); // Retrieve the config node
node.template = config.template;
node.name = config.name;
node.varsField = config.varsField || "variables";
node.syntax = config.syntax || "mustache";
node.showDebug = config.showDebug || false
node.token = node.credentials.token || "";
node.customHeaders = config.customHeaders || {}
node.varsField = config.varsField || "variables";
RED.log.debug("--- GraphqlExecNode ---");
if (!node.graphqlConfig) {
node.error("invalid graphql config");
}
function dataobject(context, msg){
data = {}
data.msg = msg;
data.global = {};
data.flow = {};
g_keys = context.global.keys();
f_keys = context.flow.keys();
for (k in g_keys){
data.global[g_keys[k]] = context.global.get(g_keys[k]);
};
for (k in f_keys){
data.flow[f_keys[k]] = context.flow.get(f_keys[k]);
};
return data
}
function callGraphQLServer(query, variables = {}, customHeaders = {}) {
let data = dataobject(node.context(), node.msg);
let url = mustache.render(node.graphqlConfig.endpoint, data);
let headers = customHeaders
const token = node.token || node.graphqlConfig.token || "";
if (token) {
headers["Authorization"] = `Bearer ${token}`
}
if (node.showDebug) {
node.log(safeJSONStringify(data));
node.log(headers.Authorization);
}
axios({
method: "POST",
url,
headers,
timeout: 20000,
data: {
query: query,
variables: variables
}
})
.then(function(response) {
if (!node.msg.payload) node.msg.payload = {};
switch (true) {
case response.status == 200 && !response.data.errors:
node.status({
fill: "green",
shape: "dot",
text: RED._("graphql.status.success")
});
node.msg.payload.graphql = response.data.data; // remove .data to see entire response
if (node.showDebug){
node.msg.debugInfo = {
data: response.data,
headers,
query,
variables
}
}
node.send(node.msg);
break;
case response.status == 200 && !!response.data.errors:
node.status({
fill: "yellow",
shape: "dot",
text: RED._("graphql.status.gqlError")
});
node.msg.payload.graphql = response.data.errors;
node.send([null, node.msg]);
break;
default:
node.status({
fill: "red",
shape: "dot",
text: "status: " + response.status
});
node.msg.payload.graphql = {
statusCode: response.status,
body: response.data
};
node.send([null, node.msg]);
break;
}
})
.catch(function(error) {
RED.log.debug("error:" + error);
node.status({ fill: "red", shape: "dot", text: "error" });
if (!node.msg.payload) node.msg.payload = {};
node.msg.payload.graphql = { error };
node.error("error: " + error);
node.send([null, node.msg]);
});
}
node.on("input", function(msg) {
RED.log.debug("--- on(input) ---");
RED.log.debug("msg: " + safeJSONStringify(msg));
node.msg = msg;
node.template = msg.template || node.template;
node.syntax = msg.syntax || node.syntax;
node.customHeaders = {...node.customHeaders, ...msg.customHeaders}
var query;
if (node.syntax === "mustache") {
query = mustache.render(node.template, msg);
} else {
query = node.template;
}
var variables = msg[node.varsField] || {}
callGraphQLServer(query, variables, node.customHeaders);
});
node.on("close", function() {
RED.log.debug("--- closing node ---");
node.graphqlConfig.credentials.token = node.token || "";
RED.nodes.addCredentials(
node.graphqlConfig,
node.graphqlConfig.credentials
);
});
}
RED.nodes.registerType("graphql", GraphqlExecNode, {
credentials: {
token: { type: "password" },
}
});
};