Update juniper_rocket to central GraphQLBatch* enums (#614)

This commit is contained in:
Christian Legnitto 2020-04-09 23:24:33 -10:00 committed by GitHub
parent 6b040c509b
commit 2618100140
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 5 additions and 81 deletions

View file

@ -247,8 +247,6 @@ where
QueryT: crate::GraphQLType<S, Context = CtxT>,
MutationT: crate::GraphQLType<S, Context = CtxT>,
SubscriptionT: crate::GraphQLType<S, Context = CtxT>,
SubscriptionT::TypeInfo: Send + Sync,
CtxT: Send + Sync,
{
match *self {
GraphQLBatchRequest::Single(ref request) => {

View file

@ -54,81 +54,9 @@ use rocket::{
use juniper::{http, InputValue};
use juniper::{
serde::Deserialize, DefaultScalarValue, FieldError, GraphQLType, RootNode, ScalarValue,
http::GraphQLBatchRequest, DefaultScalarValue, FieldError, GraphQLType, RootNode, ScalarValue,
};
#[derive(Debug, serde_derive::Deserialize, PartialEq)]
#[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, SubscriptionT>(
&'a self,
root_node: &'a RootNode<QueryT, MutationT, SubscriptionT, S>,
context: &CtxT,
) -> GraphQLBatchResponse<'a, S>
where
QueryT: GraphQLType<S, Context = CtxT>,
MutationT: GraphQLType<S, Context = CtxT>,
SubscriptionT: 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(),
),
}
}
pub fn operation_names(&self) -> Vec<Option<&str>> {
match self {
GraphQLBatchRequest::Single(req) => vec![req.operation_name()],
GraphQLBatchRequest::Batch(reqs) => {
reqs.iter().map(|req| req.operation_name()).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())
}
}
}
}
/// Simple wrapper around an incoming GraphQL request
///
/// See the `http` module for more information. This type can be constructed

View file

@ -65,7 +65,7 @@ use juniper::{
#[derive(Debug, PartialEq)]
pub struct GraphQLRequest<S = DefaultScalarValue>(GraphQLBatchRequest<S>)
where
S: ScalarValue + Send + Sync;
S: ScalarValue;
/// Simple wrapper around the result of executing a GraphQL query
pub struct GraphQLResponse(pub Status, pub String);
@ -85,9 +85,9 @@ pub fn playground_source(graphql_endpoint_url: &str) -> content::Html<String> {
impl<S> GraphQLRequest<S>
where
S: ScalarValue + Sync + Send,
S: ScalarValue,
{
/// Execute an incoming GraphQL query synchronously.
/// Synchronously execute an incoming GraphQL query.
pub fn execute_sync<CtxT, QueryT, MutationT, SubscriptionT>(
&self,
root_node: &RootNode<QueryT, MutationT, SubscriptionT, S>,
@ -97,8 +97,6 @@ where
QueryT: GraphQLType<S, Context = CtxT>,
MutationT: GraphQLType<S, Context = CtxT>,
SubscriptionT: GraphQLType<S, Context = CtxT>,
SubscriptionT::TypeInfo: Send + Sync,
CtxT: Send + Sync,
{
let response = self.0.execute_sync(root_node, context);
let status = if response.is_ok() {
@ -111,7 +109,7 @@ where
GraphQLResponse(status, json)
}
/// Asynchronously execute an incoming GraphQL query
/// Asynchronously execute an incoming GraphQL query.
pub async fn execute<CtxT, QueryT, MutationT, SubscriptionT>(
&self,
root_node: &RootNode<'_, QueryT, MutationT, SubscriptionT, S>,