@@ -41,9 +41,8 @@ Check the LICENSE file for details.
41
41
#![ doc( html_root_url = "https://docs.rs/juniper_actix/0.1.0" ) ]
42
42
43
43
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 ,
47
46
} ;
48
47
use juniper:: {
49
48
http:: {
98
97
match * req. method ( ) {
99
98
Method :: POST => post_graphql_handler ( schema, context, req, payload) . await ,
100
99
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 ( ) ) ,
104
101
}
105
102
}
106
103
/// Actix GraphQL Handler for GET requests
@@ -152,17 +149,16 @@ where
152
149
let req = match req. content_type ( ) {
153
150
"application/json" => {
154
151
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 )
156
154
}
157
155
"application/graphql" => {
158
156
let body = String :: from_request ( & req, & mut payload. into_inner ( ) ) . await ?;
159
157
Ok ( GraphQLBatchRequest :: Single ( GraphQLRequest :: new (
160
158
body, None , None ,
161
159
) ) )
162
160
}
163
- _ => Err ( ErrorUnsupportedMediaType (
164
- "GraphQL requests should have content type `application/json` or `application/graphql`" ,
165
- ) ) ,
161
+ _ => Err ( JsonPayloadError :: ContentType ) ,
166
162
} ?;
167
163
let gql_batch_response = req. execute ( schema, context) . await ;
168
164
let gql_response = serde_json:: to_string ( & gql_batch_response) ?;
@@ -472,9 +468,9 @@ pub mod subscriptions {
472
468
473
469
#[ cfg( test) ]
474
470
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 } ;
476
473
use juniper:: {
477
- futures:: stream:: StreamExt ,
478
474
http:: tests:: { run_http_test_suite, HttpIntegration , TestResponse } ,
479
475
tests:: fixtures:: starwars:: schema:: { Database , Query } ,
480
476
EmptyMutation , EmptySubscription , RootNode ,
@@ -487,14 +483,9 @@ mod tests {
487
483
juniper:: RootNode < ' static , Query , EmptyMutation < Database > , EmptySubscription < Database > > ;
488
484
489
485
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 ( "" ) ,
498
489
}
499
490
}
500
491
@@ -608,11 +599,15 @@ mod tests {
608
599
. uri ( "/" )
609
600
. to_request ( ) ;
610
601
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 ;
613
608
614
609
let mut resp = test:: call_service ( & mut app, req) . await ;
615
-
610
+ dbg ! ( take_response_body_string ( & mut resp ) . await ) ;
616
611
assert_eq ! ( resp. status( ) , http:: StatusCode :: OK ) ;
617
612
assert_eq ! (
618
613
take_response_body_string( & mut resp) . await ,
@@ -637,8 +632,12 @@ mod tests {
637
632
. uri ( "/?query=%7B%20hero%28episode%3A%20NEW_HOPE%29%20%7B%20name%20%7D%20%7D&variables=null" )
638
633
. to_request ( ) ;
639
634
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 ;
642
641
643
642
let mut resp = test:: call_service ( & mut app, req) . await ;
644
643
@@ -677,8 +676,12 @@ mod tests {
677
676
. uri ( "/" )
678
677
. to_request ( ) ;
679
678
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 ;
682
685
683
686
let mut resp = test:: call_service ( & mut app, req) . await ;
684
687
@@ -712,8 +715,12 @@ mod tests {
712
715
EmptySubscription :: < Database > :: new ( ) ,
713
716
) ;
714
717
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 ;
717
724
718
725
let resp = test:: call_service ( & mut app, req. to_request ( ) ) . await ;
719
726
make_test_response ( resp) . await
@@ -768,7 +775,10 @@ mod subscription_tests {
768
775
use std:: time:: Duration ;
769
776
770
777
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
+ } ;
772
782
use actix_web_actors:: ws;
773
783
use juniper:: {
774
784
futures:: { SinkExt , StreamExt } ,
@@ -791,11 +801,11 @@ mod subscription_tests {
791
801
) -> Result < ( ) , anyhow:: Error > {
792
802
let mut server = start ( || {
793
803
App :: new ( )
794
- . data ( Schema :: new (
804
+ . app_data ( Data :: new ( Schema :: new (
795
805
Query ,
796
806
EmptyMutation :: < Database > :: new ( ) ,
797
807
Subscription ,
798
- ) )
808
+ ) ) )
799
809
. service ( web:: resource ( "/subscriptions" ) . to ( subscriptions) )
800
810
} ) ;
801
811
let mut framed = server. ws_at ( "/subscriptions" ) . await . unwrap ( ) ;
0 commit comments