Skip to content

Commit

Permalink
refactor: move graphql queries to own crate
Browse files Browse the repository at this point in the history
Signed-off-by: Gustavo Inacio <[email protected]>
  • Loading branch information
gusinacio committed Nov 13, 2024
1 parent 24b558c commit a38439f
Show file tree
Hide file tree
Showing 28 changed files with 170 additions and 110 deletions.
13 changes: 13 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ members = [
"crates/common",
"crates/config",
"crates/dips",
"crates/query",
"crates/service",
"crates/tap-agent",
]
Expand Down
9 changes: 5 additions & 4 deletions crates/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ edition = "2021"

[dependencies]
indexer-config = { path = "../config" }
indexer-query = { path = "../query" }
thiserror.workspace = true
async-trait.workspace = true
alloy.workspace = true
Expand All @@ -29,14 +30,14 @@ tokio = { workspace = true, features = ["fs", "tokio-macros"] }
cost-model = { git = "https://github.com/graphprotocol/agora", rev = "3ed34ca" }
regex = "1.7.1"
axum-extra = { version = "0.9.3", features = [
"typed-header",
"typed-header",
], default-features = false }
autometrics = { version = "1.0.1", features = ["prometheus-exporter"] }
tower_governor = "0.4.0"
tower-http = { version = "0.5.2", features = [
"cors",
"normalize-path",
"trace",
"cors",
"normalize-path",
"trace",
] }
tokio-util = "0.7.10"
bip39 = "2.0.0"
Expand Down
16 changes: 2 additions & 14 deletions crates/common/src/allocations/monitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,11 @@ use std::{

use super::Allocation;
use crate::{prelude::SubgraphClient, watcher::new_watcher};
use alloy::primitives::{TxHash, B256, U256};
use graphql_client::GraphQLQuery;
use alloy::primitives::TxHash;
use indexer_query::allocations_query::{self, AllocationsQuery};
use thegraph_core::{Address, DeploymentId};
use tokio::sync::watch::Receiver;

type BigInt = U256;
type Bytes = B256;

#[derive(GraphQLQuery)]
#[graphql(
schema_path = "../graphql/network.schema.graphql",
query_path = "../graphql/allocations.query.graphql",
response_derives = "Debug",
variables_derives = "Clone"
)]
pub struct AllocationsQuery;

impl TryFrom<allocations_query::AllocationFragment> for Allocation {
type Error = anyhow::Error;

Expand Down
13 changes: 1 addition & 12 deletions crates/common/src/attestations/dispute_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,10 @@ use crate::subgraph_client::SubgraphClient;
use crate::watcher::new_watcher;
use alloy::primitives::Address;
use anyhow::Error;
use graphql_client::GraphQLQuery;
use indexer_query::dispute_manager::{self, DisputeManager};
use std::time::Duration;
use tokio::sync::watch::Receiver;

type Bytes = Address;

#[derive(GraphQLQuery)]
#[graphql(
schema_path = "../graphql/network.schema.graphql",
query_path = "../graphql/dispute.query.graphql",
response_derives = "Debug",
variables_derives = "Clone"
)]
struct DisputeManager;

pub async fn dispute_manager(
network_subgraph: &'static SubgraphClient,
interval: Duration,
Expand Down
15 changes: 2 additions & 13 deletions crates/common/src/escrow_accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::{

use alloy::primitives::{Address, U256};
use anyhow::{anyhow, Result};
use graphql_client::GraphQLQuery;
use indexer_query::escrow_account::{self, EscrowAccountQuery};
use thiserror::Error;
use tokio::sync::watch::Receiver;
use tracing::{error, warn};
Expand Down Expand Up @@ -87,17 +87,6 @@ impl EscrowAccounts {
}
}

type BigInt = String;

#[derive(GraphQLQuery)]
#[graphql(
schema_path = "../graphql/tap.schema.graphql",
query_path = "../graphql/escrow_account.query.graphql",
response_derives = "Debug",
variables_derives = "Clone"
)]
pub struct EscrowAccountQuery;

