Need some help with a recursive query

Hi, I have been enjoying working with Dgraph and I think this would be an ideal database for my project TNB Explorer which is a blockchain explorer. We have a feature where we want to track the source of transactions and we want to visualize it like in the screenshot below.

We implemented it using recursive query in postgres SQL which is quite expensive to perform. Finally the feature would look something like this Trace transactions | TNB Explorer

So, I started migrating to Dgraph!
My schema:

type Block {
  	balanceKey: String! @id
  	createdDate: DateTime!
  	sender: String! @search(by:["exact"])
  	signature: String!
  	transactions: [Transaction] @hasInverse(field: block)
}

type Transaction {
  	id: String! @id
    amount: Int!
    recipient: String! @search(by:["exact"])
  	fee: String
  	memo: String
  	block: Block!
}

Mutation query (thousands of such records):

mutation{
    addTransaction(input: [
    {
        id: "f36d1813-59d0-4842-a335-f307baa3e58b",
        amount: 1,
        recipient: "649da496fc8dfe070eaa64e279258485f696333fbcf8b4a1223cd6d9388d0088",
        block:{
            createdDate: "2021-03-09T16:19:40.239813Z",
            balanceKey: "de50471ab2c3affb9069f246ba126082a9022c9bc208d46d8228a18ebabef15d",
            sender: "2c83d639e20b8937edee50fd2b5286c1651012fc5cd4825e7f156d5a88b4e822",
            signature: "ef6a19f2aea1427b88acf83edf10c499c1f76d428ca0294fc65d5c048eb2a4af91bd633d1b30ddd58ea6df92173e2766a5b4ed253d401befc10d9b6e46dcdd08"}
        }
    ])
    {
        numUids
    }
}

Now that you have all the context, I need to build a query on this data which takes in an recipient id and spits out edges and vertices which are used by charting library to visualize the information.

const edges = [{
	source: "23676c35fce177aef2412e3ab12d22bf521ed423c6f55b8922c336500a1a27c5",
	target: "f0fe0fdff41db888a0938882502ee809f6874c015aa09e11e38c8452d4175535",
	label:	"15902786"
},
{
	source: "29f492ff241c43afd78027822151ea52a5409cbd684d75bde8973f5a465a0d25",
	target: "23676c35fce177aef2412e3ab12d22bf521ed423c6f55b8922c336500a1a27c5",
	label:	"72800"
},
...
];

const vertices = [{
	id: "23676c35fce177aef2412e3ab12d22bf521ed423c6f55b8922c336500a1a27c5",
	label: "23676c35..."
},
{
	id: "29f492ff241c43afd78027822151ea52a5409cbd684d75bde8973f5a465a0d25",
	label: "29f492ff..."
},
...
];

I am struggling to craft the ideal query that could yield me data in such format.

{
  traceTransactions(func: eq(Transaction.recipient, "e9c5acac0806aca6ba2c0ade74d93ec4f9a89d8743fa477c52ce9b7817dcad95")) 
  {
    Transaction.amount
    Transaction.recipient
    Transaction.block {
			Block.sender
      Block.transactions {
				Transaction.amount
        Transaction.recipient
      }
    }
  }
}

In each recursive query, senders will become the recipient and the cycle will continue to a depth that I can control.
I tried to share as much information I could to make my question clear but if you want more information please feel free to ask in comments. If you have experience in dgraph please help me with such an operation, it would be really helpful. Also, if you think such query would require different schema then do not hesitate in suggesting that approach.

I would be really looking forward to find a solution to my query

1 Like