Skip to content

Commit b762d4e

Browse files
committed
Fix juniper_rocket
1 parent 644c4f7 commit b762d4e

File tree

1 file changed

+56
-25
lines changed

1 file changed

+56
-25
lines changed

juniper_rocket/src/lib.rs

Lines changed: 56 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -59,33 +59,48 @@ use rocket::Request;
5959
use juniper::http;
6060
use juniper::InputValue;
6161

62+
use juniper::serde::Deserialize;
63+
use juniper::DefaultScalarValue;
6264
use juniper::FieldError;
6365
use juniper::GraphQLType;
6466
use juniper::RootNode;
67+
use juniper::ScalarRefValue;
68+
use juniper::ScalarValue;
6569

6670
#[derive(Debug, Deserialize, PartialEq)]
6771
#[serde(untagged)]
68-
enum GraphQLBatchRequest {
69-
Single(http::GraphQLRequest),
70-
Batch(Vec<http::GraphQLRequest>),
72+
#[serde(bound = "InputValue<S>: Deserialize<'de>")]
73+
enum GraphQLBatchRequest<S = DefaultScalarValue>
74+
where
75+
S: ScalarValue,
76+
{
77+
Single(http::GraphQLRequest<S>),
78+
Batch(Vec<http::GraphQLRequest<S>>),
7179
}
7280

7381
#[derive(Serialize)]
7482
#[serde(untagged)]
75-
enum GraphQLBatchResponse<'a> {
76-
Single(http::GraphQLResponse<'a>),
77-
Batch(Vec<http::GraphQLResponse<'a>>),
83+
enum GraphQLBatchResponse<'a, S = DefaultScalarValue>
84+
where
85+
S: ScalarValue,
86+
{
87+
Single(http::GraphQLResponse<'a, S>),
88+
Batch(Vec<http::GraphQLResponse<'a, S>>),
7889
}
7990

80-
impl GraphQLBatchRequest {
91+
impl<S> GraphQLBatchRequest<S>
92+
where
93+
S: ScalarValue,
94+
for<'b> &'b S: ScalarRefValue<'b>,
95+
{
8196
pub fn execute<'a, CtxT, QueryT, MutationT>(
8297
&'a self,
83-
root_node: &RootNode<QueryT, MutationT>,
98+
root_node: &'a RootNode<QueryT, MutationT, S>,
8499
context: &CtxT,
85-
) -> GraphQLBatchResponse<'a>
100+
) -> GraphQLBatchResponse<'a, S>
86101
where
87-
QueryT: GraphQLType<Context = CtxT>,
88-
MutationT: GraphQLType<Context = CtxT>,
102+
QueryT: GraphQLType<S, Context = CtxT>,
103+
MutationT: GraphQLType<S, Context = CtxT>,
89104
{
90105
match self {
91106
&GraphQLBatchRequest::Single(ref request) => {
@@ -101,7 +116,10 @@ impl GraphQLBatchRequest {
101116
}
102117
}
103118

104-
impl<'a> GraphQLBatchResponse<'a> {
119+
impl<'a, S> GraphQLBatchResponse<'a, S>
120+
where
121+
S: ScalarValue,
122+
{
105123
fn is_ok(&self) -> bool {
106124
match self {
107125
&GraphQLBatchResponse::Single(ref response) => response.is_ok(),
@@ -118,7 +136,9 @@ impl<'a> GraphQLBatchResponse<'a> {
118136
/// automatically from both GET and POST routes by implementing the `FromForm`
119137
/// and `FromData` traits.
120138
#[derive(Debug, PartialEq)]
121-
pub struct GraphQLRequest(GraphQLBatchRequest);
139+
pub struct GraphQLRequest<S = DefaultScalarValue>(GraphQLBatchRequest<S>)
140+
where
141+
S: ScalarValue;
122142

123143
/// Simple wrapper around the result of executing a GraphQL query
124144
pub struct GraphQLResponse(pub Status, pub String);
@@ -128,16 +148,20 @@ pub fn graphiql_source(graphql_endpoint_url: &str) -> content::Html<String> {
128148
content::Html(juniper::graphiql::graphiql_source(graphql_endpoint_url))
129149
}
130150

131-
impl GraphQLRequest {
151+
impl<S> GraphQLRequest<S>
152+
where
153+
S: ScalarValue,
154+
for<'b> &'b S: ScalarRefValue<'b>,
155+
{
132156
/// Execute an incoming GraphQL query
133157
pub fn execute<CtxT, QueryT, MutationT>(
134158
&self,
135-
root_node: &RootNode<QueryT, MutationT>,
159+
root_node: &RootNode<QueryT, MutationT, S>,
136160
context: &CtxT,
137161
) -> GraphQLResponse
138162
where
139-
QueryT: GraphQLType<Context = CtxT>,
140-
MutationT: GraphQLType<Context = CtxT>,
163+
QueryT: GraphQLType<S, Context = CtxT>,
164+
MutationT: GraphQLType<S, Context = CtxT>,
141165
{
142166
let response = self.0.execute(root_node, context);
143167
let status = if response.is_ok() {
@@ -205,7 +229,10 @@ impl GraphQLResponse {
205229
}
206230
}
207231

208-
impl<'f> FromForm<'f> for GraphQLRequest {
232+
impl<'f, S> FromForm<'f> for GraphQLRequest<S>
233+
where
234+
S: ScalarValue,
235+
{
209236
type Error = String;
210237

211238
fn from_form(form_items: &mut FormItems<'f>, strict: bool) -> Result<Self, String> {
@@ -248,8 +275,10 @@ impl<'f> FromForm<'f> for GraphQLRequest {
248275
Ok(v) => decoded = v,
249276
Err(e) => return Err(e.description().to_string()),
250277
}
251-
variables = Some(serde_json::from_str::<InputValue>(&decoded)
252-
.map_err(|err| err.description().to_owned())?);
278+
variables = Some(
279+
serde_json::from_str::<InputValue<_>>(&decoded)
280+
.map_err(|err| err.description().to_owned())?,
281+
);
253282
}
254283
}
255284
_ => {
@@ -270,7 +299,10 @@ impl<'f> FromForm<'f> for GraphQLRequest {
270299
}
271300
}
272301

273-
impl FromData for GraphQLRequest {
302+
impl<S> FromData for GraphQLRequest<S>
303+
where
304+
S: ScalarValue,
305+
{
274306
type Error = String;
275307

276308
fn from_data(request: &Request, data: Data) -> FromDataOutcome<Self, Self::Error> {
@@ -311,7 +343,7 @@ mod fromform_tests {
311343

312344
fn check_error(input: &str, error: &str, strict: bool) {
313345
let mut items = FormItems::from(input);
314-
let result = GraphQLRequest::from_form(&mut items, strict);
346+
let result: Result<GraphQLRequest, _> = GraphQLRequest::from_form(&mut items, strict);
315347
assert!(result.is_err());
316348
assert_eq!(result.unwrap_err(), error);
317349
}
@@ -401,7 +433,7 @@ mod fromform_tests {
401433
fn test_url_decode() {
402434
let form_string = "query=%25foo%20bar+baz%26%3F&operation_name=test";
403435
let mut items = FormItems::from(form_string);
404-
let result = GraphQLRequest::from_form(&mut items, false);
436+
let result: Result<GraphQLRequest, _> = GraphQLRequest::from_form(&mut items, false);
405437
assert!(result.is_ok());
406438
let expected = GraphQLRequest(GraphQLBatchRequest::Single(http::GraphQLRequest::new(
407439
"%foo bar baz&?".to_string(),
@@ -477,8 +509,7 @@ mod tests {
477509
.manage(Schema::new(
478510
Database::new(),
479511
EmptyMutation::<Database>::new(),
480-
))
481-
.mount("/", routes![post_graphql_handler, get_graphql_handler])
512+
)).mount("/", routes![post_graphql_handler, get_graphql_handler])
482513
}
483514

484515
fn make_test_response<'r>(request: &LocalRequest<'r>) -> http_tests::TestResponse {

0 commit comments

Comments
 (0)