I have this GraphQL query:
{
timeline(limit:10){
eventType
level: eventSeverity
},
}
I would like to set the 'eventSeverity' alias name using a variable, rather than the fixed name 'level'. Something like this:
query($name:String!)
{
timeline(limit:10){
eventType
$name: eventSeverity
},
}
But running the above yields this error:
Syntax Error GraphQL request (5:5) Expected Name, found $
Is it possible to use a variable value as an alias name at all?
You cannot use dynamic field name in GraphQL, but what you can do is create a field that accepts a name argument, example
query($name: String!) {
timeline(limit: 10) {
eventType
eventSeverity(name: $name) // Write a resolve function that returns timeline[name]
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With