Skip to content

Commit

Permalink
Normalize "operation_name" value inside supergraph request
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanGoncharov committed Apr 23, 2024
1 parent 6f12471 commit 75ad1e7
Showing 1 changed file with 23 additions and 6 deletions.
29 changes: 23 additions & 6 deletions apollo-router/src/services/layers/query_analysis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use tokio::sync::Mutex;

use crate::context::OPERATION_KIND;
use crate::context::OPERATION_NAME;
use crate::graphql;
use crate::graphql::Error;
use crate::graphql::ErrorExtension;
use crate::graphql::IntoGraphQLErrors;
Expand All @@ -34,7 +35,14 @@ use crate::Context;
pub(crate) struct QueryAnalysisLayer {
pub(crate) schema: Arc<Schema>,
configuration: Arc<Configuration>,
cache: Arc<Mutex<LruCache<QueryAnalysisKey, Result<(Context, ParsedDocument), SpecError>>>>,
cache: Arc<
Mutex<
LruCache<
QueryAnalysisKey,
Result<(Context, ParsedDocument, Option<String>), SpecError>,
>,
>,
>,
enable_authorization_directives: bool,
}

Expand Down Expand Up @@ -165,7 +173,7 @@ impl QueryAnalysisLayer {
}

context
.insert(OPERATION_NAME, operation_name)
.insert(OPERATION_NAME, operation_name.clone())
.expect("cannot insert operation name into context; this is a bug");
let operation_kind =
operation.map(|op| OperationKind::from(op.operation_type));
Expand All @@ -178,26 +186,35 @@ impl QueryAnalysisLayer {
query,
operation_name: op_name,
},
Ok((context.clone(), doc.clone())),
Ok((context.clone(), doc.clone(), operation_name.clone())),
);

Ok((context, doc))
Ok((context, doc, operation_name))
}
}
}
Some(c) => c,
};

match res {
Ok((context, doc)) => {
Ok((context, doc, operation_name)) => {
// Normalize request by adding operation name if it is missing
// Note: if request's operationName was invalid we would fail earlier.
// So this code will never change validity of client request.
let supergraph_request =
request.supergraph_request.map(|graphql| graphql::Request {
operation_name: graphql.operation_name.or(operation_name),
..graphql
});

request.context.extend(&context);
request
.context
.extensions()
.lock()
.insert::<ParsedDocument>(doc);
Ok(SupergraphRequest {
supergraph_request: request.supergraph_request,
supergraph_request,
context: request.context,
})
}
Expand Down

0 comments on commit 75ad1e7

Please sign in to comment.