How to recurse recursive type defined with GraphQL schema?

I have this GraphQL schema:

enum Status {
    OK
    FAIL
}

interface Node {
    id: ID!
    name: String! @id @search(by: [term])
    status: Status! @search
}
type Library implements Node {
    version: String!
    repo: String!
    nodes: [Library]
}

I need to find all Library with status FAIL and then all connected parent Library.

In DQL I could do this:

{
  Library(func: eq(status, "FAIL")) @recurse(loop: true, depth: 5) {
    name
    ~nodes
  }
}

How to do the same with GraphQL schema?

GraphQL doesn’t have @recurse.

In GraphQL, the principle is:

You must explicitly ask what you want to get in the results.

So, you will have to send a query that is 5 level deep:

query {
  queryLibrary(filter: {status: {eq: FAIL}}) {
    name
    nodes {
      name
      nodes {
        name
        ... # as deep as you want to go
      }
    }
  }
}

EDIT:
If you want, you can specify filters for nodes too like this:

query {
  queryLibrary(filter: {status: {eq: FAIL}}) {
    name
    nodes(filter: {status: {eq: FAIL}}) {
      name
      nodes(filter: {status: {eq: FAIL}}) {
        name
        ... # as deep as you want to go
      }
    }
  }
}

The thing is that I don’t know how deep it can be

Yeah! I understand the pain.

But, GraphQL is all about a fixed structure, so it is not possible to @recurse in GraphQL. You have to specify the nested levels you want to query.

If you don’t know how deep it can be, then I guess DQL is the only option you have got.

EDIT:
Here is what the GraphQL spec has to say on cycles in a query: GraphQL

1 Like