How to call GraphQL API end point using REST API method or any script method?

Ganeshm1
Tera Guru

Hi All,

 

I have created a graphQL API and using the below query I am able to get the result as expected in GraphQL Explorer. 

Query:

query {
  x1306737Graphql {
    IncidentQuerySchema {
      getIncidents(
        priority: "1"
      ) {
        shortDescription
        priority
        number
        
      }
    }
  }
}



I am not getting how to pass this query in REST API POST/GET or any server side script method to get the response? Can anyone help me how we can pass this query in REST API to get the response as expected. 

 

Thanks in advance!

 

Regards,
Ganesh

2 ACCEPTED SOLUTIONS

Hi @DineshS : I tried with below query in the content and it worked fine for me. 

{"query" : "query { x1306737Graphql { IncidentQuerySchema { getIncidents( priority: \\\"2\\\" ) { shortDescription priority number } } } }"}

 

Thank you so much for your response on this question. 

 

Regards,

Ganesh

View solution in original post

DineshS
Tera Guru

Hi @Ganeshm1 , I realised the trick is to convert the GraphQL to JSON format and found the site https://datafetcher.com/graphql-json-body-converter really helpful for the conversion. Could you please try and let me know if it works ?

DineshS_0-1717098489461.png

 

View solution in original post

6 REPLIES 6

Ganeshm1
Tera Guru

@Sohail Khilji : Please provide your inputs here. Thank You!

DineshS
Tera Guru

Hi @Ganeshm1 

Please try sending the request as an encoded string in the body. For example replace the new line with \r\n

{“query”:”query{\r\n and so on

Hello @DineshS,

Thank you for your reply. I tried replacing the new line with \r\n but it didn't work for me. Also I am getting error like "{"error":{"message":"Exception while reading request","detail":"The payload is not valid JSON."},"status":"failure"}"

 

Please find the below details of my configurations. 
1. Created a REST message with end point https://<myInstanceID>.service-now.com/api/now/graphql

2. Added HTTP heads like below

accept = application/json

content-type = application/json

3. Created POST Method with same end pint mentioned above

4. Added content like below

{“query”: "query {
  x1306737Graphql {
    IncidentQuerySchema {
       getIncidents(
       priority: "2"
      ) {
        shortDescription
        priority
        number     
      }
    }
  }
}"
}

When I test status code is coming as 200 but query is not returning expected result. 

I am not getting what I am missing here. Please help me with your inputs here. Thank You!

 

Regards,

Ganesh

 

Could you also please share the encoded body you tried with?