Query using Subgraph

This section will guide you through querying contract state data from the blockchain using a deployed Subgraph.

Contracts:

Prerequisites:

  • Deployed Subgraph on Alchemy. If not, please follow the instructions [here].

  • NodeJS: version v20.14.0 or v20.15.0.

  • Package Manager: yarn or npm.

Query Example

Folder Structure:

query-subgraph
├── package.json
├── src
   ├── main.ts
   └── query.ts
└── tsconfig.json

Let's get started:

  1. Create a project folder:

  1. Create package.json and install dependencies:

  • Create package.json

  • Add the following content to package.json:

  • Install dependencies:

  1. Create tsconfig.json:

  • Create the file:

  • Add the following content to tsconfig.json:

  1. Create .env file:

  • Create the file:

  • Add the following content to .env:

  1. Create a script to query from the Alchemy Subgraph:

  • Create a script file:

  • Add the following code to the query.ts script:

  1. Create main.ts to test the setup:

  • Create the file:

  • Add the code into main.ts:

  1. Run the test:

  • After running, you should see output similar to the following:

Last updated