juniper/juniper_axum/src/response.rs
Benno Tielen 761710205a
Provide axum integration (#1088, #986, #1184)
- create `juniper_axum` crate in Cargo workspace
- implement `graphql` default `axum` handler for processing GraphQL requests
- implement `extract::JuniperRequest` and `response::JuniperResponse` for custom processing GraphQL requests
- implement `subscriptions::graphql_transport_ws()` default `axum` handler for processing the new `graphql-transport-ws` GraphQL over WebSocket Protocol
- implement `subscriptions::graphql_ws()` default `axum` handler for processing the legacy `graphql-ws` GraphQL over WebSocket Protocol
- implement `subscriptions::serve_graphql_transport_ws()` function for custom processing the new `graphql-transport-ws` GraphQL over WebSocket Protocol
- implement `subscriptions::serve_graphql_ws()` function for custom processing the legacy `graphql-ws` GraphQL over WebSocket Protocol
- provide `examples/simple.rs` of default `juniper_axum` integration
- provide `examples/custom.rs` of custom `juniper_axum` integration

Additionally:
- fix `junper_actix` crate MSRV to 1.73
- add `test_post_with_variables()` case to integration `juniper::http::tests`

Co-authored-by: ilslv <ilya.solovyiov@gmail.com>
Co-authored-by: Christian Legnitto <LegNeato@users.noreply.github.com>
Co-authored-by: Kai Ren <tyranron@gmail.com>
2023-11-09 11:57:00 +01:00

24 lines
708 B
Rust

//! [`JuniperResponse`] definition.
use axum::{
http::StatusCode,
response::{IntoResponse, Response},
Json,
};
use juniper::{http::GraphQLBatchResponse, DefaultScalarValue, ScalarValue};
/// Wrapper around a [`GraphQLBatchResponse`], implementing [`IntoResponse`], so it can be returned
/// from [`axum`] handlers.
pub struct JuniperResponse<S = DefaultScalarValue>(pub GraphQLBatchResponse<S>)
where
S: ScalarValue;
impl<S: ScalarValue> IntoResponse for JuniperResponse<S> {
fn into_response(self) -> Response {
if self.0.is_ok() {
Json(self.0).into_response()
} else {
(StatusCode::BAD_REQUEST, Json(self.0)).into_response()
}
}
}