-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
sui-graphql-client: add a build crate to allow registering schema in …
…other crates (#57)
- Loading branch information
1 parent
43090fc
commit 5e1a0dd
Showing
7 changed files
with
106 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
``` |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} |