Brainstorming Ideas to handle nested nodes and compare nested nodes

schema
graphql

#1

Hi All,

Greetings! I have been playing around with CayleyDB and developed the following generic snippet that plots the nested nodes.

function plot_family(node_one) {
childPredicates = g.V(node_one).OutPredicates().ToArray()
for(var index=0; index < childPredicates.length; index++){
  valueForChildPredicate = g.V(node_one).Out(childPredicates[index]).ToArray()
  for(var j=0; j < valueForChildPredicate.length; j++){
    currValueForChildPredicate = valueForChildPredicate[j]
    grandChildPredicates = g.V(currValueForChildPredicate).OutPredicates().ToArray()
    currDict = {"source": node_one, "target": currValueForChildPredicate}
    g.Emit(currDict)
    if(grandChildPredicates.length != 0){
      plot_family(currValueForChildPredicate)
    }
  }
}

}

node_one = "alice"
plot_family(node_one)

I would like to know whether any pre-built method exists in Cayley which does it automatically? Also to compare two nodes with nested nodes (i.e., Aggregate relationships where an object contains another Object as its variable) what would be best way in cayley

Thanks. With Regards,


#2

Hi @DroidPoe,

If you know a list of predicates/schema upfront, our GraphQL implementation will help you with loading nested object. Unlimited nesting is only exposed via Go lib for now.

We can also compare two nodes very efficiently, but again, this can be done only on Go side currently.


#3

Hi @dennwc,

Thank you. Can you please direct me to any examples to do that in go lang or any specific APIs that help to do this


#4

Regarding loading, the package you need is called schema, and the function for loading objects recursively is called schema.LoadTo.

There are few examples in tests and an example of a full program.

So it should be pretty straightforward to modify example app and replace type definition with any tree-like structure to get fist results. Please keep in mind that it does not support loops in graph.

Regarding nodes comparison, you can use a low-level function called QuadIterator which is available on QuadStore object (the database). It works like this: you can specify a “direction” of a quad (subject or object, for example) and a value on this direction to filter. In your case it will be something like this:

it := qs.QuadIterator(quad.Subject, qs.ValueOf("<your-node-id>"))

It will iterate over all predicate-object pairs that start on a given node. It’s similar to how Out works, but it returns full quads instead of object nodes.

Having these iterators for two objects you can compare if their predicates have same objects or not. Iterators are sorted, thus you can advance them one by one and compare values without loading everything into memory.


How to translate this simple DGraph schema?
#5

Hi @dennwc

Thank you very much for the detailed inputs.

I will try to work on it and update the results.

Thank you