What you'll achieve By the end of this guide, you will know how to query back-links (reverse relationships) using GraphQL.
Prerequisites: TerminusDB running on
localhost:6363with the Star Wars dataset cloned. If you haven't done this yet, follow the Explore a Real Dataset tutorial (Steps 1–2), or run:curl -u admin:root -X POST http://localhost:6363/api/clone/admin/star-wars \ -H "Content-Type: application/json" \ -H "Authorization-Remote: Basic cHVibGljOnB1YmxpYw==" \ -d '{"remote_url": "https://data.terminusdb.org/public/star-wars", "label": "Star Wars", "comment": "Star Wars dataset"}'
Using a Back Link
Many times when we are looking at an object, we are interested in which objects are pointing to it. In the TerminusDB, each object gets a number of extended queries which allows one to discover any objects which point at that object.
Once you have cloned the Star Wars demo, go to the GraphQL query panel and type:
query{
People(limit:1) {
█
}
}We would like to find the first person in the database, and then find out which starships they are the pilot of. A Starship has a pilot field, and the backlink is automatically constructed as the pilot_of_Starship by TerminusDB.
query{
People(limit:1){
label
_pilot_of_Starship{
label
}
}
}This back link will give us back the following:
{
"data": {
"People": [
{
"label": "Luke Skywalker",
"_pilot_of_Starship": [
{
"label": "X-wing"
},
{
"label": "Imperial shuttle"
}
]
}
]
}
}Backlinking allows us to focus on modeling our data in a natural way, while still allowing us to follow the graph in either direction of a field or its opposite without bias. Sometimes these backlinks are called inverseOf or inverse links.