Skip to content

Commit 6ada6b0

Browse files
authored
Actix v4.0.0-beta.8 support (#952)
1 parent 739cc3b commit 6ada6b0

File tree

5 files changed

+72
-55
lines changed

5 files changed

+72
-55
lines changed

examples/actix_subscriptions/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ authors = ["Mihai Dinculescu <[email protected]>"]
66
publish = false
77

88
[dependencies]
9-
actix-web = "4.0.0-beta.5"
10-
actix-cors = "0.6.0-beta.1"
9+
actix-web = "4.0.0-beta.8"
10+
actix-cors = {git = "https://github.com/actix/actix-extras"}
1111
futures = "0.3"
1212
env_logger = "0.8"
1313
serde = "1.0"

examples/actix_subscriptions/src/main.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
use std::{env, pin::Pin, time::Duration};
22

33
use actix_cors::Cors;
4-
use actix_web::{http::header, middleware, web, App, Error, HttpRequest, HttpResponse, HttpServer};
4+
use actix_web::{
5+
http::header,
6+
middleware,
7+
web::{self, Data},
8+
App, Error, HttpRequest, HttpResponse, HttpServer,
9+
};
510

611
use juniper::{
712
graphql_object, graphql_subscription,
@@ -114,18 +119,18 @@ async fn main() -> std::io::Result<()> {
114119

115120
HttpServer::new(move || {
116121
App::new()
117-
.data(schema())
118-
.wrap(middleware::Compress::default())
119-
.wrap(middleware::Logger::default())
122+
.app_data(Data::new(schema()))
120123
.wrap(
121124
Cors::default()
122-
.allowed_origin("http://127.0.0.1:8080")
125+
.allow_any_origin()
123126
.allowed_methods(vec!["POST", "GET"])
124127
.allowed_headers(vec![header::AUTHORIZATION, header::ACCEPT])
125128
.allowed_header(header::CONTENT_TYPE)
126129
.supports_credentials()
127130
.max_age(3600),
128131
)
132+
.wrap(middleware::Compress::default())
133+
.wrap(middleware::Logger::default())
129134
.service(web::resource("/subscriptions").route(web::get().to(subscriptions)))
130135
.service(
131136
web::resource("/graphql")

juniper_actix/Cargo.toml

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,11 @@ repository = "https://github.com/graphql-rust/juniper"
1212
subscriptions = ["juniper_graphql_ws"]
1313

1414
[dependencies]
15-
actix = "0.11"
16-
# actix-web had some problems in beta release see https://github.com/actix/actix-web/issues/2173#issuecomment-822758353
17-
# and we need these version specification to handle this issue temporarily while the stable release is not available
18-
# to understand these dependecy version specification see https://github.com/actix/actix-web/issues/2185 or https://github.com/LukeMathWalker/zero-to-production/issues/96
19-
actix-http = "=3.0.0-beta.5"
20-
actix-service = "=2.0.0-beta.5"
21-
actix-web = "=4.0.0-beta.5"
22-
actix-web-actors = "4.0.0-beta.4"
15+
actix = "0.12"
16+
actix-http = "3.0.0-beta.8"
17+
http = "0.2.4"
18+
actix-web = "4.0.0-beta.8"
19+
actix-web-actors = "4.0.0-beta.6"
2320

2421
juniper = { version = "0.15.6", path = "../juniper", default-features = false }
2522
juniper_graphql_ws = { version = "0.2.5", path = "../juniper_graphql_ws", optional = true }
@@ -32,11 +29,11 @@ thiserror = "1.0"
3229

3330
[dev-dependencies]
3431
actix-rt = "2"
35-
actix-cors = "0.6.0-beta.1"
36-
actix-identity = "0.4.0-beta.1"
32+
actix-cors = {git = "https://github.com/actix/actix-extras"}
33+
actix-identity = {git = "https://github.com/actix/actix-extras"}
3734
tokio = "1"
3835
async-stream = "0.3"
39-
actix-test = "0.1.0-beta.1"
36+
actix-test = "0.1.0-beta.3"
4037

4138
juniper = { version = "0.15.6", path = "../juniper", features = ["expose-test-schema"] }
4239

juniper_actix/examples/actix_server.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@
33
use std::{collections::HashMap, env};
44

55
use actix_cors::Cors;
6-
use actix_web::{http::header, middleware, web, App, Error, HttpResponse, HttpServer};
6+
use actix_web::{
7+
http::header,
8+
middleware,
9+
web::{self, Data},
10+
App, Error, HttpResponse, HttpServer,
11+
};
712
use juniper::{graphql_object, EmptyMutation, EmptySubscription, GraphQLObject, RootNode};
813
use juniper_actix::{graphiql_handler, graphql_handler, playground_handler};
914

@@ -107,18 +112,18 @@ async fn main() -> std::io::Result<()> {
107112

108113
let server = HttpServer::new(move || {
109114
App::new()
110-
.data(schema())
111-
.wrap(middleware::Compress::default())
112-
.wrap(middleware::Logger::default())
115+
.app_data(Data::new(schema()))
113116
.wrap(
114117
Cors::default()
115-
.allowed_origin("http://127.0.0.1:8080")
118+
.allow_any_origin()
116119
.allowed_methods(vec!["POST", "GET"])
117120
.allowed_headers(vec![header::AUTHORIZATION, header::ACCEPT])
118121
.allowed_header(header::CONTENT_TYPE)
119122
.supports_credentials()
120123
.max_age(3600),
121124
)
125+
.wrap(middleware::Compress::default())
126+
.wrap(middleware::Logger::default())
122127
.service(
123128
web::resource("/graphgl")
124129
.route(web::post().to(graphql_route))

juniper_actix/src/lib.rs

Lines changed: 42 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,8 @@ Check the LICENSE file for details.
4141
#![doc(html_root_url = "https://docs.rs/juniper_actix/0.1.0")]
4242

4343
use actix_web::{
44-
error::{ErrorBadRequest, ErrorMethodNotAllowed, ErrorUnsupportedMediaType},
45-
http::Method,
46-
web, Error, FromRequest, HttpMessage, HttpRequest, HttpResponse,
44+
error::JsonPayloadError, http::Method, web, Error, FromRequest, HttpMessage, HttpRequest,
45+
HttpResponse,
4746
};
4847
use juniper::{
4948
http::{
@@ -98,9 +97,7 @@ where
9897
match *req.method() {
9998
Method::POST => post_graphql_handler(schema, context, req, payload).await,
10099
Method::GET => get_graphql_handler(schema, context, req).await,
101-
_ => Err(ErrorMethodNotAllowed(
102-
"GraphQL requests can only be sent with GET or POST",
103-
)),
100+
_ => Err(actix_web::error::UrlGenerationError::ResourceNotFound.into()),
104101
}
105102
}
106103
/// Actix GraphQL Handler for GET requests
@@ -152,17 +149,16 @@ where
152149
let req = match req.content_type() {
153150
"application/json" => {
154151
let body = String::from_request(&req, &mut payload.into_inner()).await?;
155-
serde_json::from_str::<GraphQLBatchRequest<S>>(&body).map_err(ErrorBadRequest)
152+
serde_json::from_str::<GraphQLBatchRequest<S>>(&body)
153+
.map_err(JsonPayloadError::Deserialize)
156154
}
157155
"application/graphql" => {
158156
let body = String::from_request(&req, &mut payload.into_inner()).await?;
159157
Ok(GraphQLBatchRequest::Single(GraphQLRequest::new(
160158
body, None, None,
161159
)))
162160
}
163-
_ => Err(ErrorUnsupportedMediaType(
164-
"GraphQL requests should have content type `application/json` or `application/graphql`",
165-
)),
161+
_ => Err(JsonPayloadError::ContentType),
166162
}?;
167163
let gql_batch_response = req.execute(schema, context).await;
168164
let gql_response = serde_json::to_string(&gql_batch_response)?;
@@ -472,9 +468,9 @@ pub mod subscriptions {
472468

473469
#[cfg(test)]
474470
mod tests {
475-
use actix_web::{dev::ServiceResponse, http, http::header::CONTENT_TYPE, test, App};
471+
use actix_http::body::AnyBody;
472+
use actix_web::{dev::ServiceResponse, http, http::header::CONTENT_TYPE, test, web::Data, App};
476473
use juniper::{
477-
futures::stream::StreamExt,
478474
http::tests::{run_http_test_suite, HttpIntegration, TestResponse},
479475
tests::fixtures::starwars::schema::{Database, Query},
480476
EmptyMutation, EmptySubscription, RootNode,
@@ -487,14 +483,9 @@ mod tests {
487483
juniper::RootNode<'static, Query, EmptyMutation<Database>, EmptySubscription<Database>>;
488484

489485
async fn take_response_body_string(resp: &mut ServiceResponse) -> String {
490-
let (response_body, ..) = resp
491-
.take_body()
492-
.map(|body_out| body_out.unwrap().to_vec())
493-
.into_future()
494-
.await;
495-
match response_body {
496-
Some(response_body) => String::from_utf8(response_body).unwrap(),
497-
None => String::from(""),
486+
match resp.response().body() {
487+
AnyBody::Bytes(body) => String::from_utf8(body.to_vec()).unwrap(),
488+
_ => String::from(""),
498489
}
499490
}
500491

@@ -608,11 +599,15 @@ mod tests {
608599
.uri("/")
609600
.to_request();
610601

611-
let mut app =
612-
test::init_service(App::new().data(schema).route("/", web::post().to(index))).await;
602+
let mut app = test::init_service(
603+
App::new()
604+
.app_data(Data::new(schema))
605+
.route("/", web::post().to(index)),
606+
)
607+
.await;
613608

614609
let mut resp = test::call_service(&mut app, req).await;
615-
610+
dbg!(take_response_body_string(&mut resp).await);
616611
assert_eq!(resp.status(), http::StatusCode::OK);
617612
assert_eq!(
618613
take_response_body_string(&mut resp).await,
@@ -637,8 +632,12 @@ mod tests {
637632
.uri("/?query=%7B%20hero%28episode%3A%20NEW_HOPE%29%20%7B%20name%20%7D%20%7D&variables=null")
638633
.to_request();
639634

640-
let mut app =
641-
test::init_service(App::new().data(schema).route("/", web::get().to(index))).await;
635+
let mut app = test::init_service(
636+
App::new()
637+
.app_data(Data::new(schema))
638+
.route("/", web::get().to(index)),
639+
)
640+
.await;
642641

643642
let mut resp = test::call_service(&mut app, req).await;
644643

@@ -677,8 +676,12 @@ mod tests {
677676
.uri("/")
678677
.to_request();
679678

680-
let mut app =
681-
test::init_service(App::new().data(schema).route("/", web::post().to(index))).await;
679+
let mut app = test::init_service(
680+
App::new()
681+
.app_data(Data::new(schema))
682+
.route("/", web::post().to(index)),
683+
)
684+
.await;
682685

683686
let mut resp = test::call_service(&mut app, req).await;
684687

@@ -712,8 +715,12 @@ mod tests {
712715
EmptySubscription::<Database>::new(),
713716
);
714717

715-
let mut app =
716-
test::init_service(App::new().data(schema).route("/", web::to(index))).await;
718+
let mut app = test::init_service(
719+
App::new()
720+
.app_data(Data::new(schema))
721+
.route("/", web::to(index)),
722+
)
723+
.await;
717724

718725
let resp = test::call_service(&mut app, req.to_request()).await;
719726
make_test_response(resp).await
@@ -768,7 +775,10 @@ mod subscription_tests {
768775
use std::time::Duration;
769776

770777
use actix_test::start;
771-
use actix_web::{web, App, Error, HttpRequest, HttpResponse};
778+
use actix_web::{
779+
web::{self, Data},
780+
App, Error, HttpRequest, HttpResponse,
781+
};
772782
use actix_web_actors::ws;
773783
use juniper::{
774784
futures::{SinkExt, StreamExt},
@@ -791,11 +801,11 @@ mod subscription_tests {
791801
) -> Result<(), anyhow::Error> {
792802
let mut server = start(|| {
793803
App::new()
794-
.data(Schema::new(
804+
.app_data(Data::new(Schema::new(
795805
Query,
796806
EmptyMutation::<Database>::new(),
797807
Subscription,
798-
))
808+
)))
799809
.service(web::resource("/subscriptions").to(subscriptions))
800810
});
801811
let mut framed = server.ws_at("/subscriptions").await.unwrap();

0 commit comments

Comments
 (0)