I’m trying to figure out how to use values from one part of a GraphQL query as variables in another part of the same query. This doesn’t seem to be supported natively in GraphQL.
I have a scenario where I need to fetch sensor data from a manufacturing process. I want to get temperature readings during specific time periods, but the start and end times come from the same query.
Here’s what I wish I could do in a single query:
{
items {
id,
process(category:"heating") {
title,
$startTime: beginTimestamp,
$endTime: finishTimestamp,
sensorData(startDate:$startTime, endDate:$endTime){
recorded,
reading
}
}
}
}
Right now I have to split this into two separate queries. First I get the timestamps:
{
items {
id,
process(category:"heating") {
title,
beginTimestamp,
finishTimestamp
}
}
}
Then I make another query for each item using those values:
{
item(id:"{ITEM_ID}") {
id
process(title:"{PROCESS_NAME}") {
sensorData(startDate:"{BEGIN_TIME}", endDate:"{FINISH_TIME}"){
recorded,
reading
}
}
}
}
This defeats the purpose of GraphQL’s single query approach. Is there any way to accomplish this with one query instead of multiple round trips?