hey folks welcome to another video on sub query
where we're going to take a closer look at graphql schemas jumping into the documentation here
and navigating on the left hand side to create a project graphql schema
you'll see that the schema file essentially dictates the shape of your data from sub query
what this really means is that you create a schema file to define the type of data
you want to extract from the blockchain you can think of it as your interface definition language
because it sits between your web application or your decentralized application or dap
and the blockchain and if you're not familiar with graphql
i highly recommend you jumping into graphql.org forward slash learn to get an introduction
into graphql but essentially the file is created by defining what we call entities
you can think of entities as objects and these objects contain fields such as name and address
and these fields have a type so in this case the name is a string an address is also a string
you'll see the supported scalars and types down below and just to note that if you see
an exclamation mark here that this indicates that it's non-nullable in other words it's a
mandatory or compulsory field you can also go ahead and add an at index keyword or annotation
to improve the query performance what this does is it has an index on the entity field
so let's go ahead and jump into a sub query project to see how it works
let's start off by creating the standard sub query project
so just running the standard sub ql init command and then the standard yarn install
cogen and build something that we should all be familiar with now
now just before I run the code gin let's open up the schema file first and you'll notice that we've
got the name of the object called starter entity this could be anything really let's go my object
for instance and in here we've got the mandatory id field this is required and then we have a range
of fields called one two three four five now the reason why we have five fields is
if we go back into the documentation you'll see that the current scalar types
supported are id and string begin date and boolean so all of these are demonstrated
in the actual starter project we'll cover the last two in a future video so again I can name these
whatever I want usually something more meaningful like maybe block number this could be say account
this may be valuable for example I'll call this account 2 this could be a time stamp
really anything you want this could be maybe success let's save this next up we can run
yarn cogen and what this does is it generates code out of your graphql schema
so on the left-hand side under source you'll see a new file appear so let's run yarn code in
and we've got a types folder and under here I've got something called object.typescript
if i open this you can see this is auto-generated so do not edit this and you can see the
various fields here that have been created and some automatic functions as well
save get and create record now just a note when you make changes to the schema file
you need to regenerate your type directory by running code gen so that's saying that if you
ever do change the shape of your data you have to run code gen because this file needs to be updated
now remember i said that you can add an index when you do so for example if i go at
index here let's save this when i regenerate this this is going to automatically create a function
so if i go back to my object file you'll see that i'm going to get a new function appear here
so remember i've got 53 lines let's go and run cogen and then you can see here i've got
a new function called get by value and the reason why it's a value is because this is the name
of my field allowing you to access it directly in the mapping function as well
so that's what happens when you add the index annotation to a field many people often ask how
do i know how to define the shape of my data so this is where you need to take a step back and do
some planning and understand the data that you can extract and also the data that you want to display
now please note that this is a very simple example
with one object type because we can increase the complexity by adding more objects in here
and we can also have a relationship between these objects as well
one further way to understand schemas more is to jump into the explorer project and we'll
take a look at some examples here this is a great benefit of having an open source explorer where we
can analyze some existing projects such as maybe sub scan multi-sig let's go and do bit frost maybe
and in this project let's jump into the Github
and we'll dive right into the schema. graphql file and here you can see we've got a block entity or
object containing number timestamp parent hash spec version we'll ignore these ones down here
because these are looking at relationships to other objects and we'll explore these in a future
video we've got an extrinsic object down below again containing a lot of the fields that you
can actually extract from a substrate blockchain and scrolling down we've got events accounts we've
even got tokens and transfer as well if i take a look at another one here such as bit frost let's
open this one jumping into the schema graphql file again we've got the default starter entity other
objects are also defined such as asset token where we're pulling out symbol precision token supply
transaction by day transactions also note that you can comment out using the pound
symbol and then we've got market price day data mint price so lots of different objects
in your schema file so there we have it a basic introduction into graphql schemas
look out for a future video where we'll jump into entity relationships as well catch you next time
you