Vasantharajan N
Giga Sage
Options
- Post History
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
on 02-20-2024 03:45 AM
How we can invoke 3rd party system that expose GraphQL API from ServiceNow?
Challenge that many users are facing is that how we need to use GraphQL query with variables when we try to invoke it. It's very simple in ServiceNow, We need to prepare a request body JSON that has key "query" and "variables"
Sample code for the same.
//Mock API endpoint used to demonstrate the capability
var api = 'https://api.mocki.io/v2/c4d7a195/graphql'
var request = new sn_ws.RESTMessageV2();
request.setEndpoint(api);
request.setHttpMethod('POST');
//It's very important to set Content-Type request header, else you wont get the result back
request.setRequestHeader("Content-type","application/json");
var requestPayLoad = {};
//Set the query string
requestPayLoad .query = 'query getUser($id: String!) { user(id: $id) {id email name}}';
//Set the variables expected as part of your query string
requestPayLoad .variables = {
"id" : "4dc70521-22bb-4396-b37a-4a927c66d43b"
}
//convert the requestPayLoad as JSON string
request.setRequestBody(JSON.stringify(requestPayLoad ));
//use the execute method to invoke the GraphQL API
var response = request.execute();
//Log the response body
gs.info(response.getBody())
//Log the response code
gs.info(response.getStatusCode())
#graphql #outbound #integration
- 725 Views