Queries: Tracking recurse node depth OR shortest path to a node of a set of other nodes

Use Case Description
I have a hierarchical linked list of content (so each node has either one parent or one previous node). Each node may have a settings node associated with it.

If a node doesn’t have it’s own settings specified, it should inherit the settings of the nearest parent node that does have settings.

Given a target node, I want to find the nearest settings of a parent node.

So for example:

I think there’s two functionalities that might help me solve this, but I haven’t been able to figure out a way to do it in dgraph yet:

1. Given a target content node, recurse through an arbitrary number of parents and track the number of traversals for each node

Ideally something like:

q(func:uid(ROOT)) @recurse {
  depth as math(1)
  parentNode {
    distance: val(depth)

would yield something like

  q: [
      uid: "0x1",
      parentNode: {
        uid: "0x2",
        distance: 2,
        parentNode: {
          uid: "0x3",
          distance: 3

If I was able to do this, I could make another query block filtering for blocks that have a settings node, and then the block with the minimum depth.

But this won’t work because “recurse” can only be used with one level of predicates.

So I was thinking I could also try…

2. Given a set of nodes and one target node, find which one has the shortest distance to the target

I can create a query that recurses through all parents, and get all the settings nodes associated with them easily enough. So if I had that list of settings nodes, is there a query I could write to return “Out of the nodes in this variable, this one has the fewest traversals to your target node”?

Open to other approaches… Thanks in advance for any help pointing me in the right direction!