Path Queries in GraphQL

How to do path queries on TerminusDB and TerminusCMS data products using GraphQL

To use this How-To, first clone the Star Wars demo into your team on TerminusCMS. You will then have full access to the data needed for this tutorial

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. TerminusCMS 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 at which the Chewbacca object points at. Then <film means follow backwards to people in 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.

Last updated