Enum async_graphql::BatchRequest
source · Expand description
Batch support for GraphQL requests, which is either a single query, or an array of queries
Reference: https://www.apollographql.com/blog/batching-client-graphql-queries-a685f5bcd41b/
Variants§
Implementations§
source§impl BatchRequest
impl BatchRequest
sourcepub fn into_single(self) -> Result<Request, ParseRequestError>
pub fn into_single(self) -> Result<Request, ParseRequestError>
Attempt to convert the batch request into a single request.
Errors
Fails if the batch request is a list of requests with a message saying that batch requests aren’t supported.
Examples found in repository?
src/http/mod.rs (line 70)
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
pub async fn receive_body(
content_type: Option<impl AsRef<str>>,
body: impl AsyncRead + Send,
opts: MultipartOptions,
) -> Result<Request, ParseRequestError> {
receive_batch_body(content_type, body, opts)
.await?
.into_single()
}
/// Receive a GraphQL request from a content type and body.
pub async fn receive_batch_body(
content_type: Option<impl AsRef<str>>,
body: impl AsyncRead + Send,
opts: MultipartOptions,
) -> Result<BatchRequest, ParseRequestError> {
// if no content-type header is set, we default to json
let content_type = content_type
.as_ref()
.map(AsRef::as_ref)
.unwrap_or("application/json");
let content_type: mime::Mime = content_type.parse()?;
match (content_type.type_(), content_type.subtype()) {
// try to use multipart
(mime::MULTIPART, _) => {
if let Some(boundary) = content_type.get_param("boundary") {
multipart::receive_batch_multipart(body, boundary.to_string(), opts).await
} else {
Err(ParseRequestError::InvalidMultipart(
multer::Error::NoBoundary,
))
}
}
// application/json or cbor (currently)
// cbor is in application/octet-stream.
// Note: cbor will only match if feature ``cbor`` is active
// TODO: wait for mime to add application/cbor and match against that too
_ => receive_batch_body_no_multipart(&content_type, body).await,
}
}
/// Receives a GraphQL query which is either cbor or json but NOT multipart
/// This method is only to avoid recursive calls with [``receive_batch_body``]
/// and [``multipart::receive_batch_multipart``]
pub(super) async fn receive_batch_body_no_multipart(
content_type: &mime::Mime,
body: impl AsyncRead + Send,
) -> Result<BatchRequest, ParseRequestError> {
assert_ne!(content_type.type_(), mime::MULTIPART, "received multipart");
match (content_type.type_(), content_type.subtype()) {
#[cfg(feature = "cbor")]
// cbor is in application/octet-stream.
// TODO: wait for mime to add application/cbor and match against that too
(mime::OCTET_STREAM, _) | (mime::APPLICATION, mime::OCTET_STREAM) => {
receive_batch_cbor(body).await
}
// default to json
_ => receive_batch_json(body).await,
}
}
/// Receive a GraphQL request from a body as JSON.
pub async fn receive_json(body: impl AsyncRead) -> Result<Request, ParseRequestError> {
receive_batch_json(body).await?.into_single()
}
/// Receive a GraphQL batch request from a body as JSON.
pub async fn receive_batch_json(body: impl AsyncRead) -> Result<BatchRequest, ParseRequestError> {
let mut data = Vec::new();
futures_util::pin_mut!(body);
body.read_to_end(&mut data)
.await
.map_err(ParseRequestError::Io)?;
serde_json::from_slice::<BatchRequest>(&data)
.map_err(|e| ParseRequestError::InvalidRequest(Box::new(e)))
}
/// Receive a GraphQL request from a body as CBOR.
#[cfg(feature = "cbor")]
#[cfg_attr(docsrs, doc(cfg(feature = "cbor")))]
pub async fn receive_cbor(body: impl AsyncRead) -> Result<Request, ParseRequestError> {
receive_batch_cbor(body).await?.into_single()
}
sourcepub fn iter_mut(&mut self) -> impl Iterator<Item = &mut Request>
pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut Request>
Returns an iterator that allows modifying each request.
Examples found in repository?
src/request.rs (line 230)
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
pub fn variables(mut self, variables: Variables) -> Self {
for request in self.iter_mut() {
request.variables = variables.clone();
}
self
}
/// Insert some data for for each requests.
#[must_use]
pub fn data<D: Any + Clone + Send + Sync>(mut self, data: D) -> Self {
for request in self.iter_mut() {
request.data.insert(data.clone());
}
self
}
/// Disable introspection queries for each request.
#[must_use]
pub fn disable_introspection(mut self) -> Self {
for request in self.iter_mut() {
request.introspection_mode = IntrospectionMode::Disabled;
}
self
}
/// Only allow introspection queries for each request.
#[must_use]
pub fn introspection_only(mut self) -> Self {
for request in self.iter_mut() {
request.introspection_mode = IntrospectionMode::IntrospectionOnly;
}
self
}
sourcepub fn data<D: Any + Clone + Send + Sync>(self, data: D) -> Self
pub fn data<D: Any + Clone + Send + Sync>(self, data: D) -> Self
Insert some data for for each requests.
sourcepub fn disable_introspection(self) -> Self
pub fn disable_introspection(self) -> Self
Disable introspection queries for each request.
sourcepub fn introspection_only(self) -> Self
pub fn introspection_only(self) -> Self
Only allow introspection queries for each request.
Trait Implementations§
source§impl Debug for BatchRequest
impl Debug for BatchRequest
source§impl<'de> Deserialize<'de> for BatchRequest
impl<'de> Deserialize<'de> for BatchRequest
source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more