DEV Community

Cover image for Query blockchain data using GraphQL
Maxime Julian
Maxime Julian

Posted on

Query blockchain data using GraphQL

TLDR: I just released eth-graphql, a library that lets you fetch contract data from any EVM-compatible blockchain using GraphQL.

Working with web3 is awesome, but due to relative novelty of blockchain technologies we do not yet have access to all the tools that have been been built for web2 over the years.

One particular problem I faced when fetching contract data was the amount of individual requests I had to make to gather the data I needed to display on the frontend. Even using the amazing library React Query, having to make a few dozen requests on every page load then manually mash the data together quickly becomes messy.

A solution to that problem is to use a multicall contract. Such a contract lets you aggregate multiple calls into one, which offers two major advantages:

  • it significantly reduces the number of requests needed to obtain the data you need to fetch, meaning you no longer have to track the progress of dozens of individual queries
  • it ensures that all the data you fetch is timestamped at the same block. This can be crucial when fetching data that can change from one block to another, such as token prices

Using React Query and multicall is what I am currently doing at my regular job, but having come from the world of web2 the idea of adding GraphQL to the mix popped in my head.

After some research and a quick proof of concept, I decided to get serious about this and to release my work as an open source library: eth-graphql.

In essence, eth-graphql is an Apollo link that lets you query contract data from any EVM-compatible blockchain using GraphQL. With as little as a contract name and an ABI, it will automatically construct a local GraphQL schema you can then use to make queries against the blockchain: all of this from the client-side! All the calls needed to fulfill a query are aggregated using a well-known multicall contract, meaning each query will only make one request to your RPC provider no matter how many contracts you need to call.

I highly encourage you to try it for yourself. The setup is very simple and you'll start building your queries in just a few minutes. If you like the project, don't hesitate to give a star to the repository: this helps reach more people who could benefit from eth-graphql.

Top comments (0)