@@ -41,9 +41,8 @@ Check the LICENSE file for details.
4141#![ doc( html_root_url = "https://docs.rs/juniper_actix/0.1.0" ) ]
4242
4343use 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} ;
4847use juniper:: {
4948 http:: {
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) ]
474470mod 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