Skip to content

Commit

Permalink
sui-graphql-client: add suins queries (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
stefan-mysten authored Oct 30, 2024
1 parent 7712ef4 commit 12a24f9
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 0 deletions.
38 changes: 38 additions & 0 deletions crates/sui-graphql-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ use query_types::CheckpointsQuery;
use query_types::CoinMetadata;
use query_types::CoinMetadataArgs;
use query_types::CoinMetadataQuery;
use query_types::DefaultSuinsNameQuery;
use query_types::DefaultSuinsNameQueryArgs;
use query_types::DryRunArgs;
use query_types::DryRunQuery;
use query_types::DynamicFieldArgs;
Expand Down Expand Up @@ -48,6 +50,8 @@ use query_types::PageInfo;
use query_types::ProtocolConfigQuery;
use query_types::ProtocolConfigs;
use query_types::ProtocolVersionArgs;
use query_types::ResolveSuinsQuery;
use query_types::ResolveSuinsQueryArgs;
use query_types::ServiceConfig;
use query_types::ServiceConfigQuery;
use query_types::TransactionBlockArgs;
Expand Down Expand Up @@ -1361,6 +1365,40 @@ impl Client {

Ok(response.data.and_then(|p| p.package).and_then(|p| p.module))
}

// ===========================================================================
// SuiNS
// ===========================================================================

/// Get the address for the provided Suins domain name.
pub async fn resolve_suins_to_address(&self, domain: &str) -> Result<Option<Address>, Error> {
let operation = ResolveSuinsQuery::build(ResolveSuinsQueryArgs { name: domain });

let response = self.run_query(&operation).await?;

if let Some(errors) = response.errors {
return Err(Error::msg(format!("{:?}", errors)));
}
Ok(response
.data
.and_then(|d| d.resolve_suins_address)
.map(|a| a.address))
}

/// Get the default Suins domain name for the provided address.
pub async fn default_suins_name(&self, address: Address) -> Result<Option<String>, Error> {
let operation = DefaultSuinsNameQuery::build(DefaultSuinsNameQueryArgs { address });

let response = self.run_query(&operation).await?;

if let Some(errors) = response.errors {
return Err(Error::msg(format!("{:?}", errors)));
}
Ok(response
.data
.and_then(|d| d.address)
.and_then(|a| a.default_suins_name))
}
}

// This function is used in tests to create a new client instance for the local server.
Expand Down
5 changes: 5 additions & 0 deletions crates/sui-graphql-client/src/query_types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ mod normalized_move;
mod object;
mod protocol_config;
mod service_config;
mod suins;
mod transaction;

pub use active_validators::ActiveValidatorsArgs;
Expand Down Expand Up @@ -80,6 +81,10 @@ pub use protocol_config::ProtocolVersionArgs;
pub use service_config::Feature;
pub use service_config::ServiceConfig;
pub use service_config::ServiceConfigQuery;
pub use suins::DefaultSuinsNameQuery;
pub use suins::DefaultSuinsNameQueryArgs;
pub use suins::ResolveSuinsQuery;
pub use suins::ResolveSuinsQueryArgs;
pub use transaction::TransactionBlock;
pub use transaction::TransactionBlockArgs;
pub use transaction::TransactionBlockQuery;
Expand Down
53 changes: 53 additions & 0 deletions crates/sui-graphql-client/src/query_types/suins.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright (c) Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

// ===========================================================================
// Suins Queries
// ===========================================================================

use crate::query_types::schema;
use crate::query_types::Address as SdkAddress;

#[derive(cynic::QueryFragment, Debug)]
#[cynic(
schema = "rpc",
graphql_type = "Query",
variables = "ResolveSuinsQueryArgs"
)]
pub struct ResolveSuinsQuery {
#[arguments(domain: $name)]
pub resolve_suins_address: Option<DomainAddress>,
}

#[derive(cynic::QueryVariables, Debug)]
pub struct ResolveSuinsQueryArgs<'a> {
pub name: &'a str,
}

#[derive(cynic::QueryFragment, Debug)]
#[cynic(schema = "rpc", graphql_type = "Address")]
pub struct DomainAddress {
pub address: SdkAddress,
}

#[derive(cynic::QueryFragment, Debug)]
#[cynic(
schema = "rpc",
graphql_type = "Query",
variables = "DefaultSuinsNameQueryArgs"
)]
pub struct DefaultSuinsNameQuery {
#[arguments(address: $address)]
pub address: Option<AddressDefaultSuins>,
}

#[derive(cynic::QueryVariables, Debug)]
pub struct DefaultSuinsNameQueryArgs {
pub address: SdkAddress,
}

#[derive(cynic::QueryFragment, Debug)]
#[cynic(schema = "rpc", graphql_type = "Address")]
pub struct AddressDefaultSuins {
pub default_suins_name: Option<String>,
}

0 comments on commit 12a24f9

Please sign in to comment.