Returning an N-depth sequence query via GraphQL Dgraph Cloud like you do in graph db query languages

It’s my understanding that a very big benefit of a graph database vs other databases is the ease and speed in querying deeply related data and that their non-SQL query languages often make that easy (I think?).

“Show me the sequence from node A through any number of nodes to node Z.” Or “Show me the sequence via this deeply nested hierarchical set of nodes.”

And I assume Dgraph’s DQL may be good at doing these types of queries, yes?

But on the GraphQL side of things, how is this done?

GraphQL is a strongly typed API language. And because of this, it is impossible to get any nested edges you don’t specifically ask for. By specification, the response can ONLY be in the same (or less) shape of the query. This is why things like @normalize and expand(<type of "_all_">) cannot be achieved in GraphQL.

I have seen one other GraphQL healthcare specification try to break this by putting in their spec something like the @normalize directive, but when I questioned them about why they were doing this and breaking the official GraphQL specification, the response was that they just provide the implementation ideas, but not the actual implementation, so while they say something like that should be possible, they were not responsible if by implementing it would break the spec or not or even if it would work with other GraphQL tools like Apollo. They were only concerned about adding the directive idea, and not actually what it meant in the implementation. (That is healthcare for you)

The only way it could be done within specification, is if it was done in a way that the query matched the response. A possible solution would be to create new queries that perform advanced algorithms under the hood

query {
  shortestPath(from:["0x2"], to: ["0x3"]) {
    from # uid
    to # uid
    path # path represented as string eg: director.directed.songs.artist
    data # JSON string of data with nested path eg: `{"movie":{"id":"0x2","director":{"directed":[{"songs":[{ "artist":{"id":"0x3"}}]}]}}}`
  }
}

Thanks a lot @amaster507.

So regarding creating a custom query in the world of Dgraph Cloud/Slash, I see this example that mentions a resolver that uses an external (Twitter) api. https://dgraph.io/docs/graphql/custom/query

But to create a custom GraphQL query that queries Dgraph DB data, I assume that would work something like this?:

Custom “getShortestPath” GraphQL query → custom DQL query → returns JSON, yeah?

Are there docs for this kind of thing (if it’s true?). I havent found it, if so.

Update: Whoops! I see this here: https://dgraph.io/docs/graphql/custom/dql/

1 Like

Thanks again @amaster507 Re: a case-in-point for your thread about how GraphQL + DQL can play together, I’m still searching for good documentation for the correct syntax to build out that ShortestPath query you sketched out above (and the Recursive query approach as well) when using a GraphQL schema (and not using the DQL schema that I think the official docs are assuming.)

And unlike the world of GraphQL, I’m getting errors in the DQL ratel that do not give details of the error so it’s not the easiest to work through.

Hmm, maybe there is something else going on with my DQl ratel: the simplest queries are returning errors with no details.

OK, so some of the issues were that my ratel lost connection to the (GraphQL-created) schema.

And, oddly, clicking around the sections of the Dgraph Cloud admin eventually, mysteriously, resulted in the ratel connecting to the schema.

Am I understanding this correctly? Extending GraphQL to use a DQL query can only return one node type (based on the limitation of GraphQL?)

A single DQL “shortest path for our journey” query returns some unknown depth of various types: Restaurant nodes, GasStation nodes, RoadsideAttractions nodes, RestArea nodes… for exmple.

But the 2 examples that I’ve been able to uncover anywhere on the web seem to suggest that DQL queries can only map to a single GraphQL type /node, yes?

So GraphQL/Dgraph Cloud would not be able to return a DQL-created JSON of multiple, nested node types? Or maybe I’m wrong here?

I wonder if it might be possible if you returned an interface and all of those types implement that interface. Like the way relay does it with the node interface and can be seen here:

Thanks @amaster. I’m now trying some different approaches (to this seemingly common need) to return nested nodes as a sequence - and seeing if non-exotic queries can solve it.

So to test, I changed requirement to abide by restrictions and only return 1 Node Type. (Ex: collapse all those Restaurant nodes, RestArea nodes etc into a common “Waypoint” nodes.)

And trying a Recurse DQL query now, it is not clear to me how to build a nested sequence when I get errors of “recurse queries require that all predicates are specified in one level.”

How then would one get a nested sequence from:

Node w predicate X pointing to → Node w predicate X pointing to → Node w predicate X pointing to → Node?

Ex: Waypoint node with NextStop pointing to → Waypoint node with NextStop pointing to → Waypoint node

I’d imagine a Shortest Path query perhaps could do it IF you knew both the starting Waypoint and chose an ending Waypoint (among many)… But if the requirement changes to “let’s map out all the various paths” ie, “predicate X can return an array of Nodes / AvailableNextStops can be an array of possible next stops”, then I would image Recurse is the only way to achieve that query - to build a such a nested map?

But it’s not clear how to create a nested recurse query with the “one level” limitation?