Newer
Older
use graphql_client::reqwest::post_graphql;
use graphql_client::GraphQLQuery;
use crate::*;
// type used in parameters query
#[allow(non_camel_case_types)]
type jsonb = serde_json::Value;
schema_path = "res/indexer-schema.json",
query_path = "res/indexer-queries.graphql"
schema_path = "res/indexer-schema.json",
query_path = "res/indexer-queries.graphql"
#[derive(GraphQLQuery)]
#[graphql(
schema_path = "res/indexer-schema.json",
query_path = "res/indexer-queries.graphql"
)]
pub struct LatestBlock;
#[derive(Clone, Debug)]
pub async fn username_by_pubkey(&self, pubkey: &str) -> anyhow::Result<Option<String>> {
Ok(post_graphql::<IdentityNameByPubkey, _>(
&self.gql_client,
identity_name_by_pubkey::Variables {
pubkey: pubkey.to_string(),
},
)
.await?
.data
.and_then(|data| data.identity.into_iter().next().map(|idty| idty.name)))
}
pub async fn pubkey_by_username(&self, username: &str) -> anyhow::Result<Option<String>> {
Ok(post_graphql::<IdentityPubkeyByName, _>(
&self.gql_client,
identity_pubkey_by_name::Variables {
name: username.to_string(),
},
)
.await?
.data
.and_then(|data| data.identity_by_pk.map(|idty| idty.pubkey)))
}
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/// fetch latest block
pub async fn fetch_latest_block(&self) -> Result<u64, anyhow::Error> {
Ok(post_graphql::<LatestBlock, _>(
&self.gql_client,
self.gql_url.clone(),
latest_block::Variables {},
)
.await?
.data
.unwrap() // must have a data field
.parameters
.first()
.unwrap() // must have one and only one parameter matching request
.value
.clone()
.unwrap() // must have a value field
.as_u64()
.unwrap()) // must be a Number of blocks
}
}
#[derive(Clone, Default, Debug, clap::Parser)]
pub enum IndexerSubcommand {
#[default]
/// Show indexer endpoint
ShowEndpoint,
/// Fetch latest indexed block
LatestBlock,
}
pub async fn handle_command(data: Data, command: IndexerSubcommand) -> anyhow::Result<()> {
let data = data.build_indexer()?;
match command {
IndexerSubcommand::ShowEndpoint => {
println!("indexer endpoint: {}", data.indexer().gql_url);
}
IndexerSubcommand::LatestBlock => {
println!(
"latest indexed block is: {}",
data.indexer().fetch_latest_block().await?
);
}
};
Ok(())