Expose the operation name from juniper_rocket::GraphQLRequest (#353)

Measuring the runtime of queries will only tell if there are slow
queries. To find out which queries are slow you need the operation name.

Getting the operation name was previously not possible from a Rocket
request handler. This fixes that.
This commit is contained in:
David Pedersen 2019-05-15 16:26:40 +02:00 committed by Christian Legnitto
parent 166c6d00c5
commit 2518eff0c9
4 changed files with 54 additions and 10 deletions

View file

@ -25,10 +25,11 @@ This should not have any impact on your code, since juniper already was 2018 com
- Fix introspection query validity - Fix introspection query validity
The DirectiveLocation::InlineFragment had an invalid literal value, The DirectiveLocation::InlineFragment had an invalid literal value,
which broke third party tools like apollo cli. which broke third party tools like apollo cli.
- Added GraphQL Playground integration - Added GraphQL Playground integration.
The DirectiveLocation::InlineFragment had an invalid literal value, The `DirectiveLocation::InlineFragment` had an invalid literal value,
which broke third party tools like apollo cli. which broke third party tools like apollo cli.
- The return type of `value::object::Object::iter/iter_mut` has changed to `impl Iter`. [#312](https://github.com/graphql-rust/juniper/pull/312) - The return type of `value::object::Object::iter/iter_mut` has changed to `impl Iter`. [#312](https://github.com/graphql-rust/juniper/pull/312)
- Add `GraphQLRequest::operation_name` [#353](https://github.com/graphql-rust/juniper/pull/353)
# [0.11.1] 2018-12-19 # [0.11.1] 2018-12-19

View file

@ -36,7 +36,7 @@ where
S: ScalarValue, S: ScalarValue,
{ {
/// Returns the `operation_name` associated with this request. /// Returns the `operation_name` associated with this request.
fn operation_name(&self) -> Option<&str> { pub fn operation_name(&self) -> Option<&str> {
self.operation_name.as_ref().map(|oper_name| &**oper_name) self.operation_name.as_ref().map(|oper_name| &**oper_name)
} }

View file

@ -1,5 +1,6 @@
# master # master
- Expose the operation names from `GraphQLRequest`.
- Compatibility with the latest `juniper`. - Compatibility with the latest `juniper`.
# [0.2.0] 2018-12-17 # [0.2.0] 2018-12-17

View file

@ -108,6 +108,15 @@ where
), ),
} }
} }
pub fn operation_names(&self) -> Vec<Option<&str>> {
match self {
GraphQLBatchRequest::Single(req) => vec![req.operation_name()],
GraphQLBatchRequest::Batch(reqs) => {
reqs.iter().map(|req| req.operation_name()).collect()
}
}
}
} }
impl<'a, S> GraphQLBatchResponse<'a, S> impl<'a, S> GraphQLBatchResponse<'a, S>
@ -174,6 +183,13 @@ where
GraphQLResponse(status, json) GraphQLResponse(status, json)
} }
/// Returns the operation names associated with this request.
///
/// For batch requests there will be multiple names.
pub fn operation_names(&self) -> Vec<Option<&str>> {
self.0.operation_names()
}
} }
impl GraphQLResponse { impl GraphQLResponse {
@ -519,14 +535,40 @@ mod tests {
http_tests::run_http_test_suite(&integration); http_tests::run_http_test_suite(&integration);
} }
#[test]
fn test_operation_names() {
#[post("/", data = "<request>")]
fn post_graphql_assert_operation_name_handler(
context: State<Database>,
request: super::GraphQLRequest,
schema: State<Schema>,
) -> super::GraphQLResponse {
assert_eq!(request.operation_names(), vec![Some("TestQuery")]);
request.execute(&schema, &context)
}
let rocket = make_rocket_without_routes()
.mount("/", routes![post_graphql_assert_operation_name_handler]);
let client = Client::new(rocket).expect("valid rocket");
let req = client
.post("/")
.header(ContentType::JSON)
.body(r#"{"query": "query TestQuery {hero{name}}", "operationName": "TestQuery"}"#);
let resp = make_test_response(&req);
assert_eq!(resp.status_code, 200);
}
fn make_rocket() -> Rocket { fn make_rocket() -> Rocket {
rocket::ignite() make_rocket_without_routes().mount("/", routes![post_graphql_handler, get_graphql_handler])
.manage(Database::new()) }
.manage(Schema::new(
Database::new(), fn make_rocket_without_routes() -> Rocket {
EmptyMutation::<Database>::new(), rocket::ignite().manage(Database::new()).manage(Schema::new(
)) Database::new(),
.mount("/", routes![post_graphql_handler, get_graphql_handler]) EmptyMutation::<Database>::new(),
))
} }
fn make_test_response(request: &LocalRequest) -> http_tests::TestResponse { fn make_test_response(request: &LocalRequest) -> http_tests::TestResponse {