Expand description
§Description
This crate provides a function to register a schema to enable building custom queries using cynic derive macros queries. Call
this function in a build.rs file in your crate if you need to build custom queries.
§Usage
- Add this crate as a build dependency in your
Cargo.tomlfile.
[build-dependencies]
iota-sdk-graphql-client-build = { git = "https://github.com/iotaledger/iota-rust-sdk", package = "iota-sdk-graphql-client-build", branch = "develop" }- Add a
build.rsfile in your crate root directory and call theregister_schemafunction in it.
// build.rs file
fn main() {
let schema_name = "MYSCHEMA";
iota_graphql_client_build::register_schema(schema_name);
}- Add the
cynicandiota-sdk-graphql-clientdependencies in yourCargo.tomlfile. You should have something like this.
# Cargo.toml
# ...
[dependencies]
cynic = "3.8.0"
iota-sdk-graphql-client = { git = "https://github.com/iotaledger/iota-rust-sdk", package = "iota-sdk-graphql-client", branch = "develop" }
[build-dependencies]
iota-sdk-graphql-client-build = { git = "https://github.com/iotaledger/iota-rust-sdk", package = "iota-sdk-graphql-client-build", branch = "develop" }-
If using
cynic, use the cynic generator to generate the Rust types from the GraphQL schema.
Go to https://generator.cynic-rs.dev/ and paste the URL to the GraphQL service or manually copy paste the schema.
Then you can select the fields in the query you want to have, and the generator will generate the Rust types for you. -
In your Rust code, you can now use the custom query types generated by
cynic.
// lib.rs
// Custom query
use cynic::QueryBuilder;
use iota_graphql_client::{query_types::schema, Client};
#[derive(cynic::QueryFragment, Debug)]
#[cynic(schema = "MYSCHEMA", graphql_type = "Query")]
pub struct MyQuery {
pub chain_identifier: String,
}
#[tokio::main]
async fn main() {
let client = Client::new_mainnet();
let operation = MyQuery::build(());
let q = client.run_query(&operation).await.unwrap();
println!("{:?}", q);
}-
For
UInt53, you can useu64type directly as theiota-sdk-graphql-client’s schema implements theimpl_scalar. Similarly for other types (Base64, DateTime). See more available types here. -
Read the
cynicdocumentation to learn how to work with it, particularly when it comes to passing arguments to the query.
Functions§
- register_
schema - Register the schema to enable building custom queries using cynic derive
macros queries. Call this function in a
build.rsfile in your crate if you need to build custom queries.