Skip to content

Commit bd8dc58

Browse files
authored
Fix handling GET request in juniper_rocket example (#1223, #1098)
- rework `rocket_server` example as `simple` - provide example in `GraphQLRequest` API docs - mention GET query format in `GraphQLRequest` API docs and `simple` example Additionally: - fix `operationName` query parameter handling - make `graphiql_source()` and `playground_source()` polymorphic over `subscriptions_endpoint_url` argument - provide examples in `graphiql_source()` and `playground_source()` API docs - move integration HTTP tests to a separate file - test both sync and async `juniper_rocket` in integration HTTP tests - polish `FromForm` unit tests
1 parent add0e23 commit bd8dc58

File tree

7 files changed

+371
-238
lines changed

7 files changed

+371
-238
lines changed

book/src/servers/rocket.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@ Included in the source is a [small example][example] which sets up a basic Graph
1919
[graphiql]: https://github.com/graphql/graphiql
2020
[rocket]: https://rocket.rs/
2121
[juniper_rocket]: https://github.com/graphql-rust/juniper/tree/master/juniper_rocket
22-
[example]: https://github.com/graphql-rust/juniper/blob/master/juniper_rocket/examples/rocket_server.rs
22+
[example]: https://github.com/graphql-rust/juniper/blob/master/juniper_rocket/examples/simple.rs

juniper_rocket/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,15 @@ All user visible changes to `juniper_rocket` crate will be documented in this fi
1717

1818
- `AsRef` and `AsMut` implementation for `GraphQLRequest` to its inner type. ([#968], [#930])
1919

20+
### Changed
21+
22+
- Made `subscriptions_endpoint_url` argument polymorphic in `graphiql_source()` and `playground_source()`. ([#1223])
23+
2024
[#930]: /../../issues/930
2125
[#968]: /../../pull/968
2226
[#1205]: /../../pull/1205
2327
[#1220]: /../../pull/1220
28+
[#1223]: /../../pull/1223
2429

2530

2631

juniper_rocket/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ A basic usage example can also be found in the [API docs][`juniper_rocket`].
2424

2525
## Examples
2626

27-
Check [`examples/rocket_server.rs`][1] for example code of a working [`rocket`] server with [GraphQL] handlers.
27+
Check [`examples/simple.rs`][1] for example code of a working [`rocket`] server with [GraphQL] handlers.
2828

2929

3030

@@ -43,4 +43,4 @@ This project is licensed under [BSD 2-Clause License](https://github.com/graphql
4343
[Juniper Book]: https://graphql-rust.github.io
4444
[Rust]: https://www.rust-lang.org
4545

46-
[1]: https://github.com/graphql-rust/juniper/blob/master/juniper_rocket/examples/rocket_server.rs
46+
[1]: https://github.com/graphql-rust/juniper/blob/master/juniper_rocket/examples/simple.rs

juniper_rocket/examples/rocket_server.rs

Lines changed: 0 additions & 48 deletions
This file was deleted.

juniper_rocket/examples/simple.rs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
use juniper::{
2+
tests::fixtures::starwars::schema::{Database, Query},
3+
EmptyMutation, EmptySubscription, RootNode,
4+
};
5+
use rocket::{response::content::RawHtml, routes, State};
6+
7+
type Schema = RootNode<'static, Query, EmptyMutation<Database>, EmptySubscription<Database>>;
8+
9+
#[rocket::get("/")]
10+
async fn homepage() -> RawHtml<&'static str> {
11+
RawHtml(
12+
"<html><h1>juniper_rocket/simple example</h1>\
13+
<div>visit <a href=\"/graphiql\">GraphiQL</a></div>\
14+
<div>visit <a href=\"/playground\">GraphQL Playground</a></div>\
15+
</html>",
16+
)
17+
}
18+
19+
#[rocket::get("/graphiql")]
20+
fn graphiql() -> RawHtml<String> {
21+
juniper_rocket::graphiql_source("/graphql", None)
22+
}
23+
24+
#[rocket::get("/playground")]
25+
fn playground() -> RawHtml<String> {
26+
juniper_rocket::playground_source("/graphql", None)
27+
}
28+
29+
// GET request accepts query parameters like these:
30+
// ?query=<urlencoded-graphql-query-string>
31+
// &operationName=<optional-name>
32+
// &variables=<optional-json-encoded-variables>
33+
// See details here: https://graphql.org/learn/serving-over-http#get-request
34+
#[rocket::get("/graphql?<request..>")]
35+
async fn get_graphql(
36+
db: &State<Database>,
37+
request: juniper_rocket::GraphQLRequest,
38+
schema: &State<Schema>,
39+
) -> juniper_rocket::GraphQLResponse {
40+
request.execute(schema, db).await
41+
}
42+
43+
#[rocket::post("/graphql", data = "<request>")]
44+
async fn post_graphql(
45+
db: &State<Database>,
46+
request: juniper_rocket::GraphQLRequest,
47+
schema: &State<Schema>,
48+
) -> juniper_rocket::GraphQLResponse {
49+
request.execute(schema, db).await
50+
}
51+
52+
#[rocket::main]
53+
async fn main() {
54+
_ = rocket::build()
55+
.manage(Database::new())
56+
.manage(Schema::new(
57+
Query,
58+
EmptyMutation::new(),
59+
EmptySubscription::new(),
60+
))
61+
.mount(
62+
"/",
63+
routes![homepage, graphiql, playground, get_graphql, post_graphql],
64+
)
65+
.launch()
66+
.await
67+
.expect("server to launch");
68+
}

0 commit comments

Comments
 (0)