Skip to content

Commit e3d2ed8

Browse files
committed
graph, graphql, store: Make SQL queries configurable (default: off)
1 parent 010f194 commit e3d2ed8

File tree

6 files changed

+58
-1
lines changed

6 files changed

+58
-1
lines changed

docs/environment-variables.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,3 +284,6 @@ those.
284284
graph-node bugs, but since it is hard to work around them, setting this
285285
variable to something like 10 makes it possible to work around such a bug
286286
while it is being fixed (default: 0)
287+
- `GRAPH_ENABLE_SQL_QUERIES`: Enable the experimental [SQL query
288+
interface](implementation/sql-interface.md).
289+
(default: false)

docs/implementation/sql-interface.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
interface will ever be brought to production use. It's solely here to help
55
evaluate the utility of such an interface**
66

7+
**The interface is only available if the environment variable `GRAPH_ENABLE_SQL_QUERIES` is set to `true`**
8+
79
SQL queries can be issued by posting a JSON document to
810
`/subgraphs/sql`. The server will respond with a JSON response that
911
contains the records matching the query in JSON form.

graph/src/env/mod.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ lazy_static! {
2424
#[cfg(debug_assertions)]
2525
lazy_static! {
2626
pub static ref TEST_WITH_NO_REORG: Mutex<bool> = Mutex::new(false);
27+
pub static ref TEST_SQL_QUERIES_ENABLED: Mutex<bool> = Mutex::new(false);
2728
}
2829

2930
/// Panics if:
@@ -189,6 +190,10 @@ pub struct EnvVars {
189190
/// Set by the environment variable `ETHEREUM_REORG_THRESHOLD`. The default
190191
/// value is 250 blocks.
191192
reorg_threshold: BlockNumber,
193+
/// Enable SQL query interface. SQL queries are disabled by default
194+
/// because they are still experimental. Set by the environment variable
195+
/// `GRAPH_ENABLE_SQL_QUERIES`. Off by default.
196+
enable_sql_queries: bool,
192197
/// The time to wait between polls when using polling block ingestor.
193198
/// The value is set by `ETHERUM_POLLING_INTERVAL` in millis and the
194199
/// default is 1000.
@@ -341,6 +346,7 @@ impl EnvVars {
341346
external_ws_base_url: inner.external_ws_base_url,
342347
static_filters_threshold: inner.static_filters_threshold,
343348
reorg_threshold: inner.reorg_threshold,
349+
enable_sql_queries: inner.enable_sql_queries.0,
344350
ingestor_polling_interval: Duration::from_millis(inner.ingestor_polling_interval),
345351
subgraph_settings: inner.subgraph_settings,
346352
prefer_substreams_block_streams: inner.prefer_substreams_block_streams,
@@ -414,6 +420,27 @@ impl EnvVars {
414420
pub fn reorg_threshold(&self) -> i32 {
415421
self.reorg_threshold
416422
}
423+
424+
#[cfg(debug_assertions)]
425+
pub fn sql_queries_enabled(&self) -> bool {
426+
// SQL queries are disabled by default for security.
427+
// For testing purposes, we allow tests to enable SQL queries via TEST_SQL_QUERIES_ENABLED.
428+
if *TEST_SQL_QUERIES_ENABLED.lock().unwrap() {
429+
true
430+
} else {
431+
self.enable_sql_queries
432+
}
433+
}
434+
#[cfg(not(debug_assertions))]
435+
pub fn sql_queries_enabled(&self) -> bool {
436+
self.enable_sql_queries
437+
}
438+
439+
#[cfg(debug_assertions)]
440+
pub fn enable_sql_queries_for_tests(&self, enable: bool) {
441+
let mut lock = TEST_SQL_QUERIES_ENABLED.lock().unwrap();
442+
*lock = enable;
443+
}
417444
}
418445

419446
impl Default for EnvVars {
@@ -514,6 +541,8 @@ struct Inner {
514541
// JSON-RPC specific.
515542
#[envconfig(from = "ETHEREUM_REORG_THRESHOLD", default = "250")]
516543
reorg_threshold: BlockNumber,
544+
#[envconfig(from = "GRAPH_ENABLE_SQL_QUERIES", default = "false")]
545+
enable_sql_queries: EnvVarBoolean,
517546
#[envconfig(from = "ETHEREUM_POLLING_INTERVAL", default = "1000")]
518547
ingestor_polling_interval: u64,
519548
#[envconfig(from = "GRAPH_EXPERIMENTAL_SUBGRAPH_SETTINGS")]

graphql/src/runner.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,14 @@ where
258258
self: Arc<Self>,
259259
req: SqlQueryReq,
260260
) -> Result<Vec<SqlQueryObject>, QueryExecutionError> {
261+
// Check if SQL queries are enabled
262+
if !ENV_VARS.sql_queries_enabled() {
263+
return Err(QueryExecutionError::SqlError(
264+
"SQL queries are disabled. Set GRAPH_ENABLE_SQL_QUERIES=true to enable."
265+
.to_string(),
266+
));
267+
}
268+
261269
let store = self
262270
.store
263271
.query_store(QueryTarget::Deployment(

store/postgres/src/query_store.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,14 @@ impl QueryStoreTrait for QueryStore {
6767
&self,
6868
sql: &str,
6969
) -> Result<Vec<SqlQueryObject>, graph::prelude::QueryExecutionError> {
70+
// Check if SQL queries are enabled
71+
if !ENV_VARS.sql_queries_enabled() {
72+
return Err(QueryExecutionError::SqlError(
73+
"SQL queries are disabled. Set GRAPH_ENABLE_SQL_QUERIES=true to enable."
74+
.to_string(),
75+
));
76+
}
77+
7078
let mut conn = self
7179
.store
7280
.get_replica_conn(self.replica_id)

store/test-store/tests/graphql/sql.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ use graph::prelude::{r, QueryExecutionError};
88
use std::collections::BTreeSet;
99
use test_store::{run_test_sequentially, STORE};
1010

11+
#[cfg(debug_assertions)]
12+
use graph::env::ENV_VARS;
13+
1114
// Import test setup from query.rs module
1215
use super::query::{setup, IdType};
1316

@@ -18,6 +21,8 @@ where
1821
{
1922
let sql = sql.to_string(); // Convert to owned String
2023
run_test_sequentially(move |store| async move {
24+
ENV_VARS.enable_sql_queries_for_tests(true);
25+
2126
for id_type in [IdType::String, IdType::Bytes, IdType::Int8] {
2227
let name = id_type.deployment_id();
2328
let deployment = setup(store.as_ref(), name, BTreeSet::new(), id_type).await;
@@ -33,7 +38,9 @@ where
3338
let result = query_store.execute_sql(&sql);
3439
test(result, id_type);
3540
}
36-
})
41+
42+
ENV_VARS.enable_sql_queries_for_tests(false);
43+
});
3744
}
3845

3946
#[test]

0 commit comments

Comments
 (0)