/// Actix GraphQL Handler for POST requests
Actix GraphQL Handler for POST requests
Actix GraphQL Handler for POST requests
pub async fn post_graphql_handler<Query, Mutation, Subscription, Context, S>(
    schema: &juniper::RootNode<'static, Query, Mutation, Subscription, S>,
    context: &Context,
    req: HttpRequest,
    payload: actix_web::web::Payload,
) -> Result<HttpResponse, Error>
where
    S: ScalarValue + Send + Sync + 'static,
    Context: Send + Sync + 'static,
    Query: juniper::GraphQLTypeAsync<S, Context = Context> + Send + Sync + 'static,
    Query::TypeInfo: Send + Sync,
    Mutation: juniper::GraphQLTypeAsync<S, Context = Context> + Send + Sync + 'static,
    Mutation::TypeInfo: Send + Sync,
    Subscription: juniper::GraphQLSubscriptionType<S, Context = Context> + Send + Sync + 'static,
    Subscription::TypeInfo: Send + Sync,
{
    let content_type_header = req
        .headers()
        .get(CONTENT_TYPE)
        .and_then(|hv| hv.to_str().ok());
    let req = match content_type_header {
        Some("application/json") | Some("application/graphql") => {
            let body_string = String::from_request(&req, &mut payload.into_inner()).await;
            let body_string = body_string?;
            match serde_json::from_str::<GraphQLBatchRequest<S>>(&body_string) {
                Ok(req) => Ok(req),
                Err(err) => Err(ErrorBadRequest(err)),
            }
        }
        _ => Err(ErrorUnsupportedMediaType(
            "GraphQL requests should have content type `application/json` or `application/graphql`",
        )),
    }?;
    let gql_batch_response = req.execute(schema, context).await;
    let gql_response = serde_json::to_string(&gql_batch_response)?;
    let mut response = match gql_batch_response.is_ok() {
        true => HttpResponse::Ok(),
        false => HttpResponse::BadRequest(),
    };
    Ok(response.content_type("application/json").body(gql_response))
}
pub async fn post_graphql_handler < Query , Mutation , Subscription , Context , S > ( schema : & juniper :: RootNode < ' static , Query , Mutation , Subscription , S > , context : & Context , req : HttpRequest , payload : actix_web :: web :: Payload , ) -> Result < HttpResponse , Error > where S : ScalarValue + Send + Sync + ' static , Context : Send + Sync + ' static , Query : juniper :: GraphQLTypeAsync < S , Context = Context > + Send + Sync + ' static , Query :: TypeInfo : Send + Sync , Mutation : juniper :: GraphQLTypeAsync < S , Context = Context > + Send + Sync + ' static , Mutation :: TypeInfo : Send + Sync , Subscription : juniper :: GraphQLSubscriptionType < S , Context = Context > + Send + Sync + ' static , Subscription :: TypeInfo : Send + Sync , { let content_type_header = req . headers ( ) . get ( CONTENT_TYPE ) . and_then ( | hv | hv . to_str ( ) . ok ( ) ) ; let req = match content_type_header { Some ( " " ) | Some ( " " ) => { let body_string = String :: from_request ( & req , & mut payload . into_inner ( ) ) . await ; let body_string = body_string ? ; match serde_json :: from_str :: < GraphQLBatchRequest < S > > ( & body_string ) { Ok ( req ) => Ok ( req ) , Err ( err ) => Err ( ErrorBadRequest ( err ) ) , } } _ => Err ( ErrorUnsupportedMediaType ( " " , ) ) , } ? ; let gql_batch_response = req . execute ( schema , context ) . await ; let gql_response = serde_json :: to_string ( & gql_batch_response ) ? ; let mut response = match gql_batch_response . is_ok ( ) { true => HttpResponse :: Ok ( ) , false => HttpResponse :: BadRequest ( ) , } ; Ok ( response . content_type ( " " ) . body ( gql_response ) ) }
Actix GraphQL Handler for POST requests
Actix GraphQL Handler for POST requests
schema
&juniper::RootNode<'static, Query, Mutation, Subscription, S>
context
&Context
req
HttpRequest
payload
actix_web::web::Payload