If we compare that with GraphQL, GraphQL is very much a query language designed for working with APIs. So we have a type system that describes exactly the data that's available to the client, how it's connected. This is the data graph. And then to describe traversals through the data graph, we define a selection set in GraphQL.
So let's look at some examples. So let's say I want to see all of the articles in the news graph. In Cypher, I write a ASCII art-like pattern, parentheses represent nodes, in this case, find all the article nodes and return them. With GraphQL, in my selection set, I start with the article's query field, and then describe the fields of the articles that I want to return.
What if I want to see the ten most recent articles? Well, Cypher has the functionality for ordering, limiting, skipping, for basic pagination. This isn't built into GraphQL, but we can work with these things as field arguments. So perhaps our articles query field has a sort order argument and a limit argument that allows us to accomplish the same thing.
What if I want to see the ten most articles and their topics? Well in Cypher, I add a more complex graph pattern. So you can see here first we're matching on all of the articles and returning the first by date published. Then we have another graph pattern where we're traversing out from this article node along this has topic relationship to the topic nodes and returning both of those. And now we see the ten most recent articles and their connected topics. In GraphQL, we would just add to our selection set to describe this traversal now from the articles to the topics. So we're starting to create a nested selection set here.
What if I also want, not only the ten most recent articles, their topics, but also what are other articles in those topics? Well, in Cypher I just add on to my graph pattern. So now I want to traverse along this has topic relationship again to find articles that share similar topics. And in GraphQL, I add to my nested selection set now going from the topics to the articles and in this case, returning the title of those articles.
But what if I want something like finding the shortest path in the graph between two nodes, in this case, the National Park Service and the FAA? Well, Cypher has shortest path functionality and variable length path functionality built into Cypher so I can say, find the shortest path connecting these two organizations. In this case, following any relationships. That's what this asterisk in brackets there, it's saying sort of follow any number of relationships to find the shortest path. And I can find it through a couple of articles about labor shortages that both of these organizations are facing. This functionality isn't really built into GraphQL, so GraphQL doesn't have a sort of a native way to express this idea of a shortest path, although we could certainly implement this functionality and expose it through certain fields in GraphQL. But it's not something built in.
What about recommended articles? So a lot of news sites, as I'm reading an article, they show me something like, here are other articles you may be interested in. This sort of thing. Well, in Cypher, there are lots of ways to express those sorts of things. I could look at other articles that similar users are viewing.
Comments