pub async fn escrow_accounts(
escrow_subgraph: &'static SubgraphClient,
indexer_address: Address,
Expand All @@ -121,7 +110,7 @@ async fn get_escrow_accounts(
// isAuthorized == true means that the signer is still authorized to sign
// payments in the name of the sender.
let response = escrow_subgraph
.query::<EscrowAccountQuery, _>(escrow_account_query::Variables {
.query::<EscrowAccountQuery, _>(escrow_account::Variables {
indexer: format!("{:x?}", indexer_address),
thaw_end_timestamp: if reject_thawing_signers {
U256::ZERO.to_string()
Expand Down
9 changes: 1 addition & 8 deletions crates/common/src/indexer_service/http/health.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,11 @@ use axum::{
};
use graphql_client::GraphQLQuery;
use indexer_config::GraphNodeConfig;
use indexer_query::{health_query, HealthQuery};
use reqwest::StatusCode;
use serde_json::json;
use thiserror::Error;

#[derive(GraphQLQuery)]
#[graphql(
schema_path = "../graphql/indexing_status.schema.graphql",
query_path = "../graphql/subgraph_health.query.graphql",
response_derives = "Debug",
variables_derives = "Clone"
)]
pub struct HealthQuery;

#[derive(Debug, Error)]
pub enum CheckHealthError {
Expand Down
19 changes: 1 addition & 18 deletions crates/common/src/subgraph_client/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,7 @@ impl SubgraphClient {
mod test {
use std::str::FromStr;

use indexer_query::{current_epoch, user_query, CurrentEpoch, UserQuery};
use serde_json::json;
use wiremock::matchers::{method, path};
use wiremock::{Mock, MockServer, ResponseTemplate};
Expand Down Expand Up @@ -356,15 +357,6 @@ mod test {
.await
}

#[derive(GraphQLQuery)]
#[graphql(
schema_path = "../graphql/network.schema.graphql",
query_path = "../graphql/epoch.query.graphql",
response_derives = "Debug",
variables_derives = "Clone"
)]
struct CurrentEpoch;

#[tokio::test]
#[ignore = "depends on the defunct hosted-service"]
async fn test_network_query() {
Expand All @@ -380,15 +372,6 @@ mod test {
assert!(result.is_ok());
}

#[derive(GraphQLQuery)]
#[graphql(
schema_path = "../graphql/test.schema.graphql",
query_path = "../graphql/user.query.graphql",
response_derives = "Debug",
variables_derives = "Clone"
)]
struct UserQuery;

#[tokio::test]
async fn test_uses_local_deployment_if_healthy_and_synced() {
let deployment =
Expand Down
14 changes: 4 additions & 10 deletions crates/common/src/subgraph_client/monitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,18 @@

use std::time::Duration;

use deployment_status_query::Health;
use graphql_client::GraphQLQuery;
use indexer_query::{
deployment_status_query::{self, Health},
DeploymentStatusQuery,
};
use reqwest::Url;
use serde::Deserialize;
use thegraph_core::DeploymentId;
use tokio::sync::watch::Receiver;

use crate::watcher::new_watcher;

#[derive(GraphQLQuery)]
#[graphql(
schema_path = "../graphql/indexing_status.schema.graphql",
query_path = "../graphql/subgraph_deployment_status.graphql",
response_derives = "Debug",
variables_derives = "Clone"
)]
pub struct DeploymentStatusQuery;

#[derive(Clone, Debug, Deserialize, Eq, PartialEq)]
pub struct DeploymentStatus {
pub synced: bool,
Expand Down
11 changes: 11 additions & 0 deletions crates/query/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "indexer-query"
version = "0.1.0"
edition = "2021"

[dependencies]
graphql_client.workspace = true
thegraph-core.workspace = true
serde.workspace = true
alloy.workspace = true
anyhow.workspace = true
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
122 changes: 122 additions & 0 deletions crates/query/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
use graphql_client::GraphQLQuery;

pub mod dispute_manager {
use alloy::primitives::Address;
use graphql_client::GraphQLQuery;
type Bytes = Address;

#[derive(GraphQLQuery)]
#[graphql(
schema_path = "graphql/network.schema.graphql",
query_path = "graphql/dispute.query.graphql",
response_derives = "Debug",
variables_derives = "Clone"
)]
pub struct DisputeManager;

pub use dispute_manager::Variables;
}

pub mod escrow_account {
use graphql_client::GraphQLQuery;
type BigInt = String;

#[derive(GraphQLQuery)]
#[graphql(
schema_path = "graphql/tap.schema.graphql",
query_path = "graphql/escrow_account.query.graphql",
response_derives = "Debug",
variables_derives = "Clone"
)]
pub struct EscrowAccountQuery;

pub use escrow_account_query::Variables;
}

pub mod allocations_query {
use alloy::primitives::{B256, U256};
use graphql_client::GraphQLQuery;
type BigInt = U256;
type Bytes = B256;

#[derive(GraphQLQuery)]
#[graphql(
schema_path = "graphql/network.schema.graphql",
query_path = "graphql/allocations.query.graphql",
response_derives = "Debug",
variables_derives = "Clone"
)]
pub struct AllocationsQuery;

