Skip to content

Commit 9064d7f

Browse files
authored
Avoid unnecessary Vec allocation when executing GraphQLBatchRequest (#638)
Additionally: - make GraphQLBatchRequest/GraphQLBatchResponse code a bit more laconic
1 parent dc4cdf0 commit 9064d7f

File tree

1 file changed

+18
-23
lines changed

1 file changed

+18
-23
lines changed

juniper/src/http/mod.rs

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -249,13 +249,12 @@ where
249249
SubscriptionT: crate::GraphQLType<S, Context = CtxT>,
250250
{
251251
match *self {
252-
GraphQLBatchRequest::Single(ref request) => {
253-
GraphQLBatchResponse::Single(request.execute_sync(root_node, context))
252+
Self::Single(ref req) => {
253+
GraphQLBatchResponse::Single(req.execute_sync(root_node, context))
254254
}
255-
GraphQLBatchRequest::Batch(ref requests) => GraphQLBatchResponse::Batch(
256-
requests
257-
.iter()
258-
.map(|request| request.execute_sync(root_node, context))
255+
Self::Batch(ref reqs) => GraphQLBatchResponse::Batch(
256+
reqs.iter()
257+
.map(|req| req.execute_sync(root_node, context))
259258
.collect(),
260259
),
261260
}
@@ -281,29 +280,25 @@ where
281280
S: Send + Sync,
282281
{
283282
match *self {
284-
GraphQLBatchRequest::Single(ref request) => {
285-
let res = request.execute(root_node, context).await;
286-
GraphQLBatchResponse::Single(res)
283+
Self::Single(ref req) => {
284+
let resp = req.execute(root_node, context).await;
285+
GraphQLBatchResponse::Single(resp)
287286
}
288-
GraphQLBatchRequest::Batch(ref requests) => {
289-
let futures = requests
290-
.iter()
291-
.map(|request| request.execute(root_node, context))
292-
.collect::<Vec<_>>();
293-
let responses = futures::future::join_all(futures).await;
294-
295-
GraphQLBatchResponse::Batch(responses)
287+
Self::Batch(ref reqs) => {
288+
let resps = futures::future::join_all(
289+
reqs.iter().map(|req| req.execute(root_node, context)),
290+
)
291+
.await;
292+
GraphQLBatchResponse::Batch(resps)
296293
}
297294
}
298295
}
299296

300297
/// The operation names of the request.
301298
pub fn operation_names(&self) -> Vec<Option<&str>> {
302299
match self {
303-
GraphQLBatchRequest::Single(req) => vec![req.operation_name()],
304-
GraphQLBatchRequest::Batch(reqs) => {
305-
reqs.iter().map(|req| req.operation_name()).collect()
306-
}
300+
Self::Single(req) => vec![req.operation_name()],
301+
Self::Batch(reqs) => reqs.iter().map(|req| req.operation_name()).collect(),
307302
}
308303
}
309304
}
@@ -333,8 +328,8 @@ where
333328
/// you can use it to determine wheter to send a 200 or 400 HTTP status code.
334329
pub fn is_ok(&self) -> bool {
335330
match self {
336-
GraphQLBatchResponse::Single(res) => res.is_ok(),
337-
GraphQLBatchResponse::Batch(reses) => reses.iter().all(|res| res.is_ok()),
331+
Self::Single(resp) => resp.is_ok(),
332+
Self::Batch(resps) => resps.iter().all(GraphQLResponse::is_ok),
338333
}
339334
}
340335
}

0 commit comments

Comments
 (0)