Skip to content

Commit 2518eff

Browse files
davidpdrsnLegNeato
authored andcommitted
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.
1 parent 166c6d0 commit 2518eff

File tree

4 files changed

+54
-10
lines changed

4 files changed

+54
-10
lines changed

juniper/CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,11 @@ This should not have any impact on your code, since juniper already was 2018 com
2525
- Fix introspection query validity
2626
The DirectiveLocation::InlineFragment had an invalid literal value,
2727
which broke third party tools like apollo cli.
28-
- Added GraphQL Playground integration
29-
The DirectiveLocation::InlineFragment had an invalid literal value,
28+
- Added GraphQL Playground integration.
29+
The `DirectiveLocation::InlineFragment` had an invalid literal value,
3030
which broke third party tools like apollo cli.
3131
- The return type of `value::object::Object::iter/iter_mut` has changed to `impl Iter`. [#312](https://github.com/graphql-rust/juniper/pull/312)
32+
- Add `GraphQLRequest::operation_name` [#353](https://github.com/graphql-rust/juniper/pull/353)
3233

3334
# [0.11.1] 2018-12-19
3435

juniper/src/http/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ where
3636
S: ScalarValue,
3737
{
3838
/// Returns the `operation_name` associated with this request.
39-
fn operation_name(&self) -> Option<&str> {
39+
pub fn operation_name(&self) -> Option<&str> {
4040
self.operation_name.as_ref().map(|oper_name| &**oper_name)
4141
}
4242

juniper_rocket/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# master
22

3+
- Expose the operation names from `GraphQLRequest`.
34
- Compatibility with the latest `juniper`.
45

56
# [0.2.0] 2018-12-17

juniper_rocket/src/lib.rs

Lines changed: 49 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,15 @@ where
108108
),
109109
}
110110
}
111+
112+
pub fn operation_names(&self) -> Vec<Option<&str>> {
113+
match self {
114+
GraphQLBatchRequest::Single(req) => vec![req.operation_name()],
115+
GraphQLBatchRequest::Batch(reqs) => {
116+
reqs.iter().map(|req| req.operation_name()).collect()
117+
}
118+
}
119+
}
111120
}
112121

113122
impl<'a, S> GraphQLBatchResponse<'a, S>
@@ -174,6 +183,13 @@ where
174183

175184
GraphQLResponse(status, json)
176185
}
186+
187+
/// Returns the operation names associated with this request.
188+
///
189+
/// For batch requests there will be multiple names.
190+
pub fn operation_names(&self) -> Vec<Option<&str>> {
191+
self.0.operation_names()
192+
}
177193
}
178194

179195
impl GraphQLResponse {
@@ -519,14 +535,40 @@ mod tests {
519535
http_tests::run_http_test_suite(&integration);
520536
}
521537

538+
#[test]
539+
fn test_operation_names() {
540+
#[post("/", data = "<request>")]
541+
fn post_graphql_assert_operation_name_handler(
542+
context: State<Database>,
543+
request: super::GraphQLRequest,
544+
schema: State<Schema>,
545+
) -> super::GraphQLResponse {
546+
assert_eq!(request.operation_names(), vec![Some("TestQuery")]);
547+
request.execute(&schema, &context)
548+
}
549+
550+
let rocket = make_rocket_without_routes()
551+
.mount("/", routes![post_graphql_assert_operation_name_handler]);
552+
let client = Client::new(rocket).expect("valid rocket");
553+
554+
let req = client
555+
.post("/")
556+
.header(ContentType::JSON)
557+
.body(r#"{"query": "query TestQuery {hero{name}}", "operationName": "TestQuery"}"#);
558+
let resp = make_test_response(&req);
559+
560+
assert_eq!(resp.status_code, 200);
561+
}
562+
522563
fn make_rocket() -> Rocket {
523-
rocket::ignite()
524-
.manage(Database::new())
525-
.manage(Schema::new(
526-
Database::new(),
527-
EmptyMutation::<Database>::new(),
528-
))
529-
.mount("/", routes![post_graphql_handler, get_graphql_handler])
564+
make_rocket_without_routes().mount("/", routes![post_graphql_handler, get_graphql_handler])
565+
}
566+
567+
fn make_rocket_without_routes() -> Rocket {
568+
rocket::ignite().manage(Database::new()).manage(Schema::new(
569+
Database::new(),
570+
EmptyMutation::<Database>::new(),
571+
))
530572
}
531573

532574
fn make_test_response(request: &LocalRequest) -> http_tests::TestResponse {

0 commit comments

Comments
 (0)