This article applies to:
- Ethereum
- Polygon
- BNB Smart Chain
- Avalanche
- Fantom
- Harmony
This article gives an overview of the difference between executing a query on a Geth node via GraphQL vs web3.js v1.x vs web3.js v2.x.
Overview
- GraphQL — you can do one query to execute different RPC calls.
- web3.js v1.x — BatchRequest puts all contract calls in an array of JSON-RPC calls.
- web3.js v2.x — BatchRequest wraps all contract calls in a Promise.
GraphQL
GraphQL is a query language designed to provide flexibility when querying data.
GraphQL was introduced as an alternative to the Ethereum JSON-RPC interface. See EIP-1767.
GraphQL solves the underfetching and overfetching issue faced by many when retrieving data from an EVM node.
Below is an example on how we can execute three different RPC calls within one GraphQL query and format the response according to the format we specify. For more information also checkout the GraphQL on Ethereum: Availability on Chainstack and a quick rundown article.
web3.js BatchRequest
The web3.js function BatchRequest allows you to aggregate a list of contract function calls, these function calls will be transformed into an array of JSON-RPC calls before being sent to the Ethereum node in one XMLHttpRequest. web3.js 2.x wraps the batchRequest in a promise as compared to web3.js 1.x as discussed in this issue.
Below are two examples of the different implementations.
Geth node will execute the JSON-RPC calls asynchronously on the backend for all the three implementations.
To learn more about executing batch calls take a look at The ultimate guide to getting multiple token balances on Ethereum.
Full code is in the Ethereum token balances repository.
Comments
0 comments
Article is closed for comments.