Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 2 additions & 64 deletions juniper_iron/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down