Skip to content

Commit

Permalink
Merge pull request #24 from semiotic-ai/gustavo/sql-2-create-flag-tha…
Browse files Browse the repository at this point in the history
…t-enables-sql-service

feat: add opt-in sql service environment variable
  • Loading branch information
gusinacio authored Feb 6, 2024
2 parents 1ba3db3 + 17c229f commit 78f5edd
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 3 deletions.
1 change: 1 addition & 0 deletions docs/environment-variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ those.
`X-GraphTraceQuery` set to this value will include a trace of the SQL
queries that were run. Defaults to the empty string which disables
tracing.
- `GRAPH_GRAPHQL_ENABLE_SQL_SERVICE`: enables the sql service integration. This allows clients to execute `sql()` operations on subgraphs.

### GraphQL caching

Expand Down
6 changes: 6 additions & 0 deletions graph/src/env/graphql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ pub struct EnvVarsGraphQl {
/// Set by the flag `GRAPH_GRAPHQL_DISABLE_BOOL_FILTERS`. Off by default.
/// Disables AND/OR filters
pub disable_bool_filters: bool,
/// Set by the flag `GRAPH_GRAPHQL_ENABLE_SQL_SERVICE`. Off by default.
/// Enables queries on the `sql()` field of the root query
pub enable_sql_service: bool,
/// Set by the flag `GRAPH_GRAPHQL_DISABLE_CHILD_SORTING`. Off by default.
/// Disables child-based sorting
pub disable_child_sorting: bool,
Expand Down Expand Up @@ -136,6 +139,7 @@ impl From<InnerGraphQl> for EnvVarsGraphQl {
error_result_size: x.error_result_size.0 .0,
max_operations_per_connection: x.max_operations_per_connection,
disable_bool_filters: x.disable_bool_filters.0,
enable_sql_service: x.enable_sql_service.0,
disable_child_sorting: x.disable_child_sorting.0,
query_trace_token: x.query_trace_token,
}
Expand Down Expand Up @@ -185,6 +189,8 @@ pub struct InnerGraphQl {
pub disable_bool_filters: EnvVarBoolean,
#[envconfig(from = "GRAPH_GRAPHQL_DISABLE_CHILD_SORTING", default = "false")]
pub disable_child_sorting: EnvVarBoolean,
#[envconfig(from = "GRAPH_GRAPHQL_ENABLE_SQL_SERVICE", default = "false")]
pub enable_sql_service: EnvVarBoolean,
#[envconfig(from = "GRAPH_GRAPHQL_TRACE_TOKEN", default = "")]
query_trace_token: String,
}
14 changes: 11 additions & 3 deletions graph/src/schema/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,10 @@ pub(in crate::schema) fn api_schema(input_schema: &Schema) -> Result<Document, A
// Refactor: Don't clone the schema.
let mut schema = input_schema.clone();
add_meta_field_type(&mut schema.document);
add_sql_field_type(&mut schema.document);

if ENV_VARS.graphql.enable_sql_service {
add_sql_field_type(&mut schema.document);
}
add_types_for_object_types(&mut schema, &object_types)?;
add_types_for_interface_types(&mut schema, &interface_types)?;
add_field_arguments(&mut schema.document, &input_schema.document)?;
Expand Down Expand Up @@ -869,7 +872,9 @@ fn add_query_type(
.collect();
fields.append(&mut fulltext_fields);
fields.push(meta_field());
fields.push(sql_field());
if ENV_VARS.graphql.enable_sql_service {
fields.push(sql_field());
}

let typedef = TypeDefinition::Object(ObjectType {
position: Pos::default(),
Expand Down Expand Up @@ -964,7 +969,10 @@ fn add_subscription_type(
.flat_map(|name| query_fields_for_type(name))
.collect();
fields.push(meta_field());
fields.push(sql_field());

if ENV_VARS.graphql.enable_sql_service {
fields.push(sql_field());
}

let typedef = TypeDefinition::Object(ObjectType {
position: Pos::default(),
Expand Down
5 changes: 5 additions & 0 deletions graphql/src/store/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,11 @@ impl StoreResolver {
field: &a::Field,
object_type: &ObjectOrInterface<'_>,
) -> Result<(Option<r::Value>, Option<r::Value>), QueryExecutionError> {
if !ENV_VARS.graphql.enable_sql_service {
return Err(QueryExecutionError::NotSupported(
"SQL service is not enabled".into(),
));
}
if !object_type.is_sql() {
return Ok((prefetched_object, None));
}
Expand Down

0 comments on commit 78f5edd

Please sign in to comment.