Update iron to central GraphQLBatch* enums (#611)

This commit is contained in:
Christian Legnitto 2020-04-09 21:28:36 -10:00 committed by GitHub
parent 926d4e5738
commit 33c3526931
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -122,72 +122,10 @@ use std::{error::Error, fmt, io::Read};
use serde_json::error::Error as SerdeError;
use juniper::{
http, serde::Deserialize, DefaultScalarValue, GraphQLType, InputValue, RootNode, ScalarValue,
http, http::GraphQLBatchRequest, DefaultScalarValue, GraphQLType, InputValue, RootNode,
ScalarValue,
};
#[derive(serde_derive::Deserialize)]
#[serde(untagged)]
#[serde(bound = "InputValue<S>: Deserialize<'de>")]
enum GraphQLBatchRequest<S = DefaultScalarValue>
where
S: ScalarValue,
{
Single(http::GraphQLRequest<S>),
Batch(Vec<http::GraphQLRequest<S>>),
}
#[derive(serde_derive::Serialize)]
#[serde(untagged)]
enum GraphQLBatchResponse<'a, S = DefaultScalarValue>
where
S: ScalarValue,
{
Single(http::GraphQLResponse<'a, S>),
Batch(Vec<http::GraphQLResponse<'a, S>>),
}
impl<S> GraphQLBatchRequest<S>
where
S: ScalarValue,
{
pub fn execute_sync<'a, CtxT, QueryT, MutationT, Subscription>(
&'a self,
root_node: &'a RootNode<QueryT, MutationT, Subscription, S>,
context: &CtxT,
) -> GraphQLBatchResponse<'a, S>
where
QueryT: GraphQLType<S, Context = CtxT>,
MutationT: GraphQLType<S, Context = CtxT>,
Subscription: GraphQLType<S, Context = CtxT>,
{
match *self {
GraphQLBatchRequest::Single(ref request) => {
GraphQLBatchResponse::Single(request.execute_sync(root_node, context))
}
GraphQLBatchRequest::Batch(ref requests) => GraphQLBatchResponse::Batch(
requests
.iter()
.map(|request| request.execute_sync(root_node, context))
.collect(),
),
}
}
}
impl<'a, S> GraphQLBatchResponse<'a, S>
where
S: ScalarValue,
{
fn is_ok(&self) -> bool {
match *self {
GraphQLBatchResponse::Single(ref response) => response.is_ok(),
GraphQLBatchResponse::Batch(ref responses) => {
responses.iter().all(|response| response.is_ok())
}
}
}
}
/// Handler that executes `GraphQL` queries in the given schema
///
/// The handler responds to GET requests and POST requests only. In GET