Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added expose_query_plan to federation source #341

Merged
merged 1 commit into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions libs/common/src/graphql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,16 @@ pub struct GraphQLResponse {
}

impl GraphQLResponse {
pub fn append_extensions(&mut self, extensions: Map<String, Value>) {
if let Some(existing_extensions) = &mut self.extensions {
if let Value::Object(existing_extensions_map) = existing_extensions {
existing_extensions_map.extend(extensions);
}
} else {
self.extensions = Some(Value::Object(extensions));
}
}

pub fn new_error(error: &str) -> Self {
GraphQLResponse {
data: None,
Expand Down
8 changes: 8 additions & 0 deletions libs/config/conductor.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,14 @@
"supergraph"
],
"properties": {
"expose_query_plan": {
"description": "Exposes the query plan as JSON under \"extensions\"",
"default": null,
"type": [
"boolean",
"null"
]
},
"supergraph": {
"description": "The endpoint URL for the GraphQL source.",
"$ref": "#/definitions/SupergraphSourceConfig"
Expand Down
3 changes: 3 additions & 0 deletions libs/config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,9 @@ fn graphql_source_definition_example() -> JsonSchemaExample<SourceDefinition> {
pub struct FederationSourceConfig {
/// The endpoint URL for the GraphQL source.
pub supergraph: SupergraphSourceConfig,
/// Exposes the query plan as JSON under "extensions"
#[serde(default)]
pub expose_query_plan: Option<bool>,
}

#[derive(Deserialize, Serialize, Debug, Clone, JsonSchema)]
Expand Down
14 changes: 12 additions & 2 deletions libs/engine/src/source/federation_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,18 @@ impl SourceRuntime for FederationSourceRuntime {
let operation = downstream_request.parsed_operation;

match execute_federation(&self.supergraph, operation).await {
Ok(response_data) => {
let response = serde_json::from_str::<GraphQLResponse>(&response_data).unwrap();
Ok((response_data, query_plan)) => {
let mut response = serde_json::from_str::<GraphQLResponse>(&response_data).unwrap();

if self.config.expose_query_plan.is_some_and(|v| v) {
let mut ext = serde_json::Map::new();
ext.insert(
"queryPlan".to_string(),
serde_json::value::to_value(query_plan).unwrap(),
);

response.append_extensions(ext);
}

Ok(response)
}
Expand Down
8 changes: 6 additions & 2 deletions libs/federation_query_planner/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::ops::Index;

use anyhow::{Ok, Result};
use graphql_parser::query::Document;
use query_planner::QueryPlan;
use serde_json::json;
use supergraph::Supergraph;

Expand All @@ -20,7 +21,7 @@ pub mod user_query;
pub async fn execute_federation(
supergraph: &Supergraph,
parsed_user_query: Document<'static, String>,
) -> Result<String> {
) -> Result<(String, QueryPlan)> {
// println!("parsed_user_query: {:#?}", user_query);
let mut user_query = parse_user_query(parsed_user_query)?;
let query_plan = plan_for_user_query(supergraph, &mut user_query)?;
Expand All @@ -31,7 +32,10 @@ pub async fn execute_federation(

// println!("response: {:#?}", json!(response_vec).to_string());

Ok(json!(response_vec.index(0).index(0).1).to_string())
Ok((
json!(response_vec.index(0).index(0).1).to_string(),
query_plan,
))
}

#[cfg(test)]
Expand Down
1 change: 1 addition & 0 deletions test_config/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ sources:
- id: fed
type: federation
config:
expose_query_plan: true
supergraph:
file: ./supergraph.graphql

Expand Down
Loading