Querying Subgraphs

Querying Subgraphs

Writing A Python Script to Query & Fetch data from a Subgraph

What the heck is The Graph & are they worth the hype?

The Graph essentially is a decentralized protocol for indexing and querying data from blockchains. It makes it possible to query data that is difficult to query directly. The Graph solves the complex and time-consuming problem of Indexing blockchain data, with the help of a decentralized protocol that indexes and enables the performant and efficient querying of blockchain data. These indexed subgraphs are APIs that can then be queried with a standard GraphQL API. These Graphs are built around the idea of Subgraphs.

They're saving a hell lot of engineering time and thus a hell lot of money in the aggregate, therefore they're definitely worth the hype!

Skimming through Subgraphs

A Subgraph Defines:-

  • Which data to fetch from the blockchain(most of the time it's smart contract events).

  • Method to query this data from a consumer's point of view.

Here's a little pictorial representation for your reference:

Screenshot from 2022-01-23 22-55-09.png

To dive into deep aspects make sure to visit The Graph Docs

The Python Script!

So for this example, we'll query the Uniswap subgraph

Screenshot from 2022-01-23 22-41-01.png

Have a look at what's the Uniswap subgraph is like:-

Screenshot from 2022-01-23 22-41-59.png

Installing the necessary python dependencies:-

import requests
import json
import pandas as pd
  • The requests module allows you to send HTTP requests using Python. The HTTP request returns a Response Object with all the response data (content, encoding, status, etc).

  • Python has a built-in package called json, which can be used to work with JSON data. If you have a JSON string, you can parse it by using the json.loads() method, the result will be a python Dictionary.

  • pandas is a fast, powerful, flexible, and easy to use open-source data analysis and manipulation tool, built on top of the Python programming language.

Setting the endpoint URL

We'll now set the endpoint URL of the subgraph that we would like to query to fetch the data, in our case it's Uniswap's

# set endpoint url in here 'https://api.thegraph.com/subgraphs/api/[api-key]/[Subgraph you want to Query]'
url = 'https://api.thegraph.com/subgraphs/name/uniswap/uniswap-v2'

Define the Query

Let's say for this example, we want to get the id, token0 & reserve0, ETH in the reserve & its corresponding USD in the reserve. To query your desired info, make sure that you understand the Subgraph Schema that is provided by the organization, or the one that you defined for your personal subgraph.

# define query
query = '''{
  pairs(first:10, orderDirection = desc) {
     id
    token0 {
      id
    }
    reserve0
    reserveETH
    reserveUSD

  }
}'''

Making requests & loading results into the JSON format

# make the request
r = requests.post(url, json={'query': query})
# load result into json
json_data = json.loads(r.text)

Converting JSON into a dataframe

Conversion into the dataframe will allow us to easily manipulate the data and finally convert it into a .csv file.

# convert json into a dataframe
df = pd.DataFrame(json_data['data']['pairs'])
# check result
print(df)

Finally, writing the data into a CSV file

# writing data to csv
df.iloc[:,0:4].to_csv("fetchedData.csv", index=False)

To get the full code as a whole, feel free to visit my GitHub Repo

Thank you so much for reading the article, I really appreciate it! I hope it saved you from getting through a tough time.

Did you find this article valuable?

Support Parth Arora by becoming a sponsor. Any amount is appreciated!