Skip to content

Commit

Permalink
Call new graphql client in example
Browse files Browse the repository at this point in the history
  • Loading branch information
darunrs committed May 20, 2024
1 parent d06e33b commit b6b01fc
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use graphql_client::{GraphQLQuery, Response};
use std::error::Error;
use ::reqwest::Client;
use block_streamer::graphql;

#[allow(clippy::upper_case_acronyms)]
type URI = String;
Expand All @@ -16,8 +17,6 @@ type Date = String;
struct GetBitmaps;

async fn perform_my_query(variables: get_bitmaps::Variables) -> Result<(), Box<dyn Error>> {

// this is the important line
let request_body = GetBitmaps::build_query(variables);

let client = reqwest::Client::new();
Expand All @@ -29,11 +28,13 @@ async fn perform_my_query(variables: get_bitmaps::Variables) -> Result<(), Box<d

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
let variables = get_bitmaps::Variables {
receiver_ids: Some(".*".to_string()),
block_date: Some("2024-03-21".to_string()),
limit: Some(100),
offset: Some(0),
};
perform_my_query(variables).await
// let variables = get_bitmaps::Variables {
// receiver_ids: Some(".*".to_string()),
// block_date: Some("2024-03-21".to_string()),
// limit: Some(100),
// offset: Some(0),
// };
// perform_my_query(variables).await
let client = graphql::GraphqlClient::new("https://queryapi-hasura-graphql-mainnet-vcqilefdcq-ew.a.run.app/v1/graphql".to_string());
client.get_bitmaps("app.nearcrowd.near".to_string(), "2024-03-21".to_string(), 100, 0).await
}
36 changes: 24 additions & 12 deletions block-streamer/src/graphql.rs
Original file line number Diff line number Diff line change
@@ -1,34 +1,46 @@
use graphql_client::{GraphQLQuery, Response};
use std::error::Error;
use ::reqwest;

const HASURA_ACCOUNT: &str = "darunrs_near";

#[allow(clippy::upper_case_acronyms)]
type URI = String;
type Date = String;

#[derive(GraphQLQuery)]
#[graphql(
schema_path = "graphql/darunrs_near/schema.json",
query_path = "graphql/darunrs_near/get_bitmap_by_date.graphql",
query_path = "graphql/darunrs_near/get_bitmaps.graphql",
response_derives = "Debug",
normalization = "rust"
)]
struct GetBitmapsByDateAndId;
struct GetBitmaps;

pub struct GraphqlClient {
client: reqwest::Client,
graphql_endpoint: URI,
graphql_endpoint: String,
}

#[cfg(not(test))]
pub use GraphqlClientImpl as GraphqlClient;
#[cfg(test)]
pub use MockGraphqlClientImpl as GraphqlClient;

#[cfg_attr(test, mockall::automock)]
impl GraphqlClientImpl {
pub fn new (graphql_endpoint: URI) -> Self {
impl GraphqlClient {
pub fn new(graphql_endpoint: String) -> Self {
Self {
client: reqwest::Client::new(),
graphql_endpoint,
}
}


pub async fn get_bitmaps(&self, receiver_ids: String, block_date: String, limit: i64, offset: i64) -> Result<(), Box<dyn Error>> {
let variables = get_bitmaps::Variables {
receiver_ids: Some(receiver_ids),
block_date: Some(block_date),
limit: Some(limit),
offset: Some(offset),
};
let request_body = GetBitmaps::build_query(variables);
let res = self.client.post(&self.graphql_endpoint).header("x-hasura-role", HASURA_ACCOUNT).json(&request_body).send().await?;
let response_body: Response<get_bitmaps::ResponseData> = res.json().await?;
println!("{:#?}", response_body);
Ok(())
}
}
1 change: 1 addition & 0 deletions block-streamer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ mod blockstreamer {
}

pub use blockstreamer::*;
pub mod graphql;
1 change: 1 addition & 0 deletions block-streamer/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use tracing_subscriber::prelude::*;

mod block_stream;
mod delta_lake_client;
mod graphql;
mod indexer_config;
mod lake_s3_client;
mod metrics;
Expand Down

0 comments on commit b6b01fc

Please sign in to comment.