Skip to content

Commit 58ae682

Browse files
authored
Upgrade axum crate from 0.6 to 0.7 version (#1224)
1 parent bd8dc58 commit 58ae682

File tree

8 files changed

+61
-58
lines changed

8 files changed

+61
-58
lines changed

juniper_axum/CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ All user visible changes to `juniper_axum` crate will be documented in this file
1010

1111
### Initialized
1212

13-
- Dependent on 0.6 version of [`axum` crate]. ([#1088])
13+
- Dependent on 0.7 version of [`axum` crate]. ([#1088], [#1224])
1414
- Dependent on 0.16 version of [`juniper` crate]. ([#1088])
1515
- Dependent on 0.4 version of [`juniper_graphql_ws` crate]. ([#1088])
1616

@@ -28,6 +28,7 @@ All user visible changes to `juniper_axum` crate will be documented in this file
2828
[#986]: /../../issues/986
2929
[#1088]: /../../pull/1088
3030
[#1184]: /../../issues/1184
31+
[#1224]: /../../pull/1224
3132

3233

3334

juniper_axum/Cargo.toml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ rustdoc-args = ["--cfg", "docsrs"]
2525
subscriptions = ["axum/ws", "juniper_graphql_ws/graphql-ws", "dep:futures"]
2626

2727
[dependencies]
28-
axum = "0.6.20"
28+
axum = "0.7"
2929
futures = { version = "0.3.22", optional = true }
3030
juniper = { version = "0.16.0-dev", path = "../juniper", default-features = false }
3131
juniper_graphql_ws = { version = "0.4.0-dev", path = "../juniper_graphql_ws", features = ["graphql-transport-ws"] }
@@ -38,9 +38,10 @@ bytes = "1.2"
3838

3939
[dev-dependencies]
4040
anyhow = "1.0"
41-
axum = { version = "0.6", features = ["macros"] }
41+
axum = { version = "0.7", features = ["http1", "macros", "tokio"] }
42+
futures = "0.3.22"
4243
juniper = { version = "0.16.0-dev", path = "../juniper", features = ["expose-test-schema"] }
43-
tokio = { version = "1.20", features = ["macros", "rt-multi-thread", "time"] }
44+
tokio = { version = "1.20", features = ["macros", "net", "rt-multi-thread", "time"] }
4445
tokio-stream = "0.1"
4546
tokio-tungstenite = "0.20"
4647
tower-service = "0.3"

juniper_axum/examples/custom.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use juniper_axum::{
1919
extract::JuniperRequest, graphiql, playground, response::JuniperResponse, subscriptions,
2020
};
2121
use juniper_graphql_ws::ConnectionConfig;
22+
use tokio::net::TcpListener;
2223

2324
type Schema = RootNode<'static, Query, EmptyMutation<Database>, Subscription>;
2425

@@ -68,7 +69,7 @@ async fn main() {
6869
let app = Router::new()
6970
.route(
7071
"/graphql",
71-
on(MethodFilter::GET | MethodFilter::POST, custom_graphql),
72+
on(MethodFilter::GET.or(MethodFilter::POST), custom_graphql),
7273
)
7374
.route("/subscriptions", get(custom_subscriptions))
7475
.route("/graphiql", get(graphiql("/graphql", "/subscriptions")))
@@ -78,9 +79,11 @@ async fn main() {
7879
.layer(Extension(database));
7980

8081
let addr = SocketAddr::from(([127, 0, 0, 1], 8080));
82+
let listener = TcpListener::bind(addr)
83+
.await
84+
.unwrap_or_else(|e| panic!("failed to listen on {addr}: {e}"));
8185
tracing::info!("listening on {addr}");
82-
axum::Server::bind(&addr)
83-
.serve(app.into_make_service())
86+
axum::serve(listener, app)
8487
.await
85-
.unwrap_or_else(|e| panic!("failed to run `axum::Server`: {e}"));
88+
.unwrap_or_else(|e| panic!("failed to run `axum::serve`: {e}"));
8689
}

juniper_axum/examples/simple.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use futures::stream::{BoxStream, StreamExt as _};
1111
use juniper::{graphql_object, graphql_subscription, EmptyMutation, FieldError, RootNode};
1212
use juniper_axum::{graphiql, graphql, playground, ws};
1313
use juniper_graphql_ws::ConnectionConfig;
14-
use tokio::time::interval;
14+
use tokio::{net::TcpListener, time::interval};
1515
use tokio_stream::wrappers::IntervalStream;
1616

1717
#[derive(Clone, Copy, Debug)]
@@ -65,7 +65,7 @@ async fn main() {
6565
.route(
6666
"/graphql",
6767
on(
68-
MethodFilter::GET | MethodFilter::POST,
68+
MethodFilter::GET.or(MethodFilter::POST),
6969
graphql::<Arc<Schema>>,
7070
),
7171
)
@@ -79,9 +79,11 @@ async fn main() {
7979
.layer(Extension(Arc::new(schema)));
8080

8181
let addr = SocketAddr::from(([127, 0, 0, 1], 8080));
82+
let listener = TcpListener::bind(addr)
83+
.await
84+
.unwrap_or_else(|e| panic!("failed to listen on {addr}: {e}"));
8285
tracing::info!("listening on {addr}");
83-
axum::Server::bind(&addr)
84-
.serve(app.into_make_service())
86+
axum::serve(listener, app)
8587
.await
86-
.unwrap_or_else(|e| panic!("failed to run `axum::Server`: {e}"));
88+
.unwrap_or_else(|e| panic!("failed to run `axum::serve`: {e}"));
8789
}

juniper_axum/src/extract.rs

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,14 @@ where
7171
S: ScalarValue;
7272

7373
#[async_trait]
74-
impl<S, State> FromRequest<State, Body> for JuniperRequest<S>
74+
impl<S, State> FromRequest<State> for JuniperRequest<S>
7575
where
7676
S: ScalarValue,
7777
State: Sync,
7878
Query<GetRequest>: FromRequestParts<State>,
79-
Json<GraphQLBatchRequest<S>>: FromRequest<State, Body>,
80-
<Json<GraphQLBatchRequest<S>> as FromRequest<State, Body>>::Rejection: fmt::Display,
81-
String: FromRequest<State, Body>,
79+
Json<GraphQLBatchRequest<S>>: FromRequest<State>,
80+
<Json<GraphQLBatchRequest<S>> as FromRequest<State>>::Rejection: fmt::Display,
81+
String: FromRequest<State>,
8282
{
8383
type Rejection = Response;
8484

@@ -96,7 +96,9 @@ where
9696
.into_response()
9797
})?;
9898

99-
match (req.method(), content_type) {
99+
// TODO: Move into `match` expression directly once MSRV is bumped higher than 1.74.
100+
let method = req.method();
101+
match (method, content_type) {
100102
(&Method::GET, _) => req
101103
.extract_parts::<Query<GetRequest>>()
102104
.await
@@ -180,13 +182,8 @@ impl<S: ScalarValue> TryFrom<GetRequest> for GraphQLRequest<S> {
180182

181183
#[cfg(test)]
182184
mod juniper_request_tests {
183-
use std::fmt;
184-
185-
use axum::{
186-
body::{Body, Bytes, HttpBody},
187-
extract::FromRequest as _,
188-
http::Request,
189-
};
185+
use axum::{body::Body, extract::FromRequest as _, http::Request};
186+
use futures::TryStreamExt as _;
190187
use juniper::{
191188
graphql_input_value,
192189
http::{GraphQLBatchRequest, GraphQLRequest},
@@ -279,18 +276,15 @@ mod juniper_request_tests {
279276
}
280277
}
281278

282-
/// Converts the provided [`HttpBody`] into a [`String`].
283-
async fn display_body<B>(mut body: B) -> String
284-
where
285-
B: HttpBody<Data = Bytes> + Unpin,
286-
B::Error: fmt::Display,
287-
{
288-
let mut body_bytes = vec![];
289-
while let Some(bytes) = body.data().await {
290-
body_bytes.extend(
291-
bytes.unwrap_or_else(|e| panic!("failed to represent `Body` as `Bytes`: {e}")),
292-
);
293-
}
294-
String::from_utf8(body_bytes).unwrap_or_else(|e| panic!("not UTF-8 body: {e}"))
279+
/// Converts the provided [`Body`] into a [`String`].
280+
async fn display_body(body: Body) -> String {
281+
String::from_utf8(
282+
body.into_data_stream()
283+
.map_ok(|bytes| bytes.to_vec())
284+
.try_concat()
285+
.await
286+
.unwrap(),
287+
)
288+
.unwrap_or_else(|e| panic!("not UTF-8 body: {e}"))
295289
}
296290
}

juniper_axum/tests/http_test_suite.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
use std::sync::Arc;
22

33
use axum::{
4-
body::{Body, HttpBody as _},
4+
body::Body,
55
http::Request,
66
response::Response,
77
routing::{get, post},
88
Extension, Router,
99
};
10+
use futures::TryStreamExt as _;
1011
use juniper::{
1112
http::tests::{run_http_test_suite, HttpIntegration, TestResponse},
1213
tests::fixtures::starwars::schema::{Database, Query},
@@ -97,12 +98,15 @@ async fn into_test_response(resp: Response) -> TestResponse {
9798
})
9899
.unwrap_or_default();
99100

100-
let mut body = resp.into_body();
101-
let mut body_bytes = vec![];
102-
while let Some(bytes) = body.data().await {
103-
body_bytes.extend(bytes.unwrap());
104-
}
105-
let body = String::from_utf8(body_bytes).unwrap_or_else(|e| panic!("not UTF-8 body: {e}"));
101+
let body = String::from_utf8(
102+
resp.into_body()
103+
.into_data_stream()
104+
.map_ok(|bytes| bytes.to_vec())
105+
.try_concat()
106+
.await
107+
.unwrap(),
108+
)
109+
.unwrap_or_else(|e| panic!("not UTF-8 body: {e}"));
106110

107111
TestResponse {
108112
status_code,

juniper_axum/tests/ws_test_suite.rs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
#![cfg(not(windows))]
22

3-
use std::{
4-
net::{SocketAddr, TcpListener},
5-
sync::Arc,
6-
};
3+
use std::{net::SocketAddr, sync::Arc};
74

85
use anyhow::anyhow;
96
use axum::{routing::get, Extension, Router};
@@ -15,7 +12,10 @@ use juniper::{
1512
};
1613
use juniper_axum::subscriptions;
1714
use juniper_graphql_ws::ConnectionConfig;
18-
use tokio::{net::TcpStream, time::timeout};
15+
use tokio::{
16+
net::{TcpListener, TcpStream},
17+
time::timeout,
18+
};
1919
use tokio_tungstenite::{connect_async, tungstenite::Message, MaybeTlsStream, WebSocketStream};
2020

2121
type Schema = RootNode<'static, Query, EmptyMutation<Database>, Subscription>;
@@ -49,15 +49,13 @@ impl TestApp {
4949
}
5050

5151
async fn run(self, messages: Vec<WsIntegrationMessage>) -> Result<(), anyhow::Error> {
52-
let listener = TcpListener::bind("0.0.0.0:0".parse::<SocketAddr>().unwrap()).unwrap();
52+
let listener = TcpListener::bind("0.0.0.0:0".parse::<SocketAddr>().unwrap())
53+
.await
54+
.unwrap();
5355
let addr = listener.local_addr().unwrap();
5456

5557
tokio::spawn(async move {
56-
axum::Server::from_tcp(listener)
57-
.unwrap()
58-
.serve(self.0.into_make_service())
59-
.await
60-
.unwrap();
58+
axum::serve(listener, self.0).await.unwrap();
6159
});
6260

6361
let (mut websocket, _) = connect_async(format!("ws://{}/subscriptions", addr))

juniper_warp/tests/http_test_suite.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ impl TestWarpIntegration {
3939
let rt = tokio::runtime::Runtime::new()
4040
.unwrap_or_else(|e| panic!("failed to create `tokio::Runtime`: {e}"));
4141
rt.block_on(async move {
42-
make_test_response(req.filter(&self.filter).await.unwrap_or_else(|rejection| {
42+
into_test_response(req.filter(&self.filter).await.unwrap_or_else(|rejection| {
4343
let code = if rejection.is_not_found() {
4444
http::StatusCode::NOT_FOUND
4545
} else if let Some(body::BodyDeserializeError { .. }) = rejection.find() {
@@ -101,7 +101,7 @@ impl HttpIntegration for TestWarpIntegration {
101101
}
102102
}
103103

104-
async fn make_test_response(resp: reply::Response) -> TestResponse {
104+
async fn into_test_response(resp: reply::Response) -> TestResponse {
105105
let (parts, body) = resp.into_parts();
106106

107107
let status_code = parts.status.as_u16().into();

0 commit comments

Comments
 (0)