Is it possible to sort nodes by derived value?

I have a bunch of nodes, and in the client I have a value called “status” which is derived from whether or not the nodes have certain predicates. It’s possible the nodes don’t have preceding stage predicates.

Imagine this set of data:

nodes: [{
   stage1: true
},
{
   stage1: true,
   stage2: true
},
{
   stage1: true,
   stage2: true,
   stage3: true
},
{
  stage2: true
},
{
  stage1: true,
  stage3: true
}
]

Now let’s say status is “closed” if a node has stage3, “active” if has stage2, and “ready” when it has stage1. Is there a way to query this data and sort by status like this?

Obviously I could just go in and manually set the ‘status’ value for each node, but I’m curious if there’s a way to achieve it using query variables.

How about such a query:

CLOSED as closed(func: Type(Node)) @filter(has(stage3))
ACTIVE as active(func: Type(Node)) @filter(has(stage2) AND NOT uid(CLOSED))
READY as ready(func: Type(Node)) @filter(has(stage1) AND NOT (uid(CLOSED) OR uid(ACTIVE)))

Hey ppp225, I like that for its simplicity and was playing around with something similar. My question though is how to then order all of the nodes by that status? So from ready to closed or vice versa?

{
	nodes(func: Type(Node), orderasc:stage3, orderasc:stage2, orderasc:stage1) {
		stage1
		stage2
		stage3
	}
}

The above should work, somehow.

But I think you’re asking, how to create a derived/temporary string value, (and then use it to filter things). I don’t think it’s possible currently, as I was trying to do something similar. You could maybe use math variables to hack around. Or just handle this in the backend, as I have done.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.