Query In Indefinite Depth

If you have a tree structure and want to query in an indefinite depth, it will be tricky. There is no way to navigate recursively without knowing the maximum depth.

Take the query of a category tree as example:

{

category(slug: "assembler") {

name

slug

category {

name

slug

category {

name

slug

category {

name

slug

}

}

}

}

}

Note that you need to specify each level you want to retrieve.

This query results in:

{

"data": {

"category": {

"name": "Assembler",

"slug": "assembler",

"category": {

"name": "Lisp",

"slug": "assembler/lisp",

"category": {

"name": "Samlltalk 80",

"slug": "assembler/lisp/samlltalk-80",

"category": {

"name": "C++",

"slug": "assembler/lisp/samlltalk-80/c"

}

}

}

}

}

}

You can improve the query a bit using fragments:

{

category(slug: "assembler") {

...CategoryFields

category {

...CategoryFields

category {

...CategoryFields

category {

...CategoryFields

}

}

}

}

} fragment CategoryFields on CategoryType {

name

slug

}

In this issue Lee Byron explains the reasons why GraphQL does not have recursive queries:

The issue is that GraphQL has no way of actually confirming that the data it encounters is not infinitely recursive. Without an ability to make this guarantee, allowing for cyclic queries could in fact expose infinite execution queries which could be used as an attack vector.

A way to solve this is paginate using the last resource reached as a parameter to query the next page. It's more predictable, but more complex.