Skip to main content

Use GraphQL over HTTP

GraphQL can reduce the overhead needed for common queries. For example, instead of querying each receipt in a block, GraphQL can get the same result with a single query for the entire block.

The Besu GraphQL schema describes the GraphQL implementation for Ethereum. Enable the GraphQL service using command line options.

note

GraphQL is not supported over WebSocket.

Access the GraphQL endpoint at http://<HOST>:<PORT>/graphql. Configure <HOST> and <PORT> using graphql-http-host and graphql-http-port. The default endpoint is http://127.0.0.1:8547/graphql.

GraphQL requests with cURL

Hyperledger Besu JSON-RPC API methods with an equivalent GraphQL query include a GraphQL request and result in the method example.

For example, the following request returns the block number:

curl -X POST -H "Content-Type: application/json" --data '{ "query": "{block{number}}"}' http://localhost:8547/graphql

The following request returns the gas price:

curl -X POST -H "Content-Type: application/json" --data '{ "query": "{gasPrice}"}' http://localhost:8547/graphql

The following syncing request returns data about the synchronization status:

curl -X POST -H "Content-Type: application/json" --data '{ "query": "{syncing{startingBlock currentBlock highestBlock}}"}' http://localhost:8547/graphql
note

In some cases, for example, when your node is fully synced, the syncing request returns a null response:

{
"data" : {
"syncing" : null
}
}

GraphQL requests with GraphiQL app

The third-party tool, GraphiQL, provides a tabbed interface for editing and testing GraphQL queries and mutations. GraphiQL also provides access to the Besu GraphQL schema from within the app.

GraphiQL

Pending

transactionCount and transactions supports the Pending query.

info

Besu does not execute pending transactions so results from account, call, and estimateGas for Pending do not reflect pending transactions.

curl -X POST -H "Content-Type: application/json" --data '{ "query": "{pending {transactionCount}}"}' http://localhost:8547/graphql
curl -X POST -H "Content-Type: application/json" --data '{ "query": "{pending {transactions{hash}}}"}' http://localhost:8547/graphql