Deploying a Subgraph

WebSculpt
5 min readJan 19, 2024

--

Once you have created a new subgraph from The Graph Dashboard, you will see initialization and deploy details, like this 👇

The first thing to do is to be sure you have the latest version of graph-cli installed/updated using NPM or Yarn:

npm install -g @graphprotocol/graph-cli

yarn global add @graphprotocol/graph-cli

After that, you are ready to go. Below, I will show you what the process looks like.

Your particular subgraph deploy, playground, and details will be at your link: https://thegraph.com/studio/subgraph/{SUBGRAPH_NAME}

For the following, I was initializing and deploying a subgraph named cf-test-one

Step One

graph init --studio cf-test-one

Select the protocol you wish to work on

Slug will then auto-populate

Then choose a directory to create the subgraph in (it will also auto-populate)

Choose a chain

Provide contract address:

Now we need to provide either an ABI or an ABI path. If no ABI is found, Retry (or select ‘N’ to provide the local ABI path) [Note that only verified contracts will be able to automatically retrieve these ABIs].

OUR LOCALLY GENERATED ABI will be in deployments/[protocol name]

Provide the start block (which is just the block that you deployed on):

Provide a “Contract Name” (can’t have spaces or hyphens)

Choose ‘Yes’ to Index events as entities

Then, you could add more contracts here 👇

THEN STEP ONE IS DONE:

AND YOU’LL HAVE THIS NEW DIRECTORY ON YOUR MACHINE

STEP TWO

graph auth --studio x999999999999999999

output log states that you have successfully set the deploy key

cd cf-test-one

☝️just be sure you get into the new directory before you codegen, build, and deploy

graph codegen && graph build

graph codegen will generate the /generated/ directory based on the schema. Run graph codegen if you change the schema.

graph build compiles to web assembly and prepares to deploy (alerting you about any problems along the way).

FINAL STEP

graph deploy --studio cf-test-one

☝️ will prompt you for incrementing version number (i.e.. 0.0.1)

Quick Review

  • Slug and Directory will automatically populate
  • THEN you will see a huge list of chains
  • If you can’t use the actual contract address, you can use the deployed ABI path here: /packages/hardhat/deployments/sepolia/YourContract.json
  • Contract Name (at the end) has to be one word (no hyphens either)
  • You will be prompted to add more contracts (yes or no)

When finished, you will see this:

Next steps:

1. Run `graph auth` to authenticate with your deploy key.

2. Type `cd your-project-directory` to enter the subgraph.

3. Run `yarn deploy` to deploy the subgraph.

Make sure to visit the documentation on https://thegraph.com/docs/ for further information.

Then you can just go back to subgraph studio and copy/paste the rest:

  1. graph auth --studio xxxxxxxxxxxxxxxxxxxxxxxxx
  2. cd SUBGRAPH_NAME
  3. graph codegen && graph build
    — Type-safety on connections
    — Compiles everything
  4. graph deploy --studio SUBGRAPH_NAME

Want to learn more about Subgraphs and The Graph?

What is a Subgraph?

This is — simply put — a way of organizing blockchain data — rules and instructions you give to the indexers around the world so that you can query the data. Here is a great video going over the topic.

Image from video: https://youtu.be/EJ2em_QkQWU?si=zJ_1amXXLHwJ6JCZ

  • Off-chain data: A good example of this would be IPFS
  • subgraph.yaml: Rules/Overview && Shape/Structure
  • mapping.ts: Apply logic for Data Transformation (you could look at this as the “business logic”)
  • schema.graphql: Data goes into “buckets” (AKA: “entities”); you will send GraphQL queries to these “buckets” to get data to your front-end

Example of a query

To see a subgraph get deployed: https://youtu.be/EJ2em_QkQWU?t=517

Image from video: https://youtu.be/EJ2em_QkQWU?si=zJ_1amXXLHwJ6JCZ

This image sums it up pretty well ☝️

Why use The Graph?

Much more “reading from the blockchain” occurs than “writing to the blockchain”. The web 3.0 solution is to have decentralized reading and decentralized indexing from the blockchain.

Image from video: https://youtu.be/EJ2em_QkQWU?si=zJ_1amXXLHwJ6JCZ

☝️If any of the nodes in the middle go down, the front-end can use other nodes.

Image from video: https://youtu.be/EJ2em_QkQWU?si=zJ_1amXXLHwJ6JCZ

The idea is to take the advantages of blockchain/decentralization and apply that to the read layer.

The blockchain is used to being written-to ( all the time! ) … but now we need reading to become decentralized. That’s why we are using The Graph/Subgraphs for this project.

--

--

WebSculpt

Blockchain Development, coding on Ethereum. Condensed notes for learning to code in Solidity faster.