Skip to content

Commit

Permalink
node: Add graphman query
Browse files Browse the repository at this point in the history
  • Loading branch information
lutter committed Apr 26, 2021
1 parent dea572b commit 1a9f4a7
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 4 deletions.
41 changes: 37 additions & 4 deletions node/src/bin/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@ use std::{collections::HashMap, env, sync::Arc};

use config::PoolSize;
use git_testament::{git_testament, render_testament};
use graph::prometheus::Registry;
use graph::{data::graphql::effort::LoadManager, prometheus::Registry};
use graph_core::MetricsRegistry;
use graph_graphql::prelude::GraphQlRunner;
use lazy_static::lazy_static;
use structopt::StructOpt;

use graph::{
log::logger,
prelude::{info, o, slog, tokio, Logger, NodeId},
};
use graph_node::config;
use graph_node::store_builder::StoreBuilder;
use graph_node::{manager::PanicSubscriptionManager, store_builder::StoreBuilder};
use graph_store_postgres::{
connection_pool::ConnectionPool, Shard, Store, SubgraphStore, SubscriptionManager,
PRIMARY_SHARD,
};

use crate::config::Config as Cfg;
use graph_node::config::{self, Config as Cfg};
use graph_node::manager::commands;

git_testament!(TESTAMENT);
Expand Down Expand Up @@ -136,6 +136,17 @@ pub enum Command {
Listen(ListenCommand),
/// Manage deployment copies and grafts
Copy(CopyCommand),
/// Run a GraphQL query
Query {
/// The subgraph to query
///
/// Either a deployment id `Qm..` or a subgraph name
target: String,
/// The GraphQL query
query: String,
/// The variables in the form `key=value`
vars: Vec<String>,
},
}

impl Command {
Expand Down Expand Up @@ -336,6 +347,23 @@ impl Context {

(store, pools)
}

fn graphql_runner(self) -> Arc<GraphQlRunner<Store, PanicSubscriptionManager>> {
let logger = self.logger.clone();
let registry = self.registry.clone();

let store = self.store();

let subscription_manager = Arc::new(PanicSubscriptionManager);
let load_manager = Arc::new(LoadManager::new(&logger, vec![], registry, 128));

Arc::new(GraphQlRunner::new(
&logger,
store,
subscription_manager,
load_manager,
))
}
}

#[tokio::main]
Expand Down Expand Up @@ -464,6 +492,11 @@ async fn main() {
Status { dst } => commands::copy::status(ctx.pools(), dst),
}
}
Query {
target,
query,
vars,
} => commands::query::run(ctx.graphql_runner(), target, query, vars).await,
};
if let Err(e) = result {
die!("error: {}", e)
Expand Down
1 change: 1 addition & 0 deletions node/src/manager/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pub mod config;
pub mod copy;
pub mod info;
pub mod listen;
pub mod query;
pub mod remove;
pub mod rewind;
pub mod txn_speed;
Expand Down
62 changes: 62 additions & 0 deletions node/src/manager/commands/query.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
use std::iter::FromIterator;
use std::{collections::HashMap, sync::Arc};

use graph::{
data::query::QueryTarget,
prelude::{
anyhow::{self, anyhow},
q, serde_json, GraphQlRunner as _, Query, QueryVariables, SubgraphDeploymentId,
SubgraphName,
},
};
use graph_graphql::prelude::GraphQlRunner;
use graph_store_postgres::Store;

use crate::manager::PanicSubscriptionManager;

pub async fn run(
runner: Arc<GraphQlRunner<Store, PanicSubscriptionManager>>,
target: String,
query: String,
vars: Vec<String>,
) -> Result<(), anyhow::Error> {
let target = if target.starts_with("Qm") {
let id = SubgraphDeploymentId::new(target)
.map_err(|id| anyhow!("illegal deployment id `{}`", id))?;
QueryTarget::Deployment(id)
} else {
let name = SubgraphName::new(target.clone())
.map_err(|()| anyhow!("illegal subgraph name `{}`", target))?;
QueryTarget::Name(name)
};

let document = graphql_parser::parse_query(&query)?.into_static();
let vars: Vec<(String, q::Value)> = vars
.into_iter()
.map(|v| {
let mut pair = v.splitn(2, '=').map(|s| s.to_string());
let key = pair.next();
let value = pair
.next()
.map(|s| q::Value::String(s))
.unwrap_or(q::Value::Null);
match key {
Some(key) => Ok((key, value)),
None => Err(anyhow!(
"malformed variable `{}`, it must be of the form `key=value`",
v
)),
}
})
.collect::<Result<_, _>>()?;
let query = Query::new(
document,
Some(QueryVariables::new(HashMap::from_iter(vars))),
);

let res = runner.run_query(query, target, false).await;
let json = serde_json::to_string(&res)?;
println!("{}", json);

Ok(())
}
14 changes: 14 additions & 0 deletions node/src/manager/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
use graph::{
components::store::SubscriptionManager,
prelude::{StoreEventStreamBox, SubscriptionFilter},
};

pub mod catalog;
pub mod commands;
pub mod deployment;
mod display;

/// A dummy subscription manager that always panics
pub struct PanicSubscriptionManager;

impl SubscriptionManager for PanicSubscriptionManager {
fn subscribe(&self, _: Vec<SubscriptionFilter>) -> StoreEventStreamBox {
panic!("we were never meant to call `subscribe`");
}
}

0 comments on commit 1a9f4a7

Please sign in to comment.