hey folks welcome to another sub query video where i'm going to take a look at reverse lookups
keen to know more oh stick around and i'll show you how it works
let's jump into our documentation first of all where on the left hand side i'll
click on create a project and we'll jump into the graphql schema section
now here on the right hand side i'm going to jump into reverse lookups
now in a previous video i looked at one-to-many relationships where i could a query for an entity
object such as transfer and access the various fields but what a reverse lookup allows us to do
is to create a virtual field on an entity that can be queried what this means is that in the
example you see here the transfer from field here can be made available in my account entity object
here it's called syntransfer and it's linked directly to the from field here now the advantage
of this is that i can access information such as transfer dot from within another object or entity
in this case the account entity so still a little confused let's jump into the code and i'll give
you an actual example now what i'll do here is instead of traditionally using the starter project
i'm going to do a git clone from an existing project called tutorials account transfers this
was demonstrated in a previous lesson so if you've gone through that great if not highly recommend
checking this out otherwise of course you can just do a git clone and start from here what i want to
do first is get this project up and running just to make sure that there are no problems
so now i've got my node running in the background and i've got my playground
up and running in the foreground i'm going to copy just a standard a query from the workbook paste
it in here and you can see i've got some blocks returning so all is looking good now note that
we're in the accounts entity object and at the moment i don't have access to the likes of amount
block number or two so this is where the derived function or the reverse lookup can be very useful
what i'm going to do is jump into my schema file and i'm going to create a new field such as my to
address i'll make this of type transfer and i'm going to use as mentioned the keyword at derived
like so and i'll specify the field
which is going to link to the to field in my transfer entity object let me stop
the node first of all i'll save this file and let's recompile everything
now i can go ahead and do docker compose up but it actually won't work because if i look at the
documentation looking at the accounts nodes you'll see here i've actually still got my id
and i want to see my two address here so if i go ahead and run docker compose up nothing will
change that's because i actually need to re-pull the docker image and if you're not using docker
but you've got your own local node you'll have to point to a new database so let me re-pull this
start the node again
and here you can see after doing a refresh the my to address has appeared within the account
object so what this means is i can then go inside my nodes
i can then go my to address and if i actually try and run this i should get an
error because the my to address needs some subfields so let's provide this
nodes and then it's at this point where i can go ahead and say
amount for instance so i've got access to the fields inside my transfer entity
so this could be amount this could be a block number and then my 2 this will be my 2 id actually
hit run and now i've got my accounts object where i've got access to the
fields within another entity using a reverse lookup because if i didn't have this reverse
lookup here then i wouldn't be able to run this particular query starting from the accounts entity
and also extracting fields from the transfer entity so i'm able to make one query
and grab all the information i need taking advantage of just one single call using graphql
as opposed to multiple calls in the likes of using rest apis
so there you have it just a quick recap
you can go ahead and add a reverse lookup on any field you want inside your schema graphql file
you use the at derive from keyword on the field that you want to create and then
you can query it from graphql hope that was useful and i'll catch you on the next video
you