►
Description
In this video, you'll learn how to build an NFT subgraph and API for data from a smart contract deployed to NEAR protocol.
Workshop:
https://github.com/dabit3/near-subgraph-workshop
Twitter: https://twitter.com/graphprotocol?s=20
Instagram: https://instagram.com/graphprotocol
LinkedIn: https://www.linkedin.com/company/theg...
Website: https://thegraph.com
A
All
right
so
in
this
video
I'm
going
to
be
walking
through
a
workshop
of
how
to
build
an
nft
subgraph
on
top
of
near
protocol
and
the
workshop
that
I'm
going
to
be
going
through
is
open
sourced
here
in
my
github,
so
you
can
go
to
github.com
dabit3
and
you
should
see
the
near
subgraph
workshop.
So
if
you
just
go
to
my
github
profile
and
go
to
my
repositories,
this
should
show
up
or
you
can
search
for
it
here
and
we'll
start
going
from
here.
A
So
essentially,
everything
that
you
can
do
here
should
be
able
to
be
just
walked
through.
So
everything
that
I
have
here
should
be
copy
and
paste.
I
tried
to
explain
a
lot
of
this
stuff
here
and
I'm
gonna
also
walk
through
in
this
video
so
to
get
started.
The
two
things
that
we
need
to
do
is
install
the
graph
cli
using
either
npm
or
yarn,
and
we
also
need
to
go
to
the
graph
dashboard
and
go
ahead
and
create
a
sub
graph
for
us
to
deploy
to
so
to
do
that.
A
A
So
what
we
want
to
do
is
click
create
or
add
sub
graph,
and
then
here
we
can
go
ahead
and
define
the
subgraph
name
and
details
that
we'd
like
to
work
with.
Now
you
can
create
a
subgraph
for
any
application
or
any
contract
or
program.
That's
been
deployed
to
near
the
one
that
I'm
going
to
be
working
with
is
the
misfits
nft
contract.
A
This
is
just
one
of
the
first
nft
projects
that
I
came
across
while
googling
around,
so
I
figured
I'll
play
around
with
it
and
it
ultimately
ended
up
what
I
built
this
workshop
off
of.
You
could
also
probably
do
this
with
any
other
nft
project.
That's
out
there.
The
metadata
and
stuff
might
be
a
little
bit
different,
but
everything
else
should
be
the
same.
A
So
using
this
this
address,
we
can
go
to
the
near
explorer,
and
here
we
have
some
of
the
information
that
we're
going
to
end
up
using,
for
instance,
we
have
the
account
name.
We
have
the
transaction
where
it
was
created
which
will
give
us
the
start
block
and
things
like
that
which
we'll
be
using
in
just
a
moment.
A
A
So
once
this
is
created,
we
can
now
use
our
access
token
and
the
graph
cli
to
go
ahead
and
build
this
thing
out.
So
I'm
going
to
go
back
to
the
workshop
and
I'm
going
to
go
down
here
where
it
says:
initialize
a
new
subgraph
using
the
graph
cli
and
I'm
going
to
copy
this
command
and
go
here
and
just
paste.
It
in
npm
install
dash
g
at
graph
protocol
graph
cli.
I
already
have
this
installed
on
my
computer
I'll.
A
Some
different
commands,
like
dash
dash
from
contract,
will
allow
us
to
go
ahead
and
just
define
the
contract
address
and
then
the
contract
name
we're
going
to
be
indexing
tokens
so
we'll
just
call
the
contract
token.
You
know
nfts
non-fundable
tokens
and
then
we're
also
going
to
be
indexing.
The
user
that
owns
that
token
and
we're
going
to
have
relationships
between
the
token
and
the
user.
So
the
protocol
is
near
the
subgraph
name
will
be
your
username
slash
the
subgraph
name
in
the
dashboard.
A
A
Contract
account
is
what
we
passed
in
into
the
arguments.
The
contract
name
is
also
what
we
pass
into
the
arguments
and
with
that
being
said,
we
should
go
ahead
and
have
everything
we
need
to
kind
of
get
going.
So
I'm
gonna
go
back
to
the
workshop
and
I'm
going
to
scroll
down
to
this
part
where
we
define
our
graphql
schema
and
then
once
this
is
done,
initializing
I'll
just
go
ahead
and
open
this
up
in
my
text,
editor.
A
And
here
is
our
boilerplate
subgraph
that
we're
going
to
start
working
with.
So
what
we
have
is
three
main
parts:
we
have
our
graphql
schema,
which
is
our
data
model,
so
this
is
kind
of
like
us
defining
the
data
that
we'd
like
to
have
saved.
So
in
the
case
of
an
nft,
you
might
save
stuff
like
the
metadata,
the
id,
the
collection
name
or
whatever
else
that
you
have
available
to
you,
based
on
how
that
contract
was
written.
A
We
also
have
the
subgraph.yaml,
and
this
is
our
subgraph
configuration
so
if
you've
ever
done
anything
with
infrastructure
as
code
or
just
any
type
of
configuration
using
yaml
you've.
Probably
you
have
a
good
understanding
of
kind
of
how
you
might
define
a
yaml
file
and
a
subgraph.
We
just
have
different
flags
for
things
that
we'd
like
to
kind
of
allow
the
indexer
to
know
about
the
subgraph,
and
then
they
will
essentially
look
at
this
information
and
kind
of
do
different
things
based
on
what
we've
defined
here.
A
So
we'll
walk
through
that
in
just
a
moment
and
then
the
last
thing
is
our
business
logic,
so
the
code
that
we're
actually
going
to
write
is
going
to
live
in
this
mapping.ts.
A
So
this
is
assemblyscript,
but
it's
very
much
like
typescript
or
or
even
yeah.
I
would
say
typescript
would
be
the
closest
thing.
So
if
you're
ever
in
javascript
or
typescript
assembly,
script
is
not
too
far
off
from
there.
It
has
its
own
quirks,
but
we're
going
to
start
just
by
deleting
that
and
we're
going
to
be
writing
our
own
in
just
a
moment.
So
the
first
thing
that
we'll
do
is
we'll
go
to
our
graphql
schema
and
we
can
just
copy
this
schema
and
I'm
gonna.
A
I
will
walk
through
it
just
to
kind
of
give
you
an
idea
what's
going
on
here,
so
I
will
first
start
with
these
two
types
down
here
and
then
I'll
get
to
this
full
text
search
modifier
there
or
that
directive
there.
So
the
two
pieces
of
data
that
we
would
like
to
be
able
to
query
for
are
tokens
and
users.
A
token
will
have
its
own
set
of
metadata.
A
So
we're
saying
that
a
token
should
have
an
id.
It
should
have
an
owner
that
is
relation
to
this
user,
that
we're
going
to
also
create
we're
going
to
have
an
owner
id,
which
is
just
going
to
be
the
account
that
owns
that
token,
we're
going
to
have
a
token
id
which
is
going
to
be
the
unique
id.
A
For
that
token,
obviously
we're
going
to
have
the
image,
so
this
will
be
the
ipfs
image
for
the
nft,
we're
going
to
have
the
metadata
and
we're
going
to
have
the
kind
which
is
a
piece
of
metadata.
So
a
token
is
going
to
have
a
metadata
and
that
metadata
object
is
going
to
be
different
based
on
the
nft
project.
So
in
this
project
in
particular,
you
have
the
kind
and
the
seed.
I
don't
really
know
what
the
seed
really
means,
but
I
indexed
it
anyway,
but
the
kind
is
kind
of
like
a
rarity.
A
So
you
know
some
of
these
are
more
rare
than
others
and
it's
based
on
the
kind.
So
the
last
thing
that
we
are
indexing.
In
addition,
to
the
token,
is
this
user
and
the
user
is
just
going
to
allow
us
to
have
a
relationship,
one-to-many
relationship
between
tokens
and
users
and
users
and
tokens,
so
a
token
can
have
a
user
and
a
user
can
have
many
tokens
and
we
will
use
these
two
directives
to
kind
of
make
all
this
work.
A
We
have
this
at
entity
directive,
meaning
we're
going
to
reflect
that
we're
defining
this
entity
in
our
subgraph.yaml
and
the
cli
will
be
able
to
pick
that
up
and
allow
us
to
index
that,
and
then
we
also
have
this
at
derog
from
directive,
and
this
just
allows
us
to
create
relationships
like
in
this
case
a
one-to-many
relationship
between
token,
I'm
sorry,
users
and
tokens,
and
then
finally,
we
add
this
optional,
and
this
is
optional.
A
It's
a
full
text,
search
configuration
and
in
this
case
we're
saying
we
want
to
be
able
to
search
these
tokens.
So
in
this
array,
we're
gonna
say
include
the
entity
of
token,
and
then
we
want
to
make
these
two
fields
searchable
the
owner
id
field
as
well
as
the
kind.
So
this
will
allow
us
to
kind
of
search
for
tokens
based
on
their
rarity,
so
we'll
look
at
how
that
works
in
just
a
moment.
A
A
A
A
So
that's
just
how
we
match
up
our
entities
with
our
data
model.
The
next
thing
that
we're
going
to
do
is
we're
going
to
add
the
start
block,
and
this
will
allow
us
to
kind
of
know
where
we
need
to
start
indexing.
So
this
way
we
don't
have
to
start
from
the
very
beginning
of
the
chain.
We
can
start
at
this
block
and
then
it
will
just
make
it
a
lot
faster
and
you
can
find
this
start
block
on
any
contract
just
by
looking
at
where
the
contract
was
deployed.
A
Next
we're
going
to
enable
a
couple
of
features,
so
I
can
add
these
feature
flags
so
below
the
schema
definition.
I'll,
add
features.
A
One
feature
that
we
want
to
add
is
full
text
search,
which
is
something
we
set
up
here,
and
we
also
want
to
add
this
feature
called
ipfs
on
ethereum
contracts,
meaning
that
we
want
to
be
able
to
essentially-
and
maybe
that
naming
with
ethereum
shouldn't
be
there
since
this
is
near,
but
maybe
we'll
update
that,
but
essentially
what
we
want
to
have
access
to
is
the
ipfs
api.
A
So
when
we're
in
our
mappings,
where
we're
writing
our
business
logic,
we're
going
to
be
mainly
interacting
with
a
couple
of
different
data
sources
or
apis,
you
could
say
so
the
mappings
again.
This
is
where
we
write
our
business
logic.
This
is
where
we
actually
save
information
to
the
node
and
make
it
available
for
querying.
A
A
So,
every
time
a
transaction
is
fired,
we
have
the
option
to
call
functions
and
then
we
can
handle
those
functions
in
this
mapping
and
in
the
mapping
we
can
do
stuff
like
save
to
the
node,
based
on
the
information
that
comes
in
to
the
arguments
of
that
mapping,
and
in
our
case
in
and
near
we
have
these
receipts
and
we're
going
to
basically
have
a
function
that
calls
handle
receipt.
A
So
every
time
we
get
a
receipt
from
a
transaction
we're
going
to
handle
that
in
this
mapping,
so
in
the
mapping
we're
going
to
be
essentially
calling
ipfs
to
use
the
token
id,
along
with
the
hash
of
this
ipfs
contract.
I'm
sorry
the
ipfs
address
for
where
this
nft
project
was
deployed
to
read
that
metadata
and
be
able
to
fulfill
these
two
fields
in
our
type.
So
we
need
to
get
the
kind
and
the
seed
and
the
only
way
we're
going
to
get
that
is
by
reading
from
ipfs.
So
therefore
we're
enabling
ipfs.
A
So
I
hope
that
wasn't
too
confusing
the
way.
I
just
said
that
the
next
thing
we
can
do
is
just
kind
of
like
double
check
and
make
sure
we
have
everything
set
up.
I
believe
we
do
so.
We
have
everything
ready
to
go,
and
now
we
can
do
the
last
thing
which
is
going
to
be
our
mappings,
so
the
mappings
are
going
to
be
a
lot
of
code
that
we're
going
to
write
here
and
actually
I
did
skip
a
part.
A
We
need
to
run
this
command
called
graph
cogen,
and
this
is
essentially
going
to
create
some
apis
that
we're
going
to
be
importing
right
here
for
us
to
interact
with
the
graph
node,
which
is
essentially
how
we're
going
to
be
saving
this
information
to
the
node.
So
all
I
need
to
do
is
open
the
cli
and
I
can
run
graph
cogen,
and
this
will
go
ahead
and
generate
some
code
for
us
and
if
that
runs
successfully,
you
should
be
able
to
have
these
imports.
A
So
let's
go
ahead
and
import
this
stuff
and
make
sure
that
that
looks
like
it's
working
properly
and
it
looks
like
it
is.
We
don't
have
any
errors,
so
we
are
importing
from
two
different
places:
we're
first
importing
from
the
graph
typescript
library-
and
this
just
gives
us
a
bunch
of
apis
and
types
for
us
to
use
as
we're
writing
our
business
logic.
So
this
will
have
some
types
for
nier.
A
These
are
different
apis
that
allow
us
to
do
stuff.
You
know
within
our
code
and
then
we
have
a
log
which
will
just
allow
us
to
essentially
do
a
console.log.
We
also
have
a
near
I'm
sorry.
We
have
a
token
and
a
user
api
that
allows
us
to
talk
to
the
graph
node.
So
this
will
really
be
us
just
saving
information
to
the
graph
node
or,
if
we'd
like
to
also
reading
information.
The
graph
node,
the
next
piece
of
code
that
we're
going
to
write
is
the
handler
function.
A
So
we
said
in
the
subgraph
yaml
that
we
have
this
handle
receipt,
and
this
will
fire
every
time
a
new
transaction
is
written
and
then,
when
that
function
is
called,
we
can
do
whatever
we'd
like.
A
So
what
we're
going
to
end
up
having
available
to
us
is
this
array
of
actions
and
then
we're
going
to
map-
or,
I
would
say,
loop
over
every
one
of
those
and
then
we're
going
to
call
another
function
passing
in
the
index
of
that
action
along
with
the
receipt.
A
So
here
we're
going
to
be
getting
a
couple
of
arguments,
we're
going
to
have
the
action
as
well
as
the
receipt,
and
we
will
then
be
able
to
kind
of
write
our
business
logic.
So
the
first
thing
that
we're
doing
here
is
we're
going
to
first
check
to
see
if
the
function
I'm
sorry,
the
action
is
a
function
call
and
those
are
the
only
ones
that
we're
kind
of
interested
in.
A
So
I
will
go
ahead
and
create
a
few
variables,
so
we'll
create
a
variable
setting
it
to
the
argument
here
coming
in
here,
the
receipt
with
outcome.outcome
we'll
also
get
a
reference
to
the
function,
call
name.
So
if
we
go
to
the
smart
contract
or
I'm
sorry
the
the
address
here
and
we
scroll
down,
we
can
see
that
we
have
different
method
names,
so
we
have
nft
approved,
we
have
buy.
We
have
all
this
other
stuff.
So
what
this
is
going
to
be
is
going
to
be
the
the
name
of
that
function.
A
Call
and
then,
of
course,
we
can
click
into
here
and
see
details
like
the
arguments
and
stuff
that
we're
going
to
be
also,
you
know
considering
in
just
a
moment.
A
A
A
So
these
are
the
two
different
things
that
we're
going
to
be
working
with
we'll
work
with
the
json
metadata,
as
well
as
the
image,
but
we
need
to
have
the
base
hash
in
order
to
do
that,
and
then
all
we
really
need
to
do
is
get
this
id
off
of
the
argument.
That's
coming
through
here,
so
the
token
id
and
then
using
that
combination
we
can
kind
of
get
everything
we
need.
A
So
that's
what's
going
on
so
far,
so
here
we're
you
know
getting
the
string
value
of
this
function,
call
which
is
going
to
be
how
we
determine
whether
or
not
to
call
the
next
function.
A
So
with
that
being
said,
we'll
go
back
to
the
workshop
and
now
we're
going
to
check
and
we're
only
going
to
choose
to
proceed
for
a
couple
of
different
actions
or
a
couple
different
methods.
So,
there's
a
lot
of
methods
that
will
be
called
in
this
program
or
in
this
account
in
this
contract.
Whatever
you
want
to
call
it,
and
we
can
choose
to,
you,
know,
do
stuff
based
on
which
function
and
which
data
that's
coming
in,
so
the
two
that
I'm
choosing
to
work
with
are
buy
or
meant
one
and
the
way.
A
The
reason
I
did
this
is
that
this
is
these
functions
are
going
to
be
called.
If
someone
meant
a
new
token
or
if
someone
buys
a
token
from
from
a
marketplace,
I
guess
another
way
you
might
want
to
do.
This
is
if
someone
just
transfers
one
as
well,
I
did.
The
logic
should
all
be
the
same,
but
I
left
off
that.
A
So
if
one
of
these
different
methods
are
called,
we
want
to
go
ahead
and
save
that
information
to
our
node.
So
that's
the
only
actions
that
we're
going
to
be
taking
into
consideration
only
methods.
A
So
what
we're
going
to
now
do
is
we're
going
to
have
this
array
of
logs
that
are
part
of
this
function,
call
and
we're
going
to
map
or
loop
over
those
and
using
the
information
in
this
log
we're
going
to
be
saving
or
reading
data
from
the
graph
node.
A
It
will
look
something
like
this,
where
we
have
all
this
this
information
here
so
we'll
have
like
the
owner
id
we'll
have
we
might
have
the
contract
id
we'll
have
you
know
the
token
id
we
have
all
this
stuff,
maybe
the
price.
So
we
can
use
all
this
information
out
of
the
log
that
we're
gonna
have
access
to
and
to
do
that,
it's
gonna
be
coming
in
as
a
string,
so
we
essentially
just
need
to
parse
that
and
then
use
it
and
turn
it
into
an
object.
A
So
we're
gonna
first
take
the
item
and
turn
it
into
a
string,
we're
gonna
log
it
out.
This
is
optional.
Of
course,
we're
gonna
then
basically
take
out
some
text
that
we
don't
need.
So
it's
going
to
be
an
shaped
as
an
object,
and
it's
also,
though,
going
to
also
have
this
prepended
event
json,
and
what
we
really
want
to
do
is
just
remove
that
event
json
and
then
turn
that
string
into
an
object.
A
So
what
I'm
really
doing
here
is
I'm
saying:
okay,
let's
get
this
string,
let's
remove
event
json
and
then
the
string
we
have
now
can
be
turned
into
an
object.
It's
in
the
right
format
for
us
to
do
that.
So
I
will
then
parse
that
and
turn
it
into
an
object
using
the
apis
that
we
have
available
from
the
graph
typescript
library,
and
now
we
have
this
json
object
and
we
can
now
start
reading
information
from
that
object.
A
We
first
are
going
to
now
create
an
array
or
reference,
the
the
data
coming
in
here
and
turning
it
into
an
array
we're
going
to
first
we're
going
to
then
take
the
first
item
in
that
array
and
turn
it
into
an
object.
And
now
we
have
a
new
object
that
will
have
some
of
the
metadata
that
we
were
just
looking
at
stuff
like
this
made
available
to
us.
A
So
the
two
properties
that
we're
going
to
be
getting
off
of
it
to
be
working
with
are
going
to
be
the
token
ids
and
the
owner
id
and
the
token
isds
should
be
an
array
of
just
really
one
item.
So
we're
going
to
go
ahead
and
get
those
two
values,
the
token
id
and
the
owner
id
and
we're
going
to
now
work
with
those
values
and
we're
going
to
then
check
to
see
if
they
exist,
because
if
they
don't
exist,
we'll
just
return
for
whatever
reason
they
should
exist.
Though,.
A
Get
back
up
here
there
we
go
we're
going
to
take
those
token
ids
and
turn
them
into
an
array.
A
We're
gonna
then
take
the
zero
with
item
or
the
first
item
and
then
turn
it
into
a
string
and
then
now
we
can
call
out
to
the
graph
node
to
load
that
token
id,
because
we
have
that
token
id
and
we
can
basically
check
to
see
if
it
exists
already,
because
if
it
does
exist
we
can.
We
can
assume
that
some
of
this
data
is
already
there,
but
if
it
doesn't,
we
can
go
ahead
and
add
it
so
we'll
see
if
the
token
does
not
exist.
A
And
let
me
make
sure
I
copy
that
right.
Maybe
we
need
one
more
closing
object:
okay,
there
we
go
so
now
that
we
have
the
metadata
we
can
call
out
to
ipfs.
So
here
we're
doing
that.
We're
saying
ipfs.cat
we're
passing
in
the
metadata
and
if
there
is
a
result
here,
we're
going
to
then
turn
the
result
into
an
object
and
we're
going
to
see
if
that
object,
that
value
exists
and
then
we're
going
now
have
this
object.
A
A
A
lot
of
code,
okay,
cool,
so
now
that
we've
created
the
token
or
we
know
that
the
token
exists,
so
we've
either
created
or
loaded
it.
We
now
update
the
owner
id
because
if
this
was
a
transfer
to
someone
else,
we
want
to
update
that
and
we
also
want
to
update
the
owner.
A
So
we
update
those
two
values
or
set
those
two
values.
We
then
check
to
see
if
this
user
already
exists.
If
they
don't,
we
go
ahead
and
create
that
user,
and
then
we
save
both
the
token
as
well
as
the
user,
and
then
we're
done.
Everything
is
ready
to
go
now.
We
should
be
able
to
kind
of
start
querying
both
tokens
and
users,
so
we've
taken
into
account.
Hopefully
you
know
whether
the
token
exists
or
the
user
exists
and
saved
both
of
those.
A
A
All
right,
so
it
says,
build
completed.
What
we
can
now
do
is
deploy
and
see
if
this
this
sub
graph
actually
works
so
to
deploy
the
subgraph,
we
need
our
access
token.
So
what
I
can
do
here
is
go
copy,
my
access
token,
and
we
can
now
run
this
command
here,
so
we
can
say
graph
off
and
I
can
paste
that
in
and
then
I
can
copy
the
access
token
here.
A
A
All
right,
so
everything
looks
like
it
worked
properly.
So
if
I
go
back
to
the
dashboard-
and
I
refresh-
we
should
see
that
it's
syncing
now.
I
have
already
deployed
a
similar
api
to
this,
so
it
will.
It
should
pick
up
that
the
data
is
the
same
actually
or
maybe
it
doesn't.
I
think
because
it
may
be
starting
from
a
completely
new
api.
A
Actually,
so
it's
not
going
to
do
that,
but
you
will
see
that
it's
starting
to
sync
and
as
entities
get
saved,
this
number
will
go
up
and
you
will
be
in
begin
to
be
able
to
start
querying
for
data
just
like
this,
so
I
can
go
to
and-
and
it
honestly
will
only
take
a
couple
of
minutes
before
you'll
start
seeing
data,
because
you
can
go
to
logs
and
kind
of
see
the
progress,
but
it's
kind
of
like
warming
up
right
now
and
getting
everything
going
and
it'll
start
looping
through
all
the
transactions.
A
But
if
I
go
to
my
dashboard
and
I
go
to
near
misfits,
which
is
where
I
deployed
this
earlier-
you
can
see
that
you
know
it's
already
been
deployed
somewhere
else
and
it's
working.
So
we
have
this
way
to
query
now
and
I
can
now
just
like
maybe
query
for
tokens
getting
the
id
the
under
id.
A
Here
we
go,
we
see
our
metadata.
We
also
have
that
full
text
search
so
in
our
sub
graph.
I
believe
we
called
that
something
like
token
search,
which
is
the
name
of
the
search.
So
if
I
go
here
and
I
say
token
search,
I
can
now
pass
in
text
and
I
might
say
I
want
to
look
for
only
meerkats,
because
you
have
normies
meerkats
and
a
bunch
of
other
different
kinds.
A
All
right
cool
so
now
we're
basically
going
to
do
a
full
text,
search
we're
going
to
return
the
image,
the
token
id
and
the
owner
and
we're
going
to
return
the
owner's
tokens
and
the
token
ids
that
they
own.
So
this
is
a
little
bit
more
of
a
complex
query,
and
here
you
see
that
we
have
the
image
coming
back.
We
have
the
owner
the
tokens
all
that
stuff.
A
So
you
know
it
will
take.
Maybe
five
or
ten
minutes
before
that
should
show
up
on
your
subgraph
after
you've
deployed
a
brand
new
one,
but
I
will
check
back
anyway
just
to
see
if
we
see
anything
yeah,
not
quite
yet,
but
as
long
as
there's
no
error,
you
should
be
good
to
go
as
long
as
you've
gone
through
the
code
that
we
went
through
there.
A
So
that's
it.
If
you
have
any
questions,
we
have
the
graph
discord.
Definitely
go
there
check
it
out.
I
appreciate
you
checking
out
this
workshop
and
if
you
have
any
issues
on
the
workshop
as
well,
definitely
feel
free
to
open
them
up.
This
is
github
and
I'd
be
happy
to
try
to
address
those
as
soon
as
possible.
So
thank
you
and
good
luck.