GraphQL query not working to fetch data
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
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.
"query": "query { devices(location: \"$loc\") { id name } }",
"variables": {
"loc": "TYO43"
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
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,
