construct the tree from the query result
fix wrong child hierarchy in result tree
convert the tree to d3 format before returning to the client