Skip to content

Commit

Permalink
sui-graphql-client: add a build crate to allow registering schema in …
Browse files Browse the repository at this point in the history
…other crates (#57)
  • Loading branch information
stefan-mysten authored Nov 11, 2024
1 parent 43090fc commit 5e1a0dd
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 8 deletions.
8 changes: 8 additions & 0 deletions crates/sui-graphql-client-build/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "sui-graphql-client-build"
version = "0.1.0"
edition = "2021"

[dependencies]
cynic-codegen = "3.8.0"

39 changes: 39 additions & 0 deletions crates/sui-graphql-client-build/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
### 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.

### Example
```rust,ignore
// build.rs file
fn main() {
let schema_name = "MYSCHEMA"
sui_graphql_client_build::register_schema(schema_name);
}
// Cargo.toml
...
[dependencies]
cynic = "3.8.0"
...
[build-dependencies]
sui_graphql_client_build = "VERSION_HERE"
// 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);
}
```
51 changes: 51 additions & 0 deletions crates/sui-graphql-client-build/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright (c) Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

#![doc = include_str!("../README.md")]

/// Register the 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.
///
/// Examples
/// ```rust,ignore
/// // build.rs file
/// fn main() {
/// let schema_name = "MYSCHEMA"
/// sui_graphql_client_build::register_schema(schema_name);
/// }
///
/// // Cargo.toml
/// ...
/// [dependencies]
/// cynic = "3.8.0"
/// ...
/// [build-dependencies]
/// sui_graphql_client_build = "VERSION_HERE"
///
/// // 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);
/// }
/// ```
pub fn register_schema(schema_name: &str) {
let sdl = include_str!("../schema.graphql");
cynic_codegen::register_schema(schema_name)
.from_sdl(sdl)
.expect("Failed to find GraphQL Schema")
.as_default()
.unwrap();
}
2 changes: 1 addition & 1 deletion crates/sui-graphql-client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@ rand = "0.8.5"
tokio = { version = "1.40.0", features = ["full"] }

[build-dependencies]
cynic-codegen = { version = "3.7.3" }
sui_graphql_client_build = { package = "sui-graphql-client-build", path = "../sui-graphql-client-build" }

10 changes: 6 additions & 4 deletions crates/sui-graphql-client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ query CustomQuery($id: UInt53) {
}
```

When using `cynic` and `sui-graphql-client`, you will need to register the schema by calling `sui-graphql-client-build::register_schema` in a `build.rs` file. See [sui-graphql-client-build](https://github.com/MystenLabs/sui-rust-sdk/tree/master/crates/sui-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.

Expand All @@ -125,7 +127,7 @@ pub struct CustomQueryVariables {
}
#[derive(cynic::QueryFragment, Debug)]
#[cynic(graphql_type = "Query", variables = "CustomQueryVariables")]
#[cynic(schema = "SCHEMA_NAME_HERE", graphql_type = "Query", variables = "CustomQueryVariables")]
pub struct CustomQuery {
#[arguments(id: $id)]
pub epoch: Option<Epoch>,
Expand Down Expand Up @@ -157,7 +159,7 @@ use sui_types::types::Address;
// The data returned by the custom query.
#[derive(cynic::QueryFragment, Debug)]
#[cynic(schema = "rpc", graphql_type = "Epoch")]
#[cynic(schema = "SCHEMA_NAME_HERE", graphql_type = "Epoch")]
pub struct EpochData {
pub epoch_id: u64,
pub reference_gas_price: Option<BigInt>,
Expand All @@ -176,15 +178,15 @@ pub struct CustomVariables {
// The custom query. Note that the variables need to be explicitly declared.
#[derive(cynic::QueryFragment, Debug)]
#[cynic(schema = "rpc", graphql_type = "Query", variables = "CustomVariables")]
#[cynic(schema = "SCHEMA_NAME_HERE", graphql_type = "Query", variables = "CustomVariables")]
pub struct CustomQuery {
#[arguments(id: $id)]
pub epoch: Option<EpochData>,
}
// Custom query with no variables.
#[derive(cynic::QueryFragment, Debug)]
#[cynic(schema = "rpc", graphql_type = "Query")]
#[cynic(schema = "SCHEMA_NAME_HERE", graphql_type = "Query")]
pub struct ChainIdQuery {
chain_identifier: String,
}
Expand Down
4 changes: 1 addition & 3 deletions crates/sui-graphql-client/build.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
/// Register Sui RPC schema for creating structs for queries
fn main() {
cynic_codegen::register_schema("rpc")
.from_sdl_file("schema/graphql_rpc.graphql")
.expect("Failed to find GraphQL Schema");
sui_graphql_client_build::register_schema("rpc");
}

0 comments on commit 5e1a0dd

Please sign in to comment.