Return all the nodes a graph as a list using the golang client

type:

type IdNode {
  idval: string 
  idtype: string
  ts: int 
  conn: [IdNode] 
}
idval: string @index(exact) .

data:

{
	set {
		_:uuid1 <idval> "uuid1" .
		_:uuid1 <idval> "uuid" .
		_:uuid1 <dgraph.type> "IdNode" .
		_:uuid1 <ts> "12313223211" .
		_:uuid1 <conn> _:gm1 .

		_:gm1 <idval> "mygmail.com" .
		_:gm1 <idtype> "gmail" .
		_:gm1 <dgraph.type> "IdNode" .
		_:gm1 <ts> "12313223213" .

		_:uuid2 <idval> "uuid2" .
		_:uuid2 <idtype> "uuid" .
		_:uuid2 <dgraph.type> "IdNode" .
		_:uuid2 <ts> "12313223212" .
		_:uuid2 <conn> _:gm1 .

		_:uuid3 <idval> "uuid3" .
		_:uuid3 <idtype> "uuid" .
		_:uuid3 <dgraph.type> "IdNode" .
		_:uuid3 <ts> "12313223212" .
		_:uuid3 <conn> _:uuid2 .

		_:eid <idval> "eid" .
		_:eid <idtype> "eid" .
		_:eid <dgraph.type> "IdNode" .
		_:eid <ts> "12313223212" .
		_:eid <conn> _:uuid1 .

		_:fb <idval> "fb.com" .
		_:fb <idtype> "fb" .
		_:fb <dgraph.type> "IdNode" .
		_:fb <ts> "12313223212" .
		_:fb <conn> _:uuid1 .
	}
}

I want to return a list of nodes from any other node in the graph, the relationship doesn’t matter here.
query:

{
  find_many(func: uid("0x2719")) @recurse{
    uid
    idval
    idtype
    ts
    conn 
    ~conn
  }
}

But the above query returns a nested structure. Is there any so that it will return a flattened structure like below. I know this is possible through a graph traversal logic, but just wondering is there builtin function provided by dgraph or the golang client to flatten the result.

[
     {  "uid": "0x2717",
       "idval": "uuid1",
        "idtype": "uuid",
        "ts": 12313223211
},{..}
]

If all nodes are of same type, then you should use type query instead of recursing through the nodes.

{
  q(func: type(IdNode)) {
    uid
    name
  }
}

but there can be many graphs in the system. I want to extract all the nodes of a particular graph.

To “Flat” things we use normalize. But, for your case, this would messup with everything. There’s no function in Dgraph that returns a flat line of objects. What you can do is use variables and multiple blocks to have the same result.

e.g:

PS. “idval” is a list. So it will be always like "idval": ["uuid2"]
and not "idval": "uuid2"

{
  var(func: eq(dgraph.type, "IdNode"), first:1) @recurse {
    VAR0 as uid #This will get all UIDs recursively
    idval
    idtype
    ts
    conn
    ~conn
  }
  
  flatObjects(func: uid(VAR0)){
    uid
    idval
    idtype
    ts
  }
}

Result

{
  "data": {
    "flatObjects": [
      {
        "uid": "0x4e42",
        "idval": [
          "mygmail.com"
        ],
        "idtype": "gmail",
        "ts": "12313223213"
      },
      {
        "uid": "0x4e43",
        "idval": [
          "uuid2"
        ],
        "idtype": "uuid",
        "ts": "12313223212"
      },
      {
        "uid": "0x4e44",
        "idval": [
          "uuid3"
        ],
        "idtype": "uuid",
        "ts": "12313223212"
      },
      {
        "uid": "0x4e45",
        "idval": [
          "eid"
        ],
        "idtype": "eid",
        "ts": "12313223212"
      },
      {
        "uid": "0x4e46",
        "idval": [
          "fb.com"
        ],
        "idtype": "fb",
        "ts": "12313223212"
      },
      {
        "uid": "0x4e47",
        "idval": [
          "uuid",
          "uuid1"
        ],
        "ts": "12313223211"
      }
    ]
  }
}
1 Like

That’s a neat trick!

1 Like