pub use allocations_query::*;
}

#[derive(GraphQLQuery)]
#[graphql(
schema_path = "graphql/indexing_status.schema.graphql",
query_path = "graphql/subgraph_health.query.graphql",
response_derives = "Debug",
variables_derives = "Clone"
)]
pub struct HealthQuery;

#[derive(GraphQLQuery)]
#[graphql(
schema_path = "graphql/network.schema.graphql",
query_path = "graphql/epoch.query.graphql",
response_derives = "Debug",
variables_derives = "Clone"
)]
pub struct CurrentEpoch;

#[derive(GraphQLQuery)]
#[graphql(
schema_path = "graphql/test.schema.graphql",
query_path = "graphql/user.query.graphql",
response_derives = "Debug",
variables_derives = "Clone"
)]
pub struct UserQuery;

#[derive(GraphQLQuery)]
#[graphql(
schema_path = "graphql/indexing_status.schema.graphql",
query_path = "graphql/subgraph_deployment_status.graphql",
response_derives = "Debug",
variables_derives = "Clone"
)]
pub struct DeploymentStatusQuery;

#[derive(GraphQLQuery)]
#[graphql(
schema_path = "graphql/tap.schema.graphql",
query_path = "graphql/unfinalized_tx.query.graphql",
response_derives = "Debug",
variables_derives = "Clone"
)]
pub struct UnfinalizedTransactions;

pub mod closed_allocations {
use graphql_client::GraphQLQuery;

type Bytes = String;

#[derive(GraphQLQuery)]
#[graphql(
schema_path = "graphql/network.schema.graphql",
query_path = "graphql/closed_allocations.query.graphql",
response_derives = "Debug",
variables_derives = "Clone"
)]
pub struct ClosedAllocations;
pub use closed_allocations::*;
}

#[derive(GraphQLQuery)]
#[graphql(
schema_path = "graphql/tap.schema.graphql",
query_path = "graphql/transactions.query.graphql",
response_derives = "Debug",
variables_derives = "Clone"
)]
pub struct TapTransactions;
1 change: 1 addition & 0 deletions crates/tap-agent/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ path = "src/main.rs"
[dependencies]
indexer-common = { path = "../common" }
indexer-config = { path = "../config" }
indexer-query = { path = "../query" }
alloy.workspace = true
anyhow.workspace = true
async-trait.workspace = true
Expand Down
Loading

0 comments on commit a38439f

Please sign in to comment.