2 releases
new 0.0.3 | Mar 20, 2025 |
---|---|
0.0.0 | Nov 11, 2024 |
#2 in #sui
26 downloads per month
Used in sui-graphql-client
150KB
4.5K
SLoC
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.toml
file.
[build-dependencies]
sui-graphql-client-build = { git = "https://github.com/mystenlabs/sui-rust-sdk", package = "sui-graphql-client-build", branch = "master" }
- Add a
build.rs
file in your crate root directory and call theregister_schema
function in it.
// build.rs file
fn main() {
let schema_name = "MYSCHEMA";
sui_graphql_client_build::register_schema(schema_name);
}
- Add the
cynic
andsui-graphql-client
dependencies in yourCargo.toml
file. You should have something like this.
# Cargo.toml
# ...
[dependencies]
cynic = "3.8.0"
sui-graphql-client = { git = "https://github.com/mystenlabs/sui-rust-sdk", package = "sui-graphql-client", branch = "master" }
[build-dependencies]
sui-graphql-client-build = { git = "https://github.com/mystenlabs/sui-rust-sdk", package = "sui-graphql-client-build", branch = "master" }
-
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 sui_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 useu64
type directly as thesui-graphql-client
's schema implements theimpl_scalar
. Similarly for other types (Base64, DateTime). See more available types here: https://github.com/MystenLabs/sui-rust-sdk/blob/02639f6b09375fe03fa2243868be17bec1dfa33c/crates/sui-graphql-client/src/query_types/mod.rs?plain=1#L124-L126 -
Read the
cynic
documentation to learn how to work with it, particularly when it comes to passing arguments to the query.
Dependencies
~5.5MB
~84K SLoC