Skip to content
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
19 changes: 19 additions & 0 deletions NEXT_CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,25 @@ By [@neominik](https://github.com/neominik) in https://github.com/apollographql/

## 🛠 Maintenance

### Add more details when GraphQL request is invalid ([Issue #2301](https://github.com/apollographql/router/issues/2301))

Add more context to the error we're throwing if your GraphQL request is invalid, here is an exemple response if you pass `"variables": "null"` in your JSON payload.
```json
{
"errors": [
{
"message": "Invalid GraphQL request",
"extensions": {
"details": "failed to deserialize the request body into JSON: invalid type: string \"null\", expected a map at line 1 column 100",
"code": "INVALID_GRAPHQL_REQUEST"
}
}
]
}
```

By [@bnjjj](https://github.com/bnjjj) in https://github.com/apollographql/router/pull/2306

### Add outgoing request URLs for the subgraph calls in the OTEL spans ([Issue #2280](https://github.com/apollographql/router/issues/2280))

Add attribute named `http.url` containing the subgraph URL in span `subgraph_request`.
Expand Down
50 changes: 42 additions & 8 deletions apollo-router/src/services/router_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,18 +166,43 @@ where

let supergraph_service = self.supergraph_creator.create();
let fut = async move {
let graphql_request: Result<graphql::Request, &str> = if parts.method == Method::GET {
let graphql_request: Result<graphql::Request, (&str, String)> = if parts.method
== Method::GET
{
parts
.uri
.query()
.and_then(|q| graphql::Request::from_urlencoded_query(q.to_string()).ok())
.ok_or("missing query string")
.map(|q| {
graphql::Request::from_urlencoded_query(q.to_string()).map_err(|e| {
(
"failed to decode a valid GraphQL request from path",
format!("failed to decode a valid GraphQL request from path {}", e),
)
})
})
.unwrap_or_else(|| {
Err(("missing query string", "missing query string".to_string()))
})
} else {
hyper::body::to_bytes(body)
.await
.map_err(|_| ())
.and_then(|bytes| serde_json::from_reader(bytes.reader()).map_err(|_| ()))
.map_err(|_| "failed to parse the request body as JSON")
.map_err(|e| {
(
"failed to get the request body",
format!("failed to get the request body: {}", e),
)
})
.and_then(|bytes| {
serde_json::from_reader(bytes.reader()).map_err(|err| {
(
"failed to deserialize the request body into JSON",
format!(
"failed to deserialize the request body into JSON: {}",
err
),
)
})
})
};

match graphql_request {
Expand Down Expand Up @@ -313,7 +338,7 @@ where
}
}
}
Err(error) => {
Err((error, extension_details)) => {
// BAD REQUEST
::tracing::error!(
monotonic_counter.apollo_router_http_requests_total = 1u64,
Expand All @@ -324,7 +349,16 @@ where
Ok(router::Response {
response: http::Response::builder()
.status(StatusCode::BAD_REQUEST)
.body(Body::from("Invalid GraphQL request"))
.body(Body::from(
serde_json::to_string(
&graphql::Error::builder()
.message(String::from("Invalid GraphQL request"))
.extension_code("INVALID_GRAPHQL_REQUEST")
.extension("details", extension_details)
.build(),
)
.unwrap_or_else(|_| String::from("Invalid GraphQL request")),
))
.expect("cannot fail"),
context,
})
Expand Down