GraphQL on Ethereum: Availability on Chainstack and a quick rundown

Starting today you can use GraphQL on Chainstack Ethereum nodes.

In brief

  • GraphQL is available on dedicated Ethereum nodes on Chainstack starting from the Growth plan.
  • Where previously JSON-RPC was your only option to query the data on an Ethereum node, you can now do so with GraphQL.
  • Full nodes do not have historical data but can be queried for the latest data.
  • Archive nodes have all the data—latest and historical—and can be queried live.
  • The Graph has prebuilt and pre-indexed sets of data—called subgraphs—and querying them is the fastest, albeit limited to a data set.
  • Chainstack supports all options. Our Ethereum archive nodes are already feeding the Graph nodes used by the community. We are also looking into supporting the Graph nodes natively.

Geth and GraphQL

Back in the middle of 2019, Go Ethereum (Geth) introduced native GraphQL support with the release of version v1.9.0. Geth was not just the only client to do so but did the implementation fairly quickly and with enthusiasm.

Read the original EIP-1767 for GraphQL support.

At the same time, there’s The Graph project with subgraphs, also using GraphQL.

So what are all of these, and what’s the difference?

What is GraphQL?

GraphQL is a query language and a runtime that allows the client to define a schema of all the entities it has and the relationships between the entities. With GraphQL, all you need as a user is one endpoint that you send your query to and define what fields and relationships you want to get in the response.

This is in contrast to the traditional REST API and JSON-RPC, where one has to set up in the client many endpoints that the API users might need. With the REST API, you, as a user, need to send your requests over many routes and then process the many responses.

With GraphQL, you just have one route and a query that you define.

Why replace JSON-RPC with GraphQL?

The primary purpose of JSON-RPC is to interact with the Ethereum node, not just retrieve some read-only data off the node. As such, the JSON-RPC method tends to be resource-wasteful. For example, for simple data retrieval requests like eth_getBlock, it will return all of the extra data that the user requesting may not be interested in.

GraphQL, on the other hand—specifically designed to query the data the user needs— is resource-efficient.

From the moment GraphQL support was introduced and implemented in Geth, you can run the GraphQL queries both in Geth full nodes and Geth archive nodes.

Full nodes

For a simple example, let’s take the Uniswap V2: Factory Contract and run a GraphQL query against a full node to see how many transactions it has from the start to the latest block.

Chainstack conveniently provides GraphiQL—a GraphQL IDE—that lets you type in and execute your queries.

To open your instance of GraphiQL, navigate to your deployed Ethereum full node in the Chainstack UI and click Open next to GraphQL IDE URL.

In your GraphiQL instance, paste in the following query to get all the Uniswap V2: Factory Contract transactions:

{ 
  block { 
account(address:"0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f"){ 
      transactionCount 
    } 
  } 
} 

Running this query will give you the number contract creating transactions done from the start until the latest block.

Archive nodes

If you want to check the number of transactions from the start until a certain block in the past, you won’t be able to do this with the full node as this requires querying the historical state.

Only an archive node can provide historical state.

So let’s now run the very same query for a block in the past against an archive node deployed with Chainstack.

Again, navigate to your deployed Ethereum archive node in the Chainstack UI and click Open next to GraphQL IDE URL.

In your GraphiQL instance, paste in the query for block 10010000 to get all the Uniswap V2: Factory Contract transactions:

{ 
  block (number:10010000) { 
account(address:"0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f"){ 
      transactionCount 
    } 
  } 
} 

Running this query will give you the number contract creating transactions done from the start until block 10010000. This is something that can only be done on an archive node. And on The Graph.

The Graph

Where does The Graph fit in the Geth and GraphQL scenario?

The Graph works a little bit differently by introducing the so-called subgraphs.

Subgraphs are prebuilt and pre-indexed data sets deployed to the Graph nodes that are automatically maintained and updated.

To create a subgraph, you use The Graph code and define in a YAML file what smart contract to index and what smart contract events to listen to. Then create a GraphQL schema for your subgraph that users will later be able to query.

Having defined your subgraph, you can start your own Graph node with your subgraph deployed that will index the data, or you can deploy it to a node run by The Graph organization. The defined data will be indexed and stored in its database, ready to be immediately queried.

To do a similar query with the Uniswap V2: Factory Contract, you can head over to The Graph playground to interact with the Uniswap subgraph.

Paste in the following query:

{ 
  uniswapFactory(id: "0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f", block: {number: 10010000}){ 
     txCount 
  }
}

It will take the subgraph less than a second to process it, because it’s querying a pre-indexed data set, and respond with the total number of transactions for the Uniswap V2: Factory Contract until block 10010000. You can’t get the transaction list as it’s not defined in the subgraph, but it’s fast for what it does.

You will be able to build your own subgraph by using The Graph and Ethereum nodes deployed by Chainstack. Check out our The Graph service at Chainstack marketplace and reach out to us if you want to launch a Graph node on Chainstack.

Conclusion

GraphQL is a great and efficient way to query live Ethereum data on full and archive nodes. Go Ethereum did a great job implementing GraphQL.

If you have a set of specific queries that you need to run often and with fast response times, your best bet is to build your own pre-indexed data set—called subgraph—by using The Graph and Chainstack Ethereum nodes. The data set will be updating itself live by pulling the data off Ethereum nodes and putting it in your Graph database that you can query any time.

Join our community of innovators

Chainstack uses cookies to provide you with a secure and
personalized experience on its website. Learn more.