Implementing Filesystem like Graph System. How to fully get back tree without writing a ton of graphql code?

Hello!

Here is my example schema:

enum NodeType {
  Folder
  File
  Root
}

type FSNode {
  uuid: String! @id
  name: String! @search
  permissions: Int!
  kind: NodeType!
  document: String # stringified blog if file
  contents: [FSNode!] @hasInverse(field: parent)
  parent: FSNode
}

Let’s say my data initial FSNode structure looked like this:

/
/hello.txt
/hiya.brb
/yellow/123.txt
/yellow/blue/green.md
/colors/yellow.md

How would I query the full tree with minimal graphql?
This is what I have for a query right now that is too long:

query {
  queryFSNode @cascade {
    uuid
    name
    kind
    contents {
      uuid
      name
      kind
         contents {
             name
             uuid
          }
     }
  }
}

Hi Jake,

In GraphQL you do have to add the recursive fields to the desired depth in the query text itself.

DQL is loosely an extension/modification of GraphQL and has more flexibility, including an @recurse directive.

I think there may be a way to add a DQL query for the recursive retrieval, and then call that from GraphQL by a special property defined by @custom(dql ), but others can confirm that will work.

So typically you define your GraphQL schema, and then the underlying DQL types and fields are auto created. You will see DQL type FSNode, and it will have type+field properties such as FSNode.uuid, FSNode.name, etc. automatically. You can query those with DQL for advanced cases where GraphQL is not flexible enough.

Be careful using @cascade in this case, because if any fields are empty, that sub-tree will be pruned. It may not be what you want here.

Also, fragments can make the graphql query less verbose.

If you want the response back with a GraphQL query, then you will always have to explicitly list each depth, so no, that will not worthrough GraphQL, but will work directly in DQL.

Yes, but not recursively, this will only help for the fields that repeat in each type.

If you want to see the open issue for adding recursion to fragments to the GraphQL spec, then check out:

Once that ^ gets approved, then Dgraph might be able to implement it, but until then, the spec is the law here.

1 Like

Thanks for the help everyone! I appreciate all of your feedback in a very fast time. You guys rock!