FUEL GraphQL Queries
Table of contents
· What is GraphQL?
∘ HTTP and APIs
∘ How does GraphQL work?
· GraphQL queries
· Example queries
∘ Node version
∘ Chain Name
∘ Last block
∘ Node health
∘ Peers
· Queries with parameters
∘ Block information by height
∘ Obtain the asset balance of an address
· References
What is GraphQL?
GraphQL is a query language for APIs and an execution environment for performing these queries against existing data. It allows clients to request exactly the data they need, improving efficiency and reducing the amount of data transferred compared to traditional REST APIs.
HTTP and APIs
HTTP (Hypertext Transfer Protocol) is a set of rules for accessing resources on the web. These resources can be HTML files, data from a database, photos, text, etc. These resources are made available through an API (Application Programming Interface), which can be accessed via the HTTP protocol. This allows developers to request resources efficiently.
How does GraphQL work?
GraphQL works by defining types and the properties available on those types (schema) and defining resolvers, which are functions responsible for obtaining the data for each field.
Queries in GraphQL allow you to read data in a specific and efficient way. You can ask for exactly the data you need and get multiple resources in a single query.
You can make enquiries by visiting the Fuel RPC Endpoint:
You can use your own node in case you are running a Fuel node, +info:
GraphQL queries
A simple example, query the version of the node.
query { nodeInfo{nodeVersion}}
If you are running a node you can run it in CLI:
curl -X POST http://0.0.0.0:4000/v1/graphql \
-H "Content-Type: application/json" \
-d '{"query": "{ nodeInfo{nodeVersion}}"}'
We can also copy the CLI code for use directly on your server using the COPY CURL button.
curl 'https://devnet.fuel.network/v1/graphql' -H 'Accept-Encoding: gzip, deflate, br' -H 'Content-Type: application/json' -H 'Accept: application/json' -H 'Connection: keep-alive' -H 'DNT: 1' -H 'Origin: https://devnet.fuel.network' --data-binary '{"query":"query { nodeInfo{nodeVersion}}","variables":{"height":"3412"}}' --compressed
Example queries
Node version
Returns the version of the node’s app
query { nodeInfo{nodeVersion}}
Chain Name
Returns the name of the Chain
query { chain{name}}
CLI
curl -X POST http://0.0.0.0:4000/v1/graphql \
-H "Content-Type: application/json" \
-d '{"query": "{ chain{name}}"}'
Last block
Returns the height of the last processed block.
query { chain{latestBlock{ id height}}}
CLI
curl -X POST http://0.0.0.0:5000/v1/graphql \
-H "Content-Type: application/json" \
-d '{"query": "{ chain { latestBlock { id height } } }"}'
Node health
Indicates the status of the node
{"data":{"health":true}}
CLI
curl -X POST http://0.0.0.0:5000/v1/graphql \
-H "Content-Type: application/json" \
-d '{"query": "{ health }"}'
Peers
Returns the peers of the node
query { nodeInfo{peers{id}}}
CLI
curl -X POST http://0.0.0.0:5000/v1/graphql \
-H "Content-Type: application/json" \
-d '{"query": "{ nodeInfo{peers{id}} }"}'
Queries with parameters
The API has another type of queries that allow the introduction of parameters called query variables.
Block information by height
Displays the id of a block by entering its height.
query Block($height: U64) { block(height: $height) { id } }
Variables:
{"height": "3412"}
Obtain the asset balance of an address
Displays the balance of cryptographic assets associated with a wallet by entering its address.
query Balance($address: Address, $assetId: AssetId) {
balance(owner: $address, assetId: $assetId) {
owner
amount
assetId
}
}
Variables:
{
"address": "0xf4191cdce0e418264a745b4c1011dfa708a1da9face011aac8703537ce83a18a",
"assetId": "0xf8f8b6283d7fa5b672b530cbb84fcccb4ff8dc40f8176ef4544ddb1f1952ad07"
}
References
Introduction to the Fuel GraphQL API
Twitter | Medium | LinkedIn | Discord | Telegram | cumulo.pro