Skip to content

Commit dcd0ca9

Browse files
committed
core,node,server: add graphman config place command to graphql api
1 parent cd62922 commit dcd0ca9

File tree

9 files changed

+110
-1
lines changed

9 files changed

+110
-1
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
pub mod check;
2+
pub mod place;
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
use anyhow::{anyhow, Error as AnyError};
2+
use graph_store_postgres::DeploymentPlacer;
3+
use thiserror::Error;
4+
5+
pub struct PlaceResult {
6+
pub shards: Vec<String>,
7+
pub nodes: Vec<String>,
8+
}
9+
10+
#[derive(Debug, Error)]
11+
pub enum PlaceError {
12+
#[error("No matching placement rule; default placement from JSON RPC call would be used")]
13+
NoPlacementRule,
14+
15+
#[error(transparent)]
16+
Common(#[from] AnyError),
17+
}
18+
19+
pub fn place(
20+
placer: &dyn DeploymentPlacer,
21+
name: &str,
22+
network: &str,
23+
) -> Result<PlaceResult, PlaceError> {
24+
match placer.place(name, network).map_err(|s| anyhow!(s))? {
25+
None => Err(PlaceError::NoPlacementRule),
26+
Some((shards, nodes)) => {
27+
let nodes: Vec<_> = nodes.into_iter().map(|n| n.to_string()).collect();
28+
let shards: Vec<_> = shards.into_iter().map(|s| s.to_string()).collect();
29+
Ok(PlaceResult { shards, nodes })
30+
}
31+
}
32+
}

node/src/bin/manager.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1182,7 +1182,7 @@ async fn main() -> anyhow::Result<()> {
11821182
Ok(())
11831183
}
11841184
Place { name, network } => {
1185-
commands::config::place(&ctx.config.deployment, &name, &network)
1185+
commands::config_cmd::place::run(&ctx.config.deployment, &name, &network)
11861186
}
11871187
Check { print } => commands::config_cmd::check::run(&ctx.config, print),
11881188
Pools { nodes, shard } => commands::config::pools(&ctx.config, nodes, shard),
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
pub mod check;
2+
pub mod place;
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
use anyhow::Error;
2+
use graph_store_postgres::DeploymentPlacer;
3+
use graphman::commands::config::place::{place, PlaceError};
4+
5+
pub fn run(placer: &dyn DeploymentPlacer, name: &String, network: &String) -> Result<(), Error> {
6+
let res = place(placer, name, network);
7+
match res {
8+
Ok(result) => {
9+
println!("subgraph: {}", name);
10+
println!("network: {}", network);
11+
println!("shard: {}", result.shards.join(", "));
12+
println!("nodes: {}", result.nodes.join(", "));
13+
}
14+
Err(PlaceError::NoPlacementRule) => {
15+
println!("{}", PlaceError::NoPlacementRule);
16+
}
17+
Err(PlaceError::Common(e)) => {
18+
println!("Error: {}", e.to_string());
19+
}
20+
};
21+
Ok(())
22+
}

server/graphman/src/entities/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ mod deployment_version_selector;
1010
mod empty_response;
1111
mod execution;
1212
mod execution_id;
13+
mod place_response;
1314
mod subgraph_health;
1415

1516
pub use self::block_hash::BlockHash;
@@ -24,4 +25,5 @@ pub use self::deployment_version_selector::DeploymentVersionSelector;
2425
pub use self::empty_response::EmptyResponse;
2526
pub use self::execution::Execution;
2627
pub use self::execution_id::ExecutionId;
28+
pub use self::place_response::PlaceResponse;
2729
pub use self::subgraph_health::SubgraphHealth;
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
use async_graphql::SimpleObject;
2+
3+
#[derive(Clone, Debug, SimpleObject)]
4+
pub struct PlaceResponse {
5+
subgraph: String,
6+
network: String,
7+
shards: Vec<String>,
8+
nodes: Vec<String>,
9+
}
10+
11+
impl PlaceResponse {
12+
pub fn from(
13+
subgraph: String,
14+
network: String,
15+
shards: Vec<String>,
16+
nodes: Vec<String>,
17+
) -> Self {
18+
Self {
19+
subgraph,
20+
network,
21+
shards,
22+
nodes,
23+
}
24+
}
25+
}

server/graphman/src/resolvers/config_query.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ use async_graphql::Object;
22
use async_graphql::Result;
33

44
use crate::entities::ConfigCheckResponse;
5+
use crate::entities::PlaceResponse;
56

67
mod check;
8+
mod place;
79
pub struct ConfigQuery;
810

911
#[Object]
@@ -12,4 +14,13 @@ impl ConfigQuery {
1214
pub async fn check(&self) -> Result<ConfigCheckResponse> {
1315
check::run()
1416
}
17+
18+
/// Returns how a specific subgraph would be placed
19+
pub async fn place(
20+
&self,
21+
#[graphql(desc = "Subgraph name")] subgraph: String,
22+
#[graphql(desc = "Network name")] network: String,
23+
) -> Result<PlaceResponse> {
24+
place::run(subgraph, network).await
25+
}
1526
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
use async_graphql::Result;
2+
use graphman::commands::config::place::place;
3+
4+
use crate::entities::PlaceResponse;
5+
6+
use super::check::fetch_config;
7+
8+
pub async fn run(subgraph: String, network: String) -> Result<PlaceResponse> {
9+
let config = fetch_config()?;
10+
let res = place(&config.deployment, &subgraph, &network)?;
11+
12+
Ok(PlaceResponse::from(
13+
subgraph, network, res.shards, res.nodes,
14+
))
15+
}

0 commit comments

Comments
 (0)