Geth - GraphQL Connectivity and Examples

Follow

Geth Websocket Connectivity 

Summary

GraphQL endpoints allow users to use a query language to get read-only data from their Ethereum node. With GraphQL a user is able to gather the same read-only data that the JSON-RPC endpoints offer but in a more standardized output.

You can query data such as:

  • blocks
  • pending
  • transaction
  • logs
  • gasPrice
  • protocolVersion
  • syncing
  • chainID

To view the specific subfields available to each schema please reference the schema section of this document.

How to access GraphQL on my Ethereum node 

Once you spin up a node with Blockdaemon you automatically have access to GraphQL on your node. You will need to make calls to a specific endpoint using your node url e.g: https://<NODE XID>.bdnodes.net/graphql

enpoint_graphQL._ws.jpg

# Try it now - enter a block number to get the block hash, number of transactions on the block, and the block timestamp.
curl -X POST 'https://<NODE XID>.bdnodes.net/graphql' -H 'Content-Type: application/json' -H 'X-Auth-Token: <AUTH TOKEN>' --data-raw '{"query":"{block(number: ######){hash transactionCount timestamp}}"

Note: To ensure you are getting accurate and up to date information ensure your node is fully synced before running your queries.

 

Note: You also now have the option to switch to display Wbsockets or JSON RPC endpoints:

 

geth_websocket_endpoint.jpg

How can I query my node

You can run queries against your node using an API tool like Postman or with simple curl commands. When running queries you will need to have your node url and your auth token in order to make queries.

 

Using Postman

Postman is an API tool that can make GraphQL requests on your node. Below you will find steps on how to create your first request:

  1. Create a new collection for your requests (optional)
  2. Create a new request
  3. Set your request as  POST
  4. In the request url textbox enter your node url e.g. https://<NODE XID>.bdnodes.net/graphql
  5. In the Headers section create a new key value pair
    1. Key -> X-Auth-Token
    2. Value -> <YOUR AUTH TOKEN>.  ws_auth_token.jpg
  6. In the Body section make sure GraphQL is selected
  7. In the Query section entry your query statement
  8. Click send, your resulting data will be in the response section.  ws_8..jpg

 

Using cURL

You can access GraphQL on your Ethereum node using terminal. You will need your node url and your auth token to make calls. You will hit the same node url endpoint but change your data payload with new queries. Below you will find some examples of curl commands with different queries. Ensure to set subfields in your queries in order to see the data you want.

You can query a specific block for the following information with the query below:

  • Block hex number
  • Block hash
  • Parent block hash and hex number
  • Transaction on block
  • Gas used on block
  • Timestamp of block
curl -X POST 'https://<NODE XID>.bdnodes.net/graphql' -H 'Content-Type: application/json' -H 'X-Auth-Token: <AUTH_TOKEN>' --data-raw '{"query":"{block(number: 12345){number hash parent{number hash} transactionCount gasUsed timestamp}}"}'

 

You can query a specific transaction for the following information with the query below:

  • From address
  • To address
  • Value
  • Gas
  • Block hash where there transaction occurred
curl -X POST 'https://<NODE XID>.bdnodes.net/graphql' -H 'Content-Type: application/json' -H 'X-Auth-Token: <AUTH_TOKEN>' --data-raw '{"query":"{transaction( hash: \"0x0cd5e04fa2c421c0675fde354063dc25ae43decc970ff8f05d417d0a349e4970\"){index from{address} to{address} value gasPrice gas status block{hash}}}"}'

Additional Links

Above you see a few examples of certain queries you can run on your Ethereum node. Below you can find links to additional information. Note in some of these examples not all queries will work on your node, but you can get a general understanding of how to use GraphQL:

 

1 out of 1 found this helpful

Comments

0 comments

Please sign in to leave a comment.