-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathGraphQL.js
48 lines (43 loc) · 1.21 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
/**
*
* Performs a GraphQL call
* The inbound params should contain the following properties:
* string - string - the GraphQL text to execute
*
* The results are the body of the GraphQL response.
*
*/
var openwhisk = require('openwhisk');
var request = require('request');
const {promisify} = require('util');
var _ = require('lodash');
function samePackage(action) {
// return (process.env.__OW_ACTION_NAME.replace(/\/[^\/]+$/,"") + "/" + action).replace(/^\/[^\/]+\//,"");
return "WatsonWorkspace/" + action;
}
async function main(query) {
var postPromise = promisify(request.post)
var ow = openwhisk(
_.get(query,"WatsonWorkspace.OWArgs",{})
);
var token = await ow.actions.invoke({
name: samePackage('Token'),
result: true,
blocking: true
});
var response = await postPromise(
'https://api.watsonwork.ibm.com/graphql', {
headers: {
'Content-Type': 'application/graphql',
'Authorization': 'Bearer ' + token.jwt,
'x-graphql-view': 'TYPED_ANNOTATIONS,BETA,PUBLIC'
},
body: query.string
});
response.body = JSON.parse(response.body);
return response.body;
}
exports.main = main;
exports.setOpenwhisk = function(obj) {
openwhisk = obj;
}