iota-sdk-graphql-client
The IOTA GraphQL client is a client for interacting with the IOTA blockchain via GraphQL. It provides a set of APIs for querying the blockchain for information such as chain identifier, reference gas price, protocol configuration, service configuration, checkpoint, epoch, executing transactions and more.
Design Principles
- Type Safety: The client uses the
cyniclibrary to generate types from the schema. This ensures that the queries are type-safe. - Convenience: The client provides a set of APIs for common queries such as chain identifier, reference gas price, protocol configuration, service configuration, checkpoint, epoch, executing transactions and more.
- Custom Queries: The client provides a way to run custom queries using the
cyniclibrary.
Usage
Connecting to a GraphQL server
Instantiate a client with [Client::new(server: &str)] or use one of the predefined functions for different networks [Client].
use Client;
use Result;
async
Requesting gas from the faucet
The client provides an API to request gas from the faucet. The request_and_wait function sends a request to the faucet and waits until the transaction is confirmed. The function returns the transaction details if the request is successful.
Example for standard devnet/testnet/local networks.
use FaucetClient;
use Address;
use Result;
use FromStr;
async
Example for custom faucet service.
Note that this FaucetClient is explicitly designed to work with two endpoints: v1/gas, and v1/status. When passing in the custom faucet URL, skip the final endpoint and only pass in the top-level url (e.g., https://faucet.devnet.iota.cafe).
use FaucetClient;
use Address;
use Result;
use FromStr;
async
Custom Queries
There are several options for running custom queries.
- Use a GraphQL client library of your choosing.
- Use the cynic's web generator that accepts as input the schema and generates the query types.
- Use the cynic's CLI and use the
cynic querygencommand to generate the query types.
Below is an example that uses the cynic querygen CLI to generate the query types from the schema and the following query:
where custom_query.graphql contains the following query:
query CustomQuery($id: UInt53) {
epoch(id: $id) {
referenceGasPrice
totalGasFees
totalCheckpoints
totalTransactions
}
}
When using cynic and iota-sdk-graphql-client, you will need to register the schema by calling iota-sdk-graphql-client-build::register_schema in a build.rs file. See iota-sdk-graphql-client-build for more information.
The generated query types are defined below. Note that the id variable is optional (to make it mandatory change the schema to $id: Uint53! -- note the ! character which indicates a mandatory field). That means that if the id variable is not provided, the query will return the data for the last known epoch.
Note that instead of using Uint53, the scalar is mapped to u64 in the library using impl_scalar(u64, schema::Uint53), thus all references to Uint53 in the schema are replaced with u64 in the code below.
;
The complete example is shown below:
use Result;
use QueryBuilder;
use ;
use Address;
// The data returned by the custom query.
// The variables to pass to the custom query.
// If an epoch id is passed, then the query will return the data for that epoch.
// Otherwise, the query will return the data for the last known epoch.
// The custom query. Note that the variables need to be explicitly declared.
// Custom query with no variables.
async