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

Normalize "operation_name" value inside supergraph request #5008

Draft
wants to merge 3 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 15 additions & 0 deletions .changesets/fix_normalize_operation_name.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
### Normalize "operation_name" value inside supergraph request ([Issue #5014](https://github.com/apollographql/router/issues/5014))

At present, the behavior of `request.body.operation_name` in Rhai scripts is such that it is a direct copy of `operationName` from the client request.
So `request.body.operation_name` can be empty even if `query` contains a named query. For example:
```json
{
"query": "query OperationName { me { id } }"
}
```
In this case, `request.body.operation_name` was empty because the client didn't specify `operationName.`

This behavior is very confusing to users who expect it to have an operation name if `query` contains a named query.
After this change, `request.body.operation_name` will always default to the operation name extracted from parsed `query`.

By [@IvanGoncharov](https://github.com/IvanGoncharov) in https://github.com/apollographql/router/pull/5008
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 @@ -160,7 +168,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 @@ -173,26 +181,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