graphql_starter/graphql/handler.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
use async_graphql::http::{AltairSource, Credentials, GraphiQLSource};
use axum::response::{Html, IntoResponse};
/// Handler that renders a GraphiQL playground on the given path to explore the API.
pub async fn graphiql_playground_handler(path: String, title: &str) -> impl IntoResponse {
Html(
GraphiQLSource::build()
.endpoint(&path)
.subscription_endpoint(&format!("{path}/ws"))
.title(title)
.credentials(Credentials::SameOrigin)
.header("x-requested-with", "graphiql")
.finish(),
)
}
/// Handler that renders an Altair GraphQL playground on the given path to explore the API.
pub async fn altair_playground_handler(path: String, title: &str) -> impl IntoResponse {
Html(
AltairSource::build()
.title(title)
.options(serde_json::json!({
"endpointURL": path,
"subscriptionsEndpoint": format!("{path}/ws"),
"subscriptionsProtocol": "wss",
"disableAccount": true,
"initialHeaders": {
"x-requested-with": "altair"
},
"initialSettings": {
"addQueryDepthLimit": 1,
"request.withCredentials": true,
"plugin.list": ["altair-graphql-plugin-graphql-explorer"],
"schema.reloadOnStart": true,
}
}))
.finish(),
)
}
#[cfg(feature = "auth")]
mod auth {
use std::sync::Arc;
use async_graphql::{
http::ALL_WEBSOCKET_PROTOCOLS, BatchRequest, BatchResponse, Data, ObjectType, Response, Schema,
SubscriptionType,
};
use async_graphql_axum::{GraphQLProtocol, GraphQLResponse, GraphQLWebSocket};
use axum::{
extract::{FromRequestParts, WebSocketUpgrade},
response::IntoResponse,
};
use futures_util::{stream::FuturesOrdered, StreamExt};
use tracing::Instrument;
use crate::{
auth::{AuthErrorCode, AuthState, Subject},
axum::{extract::Extension, CorsState},
error::{ApiError, GenericErrorCode, MapToErr},
graphql::GraphQLBatchRequest,
request_id::RequestId,
};
/// Middleware to customize the data attached to each GraphQL request.
pub trait RequestDataMiddleware<S: Subject>: Send + Sync + 'static {
/// Customize the given request data, inserting or modifying the content.
fn customize_request_data(&self, subject: Arc<Option<S>>, data: &mut Data);
}
/// Handler for [batch requests](https://www.apollographql.com/blog/apollo-client/performance/batching-client-graphql-queries/).
///
/// Both [`Arc<Option<Subject>>`](Subject) and [RequestId] will be added to the GraphQL context before executing the
/// request on the schema.
///
/// This handler expects four extensions:
/// - `Schema<Query, Mutation, Subscription>` with the GraphQL [Schema]
/// - `Arc<RequestDataMiddleware>` with the [RequestDataMiddleware]
/// - `Arc<Option<Subject>>` with the subject (see [CheckAuth](crate::auth::CheckAuth))
/// - `RequestId` with the request id (see [RequestIdLayer](crate::request_id::RequestIdLayer))
pub async fn graphql_batch_handler<S: Subject, M: RequestDataMiddleware<S>, Query, Mutation, Subscription>(
Extension(schema): Extension<Schema<Query, Mutation, Subscription>>,
Extension(data_middleware): Extension<Arc<M>>,
Extension(subject): Extension<Arc<Option<S>>>,
Extension(request_id): Extension<RequestId>,
req: GraphQLBatchRequest,
) -> GraphQLResponse
where
Query: ObjectType + 'static,
Mutation: ObjectType + 'static,
Subscription: SubscriptionType + 'static,
{
let mut req = req.into_inner();
// Log request operations
if tracing::event_enabled!(tracing::Level::TRACE) {
let op_names = req
.iter()
.map(|r| r.operation_name.as_deref().unwrap_or("Unknown"))
.collect::<Vec<_>>()
.join(", ");
tracing::trace!("request operations: {op_names}")
}
// Include the subject and request_id from the Axum extension into the GraphQL context as well
req = req.data(subject.clone()).data(request_id);
// Call the request data middleware
match &mut req {
BatchRequest::Single(r) => {
data_middleware.customize_request_data(subject, &mut r.data);
}
BatchRequest::Batch(b) => {
for r in b {
data_middleware.customize_request_data(subject.clone(), &mut r.data);
}
}
}
// Execute the requests, instrumenting them with the operation name (if present)
let mut res = match req {
BatchRequest::Single(request) => {
let span = if let Some(op) = &request.operation_name {
tracing::info_span!("gql", %op)
} else {
tracing::info_span!("gql")
};
BatchResponse::Single(schema.execute(request).instrument(span).await)
}
BatchRequest::Batch(requests) => BatchResponse::Batch(
FuturesOrdered::from_iter(requests.into_iter().map(|request| {
let span = if let Some(op) = &request.operation_name {
tracing::info_span!("gql", %op)
} else {
tracing::info_span!("gql")
};
schema.execute(request).instrument(span)
}))
.collect()
.await,
),
};
// Include the request id if any error is found
match &mut res {
BatchResponse::Single(res) => include_request_id(res, &request_id),
BatchResponse::Batch(responses) => {
for res in responses {
include_request_id(res, &request_id);
}
}
}
res.into()
}
/// Handler for GraphQL [subscriptions](https://www.apollographql.com/docs/react/data/subscriptions/).
///
/// **Note**: This handler only works with `GET` method, it must always be used with [`get`](axum::routing::get).
///
/// Both [`Arc<Option<Subject>>`](Subject) and [RequestId] will be added to the GraphQL context before executing the
/// request on the schema.
///
/// This handler expects three extensions:
/// - `Schema<Query, Mutation, Subscription>` with the GraphQL [Schema]
/// - `Arc<RequestDataMiddleware>` with the [RequestDataMiddleware]
/// - `RequestId` with the request id (see [RequestIdLayer](crate::request_id::RequestIdLayer))
///
/// Authentication will be performed using the same criteria than [CheckAuth](crate::auth::CheckAuth) extractor,
/// retrieving the Cookie from the `GET` request and the token from the
/// [`GQL_CONNECTION_INIT` message](https://github.com/apollographql/subscriptions-transport-ws/blob/master/PROTOCOL.md#gql_connection_init).
pub async fn graphql_subscription_handler<
Query,
Mutation,
Subscription,
S: Subject,
M: RequestDataMiddleware<S>,
B,
>(
Extension(schema): Extension<Schema<Query, Mutation, Subscription>>,
Extension(data_middleware): Extension<Arc<M>>,
Extension(request_id): Extension<RequestId>,
axum::extract::State(CorsState { cors }): axum::extract::State<CorsState>,
axum::extract::State(AuthState { authn, authz: _ }): axum::extract::State<AuthState<S>>,
req: http::Request<B>,
) -> axum::response::Response
where
Query: ObjectType + 'static,
Mutation: ObjectType + 'static,
Subscription: SubscriptionType + 'static,
{
let (mut parts, _body) = req.into_parts();
// Retrieve `Origin` header set by browsers
let origin_header = match parts
.headers
.get(http::header::ORIGIN)
.map(|v| {
v.to_str()
.map_to_err(GenericErrorCode::BadRequest, "Couldn't parse request cookies")
})
.transpose()
{
Ok(o) => o,
Err(err) => return ApiError::from(err).into_response(),
};
// If it's present, check it's allowed
if let Some(origin_header) = origin_header {
if !cors.allowed_origins().iter().any(|o| o == origin_header) {
return ApiError::from((GenericErrorCode::Forbidden, "The origin is not allowed")).into_response();
}
}
// Retrieve token & cookie names
let auth_header_name = authn.header_name().to_lowercase();
let auth_cookie_name = authn.cookie_name().to_owned();
// Retrieve the auth cookie value
let cookies = match parts
.headers
.get(http::header::COOKIE)
.map(|v| {
v.to_str()
.map_to_err(AuthErrorCode::AuthMalformedCookies, "Couldn't parse request cookies")
})
.transpose()
{
Ok(c) => c,
Err(err) => return ApiError::from(err).into_response(),
};
let auth_cookie_value = cookies
.and_then(|cookies| {
cookies
.split("; ")
.find_map(|cookie| cookie.strip_prefix(&format!("{auth_cookie_name}=")))
})
.map(|s| s.to_owned());
// Based on https://github.com/async-graphql/async-graphql/blob/master/integrations/axum/src/subscription.rs
// Extract GraphQL WebSocket protocol
let protocol = match GraphQLProtocol::from_request_parts(&mut parts, &()).await {
Ok(protocol) => protocol,
Err(err) => return err.into_response(),
};
// Prepare upgrade connection from HTTPS to WSS
let upgrade = match WebSocketUpgrade::from_request_parts(&mut parts, &()).await {
Ok(protocol) => protocol,
Err(err) => return err.into_response(),
};
// Finalize upgrading connection
upgrade
.protocols(ALL_WEBSOCKET_PROTOCOLS)
.on_upgrade(move |stream| {
// Forward the stream to the GraphQL websocket
GraphQLWebSocket::new(stream, schema.clone(), protocol)
.on_connection_init(move |payload| {
// Authenticate the subject on connection init
async move {
let mut data = Data::default();
// Retrieve auth token from the payload
let auth_token = payload.as_object().and_then(|payload| {
payload
.iter()
.find(|(k, _)| k.to_lowercase() == auth_header_name)
.and_then(|(_, v)| v.as_str())
});
// Authenticate the subject
let subject = authn.authenticate(auth_token, auth_cookie_value.as_deref()).await?;
tracing::trace!("Authenticated as {subject}");
let subject = Arc::new(Some(subject));
// Include the subject and request_id from the Axum extension into the GraphQL context
data.insert(subject.clone());
data.insert(request_id);
// Call the request data middleware
data_middleware.customize_request_data(subject, &mut data);
Ok(data)
}
})
.serve()
})
.into_response()
}
/// Includes the request id extension on the response errors (if any)
fn include_request_id(res: &mut Response, id: &RequestId) {
for e in &mut res.errors {
e.extensions
.get_or_insert_with(Default::default)
.set("requestId", id.to_string())
}
}
}
#[cfg(feature = "auth")]
pub use auth::*;