What you'll achieve By the end of this guide, you will know how to traverse graph paths using TerminusDB's GraphQL path queries.
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 Path Query
Sometimes we want to search for links that are not immediate, but need to follow a chain of links to get the object of interest. TerminusDB gives us path queries which allow us to succinctly express this.
We can find a path in GraphQL by using the _path_to_CLASS query, where CLASS is the name of one of our classes. One path should be populated for each of the available classes.
To find everyone who was in a film with Chewbacca, we can write:
query{
People(filter:{label:{eq:"Chewbacca"}}){
label
_path_to_People(path:"film,<film"){
label
}
}
}The film is the current film which the Chewbacca object points at. Then <film means follow backwards to people on the film field.
This process can be repeated to find second order connections, as follows:
query{
People(filter:{label:{eq:"Chewbacca"}}){
label
_path_to_People(path:"(film,<film){1,2}"){
label
}
}
}This says that we should repeat the process one or two times before terminating.
More complex patterns can be built using the full path query syntax described in our documentation.