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

fix: add more details when graphql request is invalid #2306

Merged
merged 3 commits into from
Dec 22, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
15 changes: 15 additions & 0 deletions NEXT_CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,21 @@ 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 if you pass `"variables": "null"` in your JSON payload.
```json
{
"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"
}
}
```
o0Ignition0o marked this conversation as resolved.
Show resolved Hide resolved

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
30 changes: 23 additions & 7 deletions apollo-router/src/services/router_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,18 +166,25 @@ 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, 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| {
format!("failed to decode a valid GraphQL request from path {}", e)
})
})
.unwrap_or_else(|| Err("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| format!("failed to get the request body: {}", e))
.and_then(|bytes| {
serde_json::from_reader(bytes.reader()).map_err(|err| {
format!("failed to deserialize the request body into JSON: {}", err)
})
})
};

match graphql_request {
Expand Down Expand Up @@ -324,7 +331,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", error)
.build(),
)
.unwrap_or_else(|_| String::from("Invalid GraphQL request")),
))
.expect("cannot fail"),
context,
})
Expand Down