Skip to main content

iota_sdk_graphql_client_build/
lib.rs

1// Copyright (c) Mysten Labs, Inc.
2// Modifications Copyright (c) 2025 IOTA Stiftung
3// SPDX-License-Identifier: Apache-2.0
4
5#![doc = include_str!("../README.md")]
6
7/// Register the schema to enable building custom queries using cynic derive
8/// macros queries. Call this function in a `build.rs` file in your crate if you
9/// need to build custom queries.
10///
11/// Examples
12/// ```rust,ignore
13/// // build.rs file
14/// fn main() {
15///     let schema_name = "MYSCHEMA"
16///     iota_graphql_client_build::register_schema(schema_name);
17/// }
18///
19/// // Cargo.toml
20/// ...
21/// [dependencies]
22/// cynic = "3.8.0"
23/// ...
24/// [build-dependencies]
25/// iota_graphql_client_build = "VERSION_HERE"
26///
27/// // lib.rs
28/// // Custom query
29/// use cynic::QueryBuilder;
30/// use iota_graphql_client::{query_types::schema, Client};
31///
32/// #[derive(cynic::QueryFragment, Debug)]
33/// #[cynic(schema = "MYSCHEMA", graphql_type = "Query")]
34/// pub struct MyQuery {
35///    pub chain_identifier: String,
36/// }
37///
38/// #[tokio::main]
39/// async fn main() {
40///     let client = Client::new_mainnet();
41///     let operation = MyQuery::build(());
42///     let q = client.run_query(&operation).await.unwrap();
43///     println!("{:?}", q);
44/// }
45/// ```
46pub fn register_schema(schema_name: &str) {
47    let sdl = include_str!("../schema.graphql");
48    cynic_codegen::register_schema(schema_name)
49        .from_sdl(sdl)
50        .expect("Failed to find GraphQL Schema")
51        .as_default()
52        .unwrap();
53}