#graphql-client #schema #sui #queries #build #register #cynic

sui-graphql-client-build

Sui GraphQL RPC Client for the Sui Blockchain

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

Apache-2.0

150KB
4.5K SLoC

GraphQL 4.5K SLoC Rust 8 SLoC // 0.2% comments

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

  1. 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" }
  1. Add a build.rs file in your crate root directory and call the register_schema function in it.
// build.rs file
fn main() {
    let schema_name = "MYSCHEMA";
    sui_graphql_client_build::register_schema(schema_name);
}
  1. Add the cynic and sui-graphql-client dependencies in your Cargo.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" }
  1. 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.

  2. 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);
}
  1. For UInt53, you can use u64 type directly as the sui-graphql-client's schema implements the impl_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

  2. 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