Optionals and Aliases
Optionals
By default, RapidQL requires every defined node to be included in the context or resulting query. If the node is not found in the results or context, an error will be thrown. To bypass this, you can define a node as an optional node using a "?".
rql.query(`
{
a
}
`, {
a: 1,
b: 2
})
In the example above, the result of this query will be {a: 1}.
rql.query(`
{
a,
c
}
`, {
a: 1,
b: 2
})
We have now added another node to the query "c". If this query is executed, RQL will throw an error: Name c does not exist in context: {a: 1, b: 2}.
rql.query(`
{
a,
?c
}
`, {
a: 1,
b: 2
})
To bypass this error, add a "?" right before the node in question. Now, if the node is not found in query results or the context, the node will be returned with a value of null.
The result of the above query will be: {a: 1, c: null}
Aliases
Sometimes, database and RapidAPI query names can get long. You may alias each node as a different name to streamline your results. To alias a node, you must follow this formula: aliasName:node.
{
MySQL.Demo.users.find() {
username,
email,
profile_image
RapidAPI.Mailboxlayer.checkEmail(email: email, apiKey: "****") {
score
}
}
}
Take the above query for example. Normally this will return a response like this:
{
"MySQL.Demo.users.find": [{
"email": "[email protected]",
"RapidAPI.Mailboxlayer.checkEmail": {
"score": 0.96
}
}]
}
The keys in this object are long and frustrating to access. Lets alias the MySQL and RapidAPI nodes.
{
database:MySQL.Demo.users.find() {
username,
email,
profile_image
valid:RapidAPI.Mailboxlayer.checkEmail(email: email, apiKey: "****") {
score
}
}
}
Now the response will be more readable...
{
"database": [{
"email": "[email protected]",
"valid": {
"score": 0.96
}
}]
}
NOTE: You can use an optional and alias together. Just write ?aliasName:node.
Updated about 6 years ago