Skip to content

Commit

Permalink
Fix handling GET request in juniper_rocket example (#1223, #1098)
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
tyranron authored Nov 24, 2023
1 parent add0e23 commit bd8dc58
Show file tree
Hide file tree
Showing 7 changed files with 371 additions and 238 deletions.
2 changes: 1 addition & 1 deletion book/src/servers/rocket.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ Included in the source is a [small example][example] which sets up a basic Graph
[graphiql]: https://github.com/graphql/graphiql
[rocket]: https://rocket.rs/
[juniper_rocket]: https://github.com/graphql-rust/juniper/tree/master/juniper_rocket
[example]: https://github.com/graphql-rust/juniper/blob/master/juniper_rocket/examples/rocket_server.rs
[example]: https://github.com/graphql-rust/juniper/blob/master/juniper_rocket/examples/simple.rs
5 changes: 5 additions & 0 deletions juniper_rocket/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,15 @@ All user visible changes to `juniper_rocket` crate will be documented in this fi

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

### Changed

- Made `subscriptions_endpoint_url` argument polymorphic in `graphiql_source()` and `playground_source()`. ([#1223])

[#930]: /../../issues/930
[#968]: /../../pull/968
[#1205]: /../../pull/1205
[#1220]: /../../pull/1220
[#1223]: /../../pull/1223



Expand Down
4 changes: 2 additions & 2 deletions juniper_rocket/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ A basic usage example can also be found in the [API docs][`juniper_rocket`].

## Examples

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



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

[1]: https://github.com/graphql-rust/juniper/blob/master/juniper_rocket/examples/rocket_server.rs
[1]: https://github.com/graphql-rust/juniper/blob/master/juniper_rocket/examples/simple.rs
48 changes: 0 additions & 48 deletions juniper_rocket/examples/rocket_server.rs

This file was deleted.

68 changes: 68 additions & 0 deletions juniper_rocket/examples/simple.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
use juniper::{
tests::fixtures::starwars::schema::{Database, Query},
EmptyMutation, EmptySubscription, RootNode,
};
use rocket::{response::content::RawHtml, routes, State};

type Schema = RootNode<'static, Query, EmptyMutation<Database>, EmptySubscription<Database>>;

#[rocket::get("/")]
async fn homepage() -> RawHtml<&'static str> {
RawHtml(
"<html><h1>juniper_rocket/simple example</h1>\
<div>visit <a href=\"/graphiql\">GraphiQL</a></div>\
<div>visit <a href=\"/playground\">GraphQL Playground</a></div>\
</html>",
)
}

#[rocket::get("/graphiql")]
fn graphiql() -> RawHtml<String> {
juniper_rocket::graphiql_source("/graphql", None)
}

#[rocket::get("/playground")]
fn playground() -> RawHtml<String> {
juniper_rocket::playground_source("/graphql", None)
}

// GET request accepts query parameters like these:
// ?query=<urlencoded-graphql-query-string>
// &operationName=<optional-name>
// &variables=<optional-json-encoded-variables>
// See details here: https://graphql.org/learn/serving-over-http#get-request
#[rocket::get("/graphql?<request..>")]
async fn get_graphql(
db: &State<Database>,
request: juniper_rocket::GraphQLRequest,
schema: &State<Schema>,
) -> juniper_rocket::GraphQLResponse {
request.execute(schema, db).await
}

#[rocket::post("/graphql", data = "<request>")]
async fn post_graphql(
db: &State<Database>,
request: juniper_rocket::GraphQLRequest,
schema: &State<Schema>,
) -> juniper_rocket::GraphQLResponse {
request.execute(schema, db).await
}

#[rocket::main]
async fn main() {
_ = rocket::build()
.manage(Database::new())
.manage(Schema::new(
Query,
EmptyMutation::new(),
EmptySubscription::new(),
))
.mount(
"/",
routes![homepage, graphiql, playground, get_graphql, post_graphql],
)
.launch()
.await
.expect("server to launch");
}
Loading

0 comments on commit bd8dc58

Please sign in to comment.