GraphQL query not working to fetch data

User205031
Tera Contributor

Hi All,

 

I have a requirement. In a catalog form, if a user selects a location manually, it will make a call to third party system and fetch data based on location.

 

I have created a script include for it. However, its not working.

 
 
getDevices: function() {
    var u_loc = this.getParameter('sysparm_location');
    gs.info('N7' + u_loc);
    this.token = gs.getProperty('N_Token');
    gs.info('N9' + this.token);
    var api = 'https://xxx/api/graphql/';
    var request = new sn_ws.RESTMessageV2('N API','Get Device details');
    request.setEndpoint(api);
    request.setHttpMethod('POST');
    request.setRequestHeader('Authorization', 'Token ' + this.token);
    request.setRequestHeader('Content-Type', 'application/json');
    var requestPayLoad = {};
    //requestPayLoad.query = 'query getDevices($loc: String!){ devices(location: "$loc") { id name } }';
    requestPayLoad.query = 'query { devices(location: "$loc") { id name } }';
    requestPayLoad.variables = { "loc": u_loc };
    gs.info("N payloadstr447" + JSON.stringify(requestPayLoad));
    request.setRequestBody(JSON.stringify(requestPayLoad));
    var response = request.execute();
    var responseBody = response.getBody();
    gs.info('N24'+responseBody);
    var httpStatus = response.getStatusCode();
    var result = JSON.parse(responseBody);
    gs.info('Na27'+result);
    //return responseBody;
   
  },
 
Logs I am getting:
 
1. N payloadstr447{
"query": "query { devices(location: \"$loc\") { id name } }",
"variables": {
"loc": "TYO43"
}
}
 
2. Nautobot24{"errors":[{"message":"{'location': ['Select a valid choice. $loc is not one of the available choices.']}","locations":[{"line":1,"column":9}],"path":["devices"]}],"data":{"devices":null}}
 
How can I modify the script to get the values?
3 REPLIES 3

Ratnakar7
Mega Sage

Hi @User205031 ,

The issue in your script is that you're not actually passing the GraphQL variable into the query - you've hardcoded "$loc" as a literal string inside the query. That's why the API error says "$loc is not one of the available choices." You need to declare the variable in the query definition and reference it properly.

Please refer below blog and article: 
Using GraphQL API's for Better Performance 
Querying Data with your GraphQL API Tutorial 

Thanks,

Ratnakar

Hi @Ratnakar7 ,

 

I have declared it in a variable parameter. It should get the location value.

Ratnakar7
Mega Sage

Hi @User205031 ,

Even though you've declared loc in the variables object, the query itself also needs to declare and consume it. Right now, "$loc" is being treated as a literal string.
Try changing your query to:

query getDevices($loc: String!) {
  devices(location: $loc) {
    id
    name
  }
}

And send the payload as:

{
  "query": "query getDevices($loc: String!) { devices(location: $loc) { id name } }",
  "variables": { "loc": "TYO43" }
}

 


That way, GraphQL will substitute the variable correctly instead of treating it as a string literal.


Thanks,
Ratnakar