Skip to content

Commit 4834349

Browse files
committed
Rebase onto async-await
1 parent 34c380b commit 4834349

File tree

12 files changed

+151
-130
lines changed

12 files changed

+151
-130
lines changed

juniper/Cargo.toml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,17 @@ default = [
3535
[dependencies]
3636
juniper_codegen = { version = "0.14.0", path = "../juniper_codegen" }
3737

38+
async-trait = "0.1.16"
39+
chrono = { version = "0.4.0", optional = true }
3840
fnv = "1.0.3"
41+
futures-preview = { version = "=0.3.0-alpha.19", optional = true }
3942
indexmap = { version = "1.0.0", features = ["serde-1"] }
4043
serde = { version = "1.0.8" }
4144
serde_derive = { version = "1.0.2" }
42-
43-
chrono = { version = "0.4.0", optional = true }
4445
serde_json = { version="1.0.2", optional = true }
4546
url = { version = "2", optional = true }
4647
uuid = { version = "0.7", optional = true }
4748

48-
futures-preview = { version = "=0.3.0-alpha.19", optional = true }
49-
5049
[dev-dependencies]
5150
bencher = "0.1.2"
5251
serde_json = { version = "1.0.2" }

juniper/src/macros/common.rs

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -96,25 +96,6 @@ macro_rules! __juniper_insert_generic {
9696
};
9797
}
9898

99-
// TODO: remove me.
100-
#[doc(hidden)]
101-
#[macro_export]
102-
macro_rules! __juniper_extract_generic {
103-
(<$name:ident>) => {
104-
$name
105-
};
106-
(
107-
<$generic:tt $(: $bound: tt)*>
108-
) => {
109-
$generic
110-
};
111-
(
112-
$scalar: ty
113-
) => {
114-
$scalar
115-
};
116-
}
117-
11899
#[doc(hidden)]
119100
#[macro_export]
120101
macro_rules! __juniper_parse_object_header {

juniper/src/macros/scalar.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -423,12 +423,16 @@ macro_rules! graphql_scalar {
423423
)
424424
{
425425

426-
fn resolve_async<'a>(
426+
fn resolve_async<'a, 'async_trait>(
427427
&'a self,
428428
info: &'a Self::TypeInfo,
429-
selection_set: Option<&'a [$crate::Selection<$crate::__juniper_insert_generic!($($scalar)+)>]>,
430-
executor: &'a $crate::Executor<Self::Context, $crate::__juniper_insert_generic!($($scalar)+)>,
431-
) -> futures::future::BoxFuture<'a, $crate::Value<$crate::__juniper_insert_generic!($($scalar)+)>> {
429+
selection_set: Option<&'a [$crate::Selection<'a, $crate::__juniper_insert_generic!($($scalar)+)>]>,
430+
executor: &'a $crate::Executor<'a, Self::Context, $crate::__juniper_insert_generic!($($scalar)+)>,
431+
) -> futures::future::BoxFuture<'async_trait, $crate::Value<$crate::__juniper_insert_generic!($($scalar)+)>>
432+
where
433+
'a: 'async_trait,
434+
Self: 'async_trait,
435+
{
432436
use $crate::GraphQLType;
433437
use futures::future;
434438
let v = self.resolve(info, selection_set, executor);

juniper/src/macros/tests/args.rs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,17 @@ impl Root {
7373
0
7474
}
7575

76-
// TODO: enable once [RFC 2565](https://github.com/rust-lang/rust/issues/60406) is implemented
77-
// fn attr_arg_descr(#[doc = "The arg"] arg: i32) -> i32 { 0 }
78-
// fn attr_arg_descr_collapse(
79-
// #[doc = "The arg"]
80-
// #[doc = "and more details"]
81-
// arg: i32,
82-
// ) -> i32 { 0 }
76+
// TODO: enable once [parameter attributes are supported by proc macros]
77+
// (https://github.com/graphql-rust/juniper/pull/441)
78+
// fn attr_arg_descr(
79+
// #[graphql(description = "The arg")]
80+
// arg: i32) -> i32
81+
// { 0 }
82+
// fn attr_arg_descr_collapse(
83+
// #[graphql(description = "The first arg")]
84+
// #[graphql(description = "and more details")]
85+
// arg: i32,
86+
// ) -> i32 { 0 }
8387

8488
#[graphql(arguments(arg(default = 123,),))]
8589
fn arg_with_default(arg: i32) -> i32 {
@@ -559,7 +563,8 @@ fn introspect_field_multi_args_descr_trailing_comma() {
559563
});
560564
}
561565

562-
// TODO: enable once [RFC 2565](https://github.com/rust-lang/rust/issues/60406) is implemented
566+
// TODO: enable once [parameter attributes are supported by proc macros]
567+
// (https://github.com/graphql-rust/juniper/pull/441)
563568
// #[test]
564569
// fn introspect_field_attr_arg_descr() {
565570
// run_args_info_query("attrArgDescr", |args| {
@@ -593,7 +598,8 @@ fn introspect_field_multi_args_descr_trailing_comma() {
593598
// });
594599
// }
595600

596-
// TODO: enable once [RFC 2565](https://github.com/rust-lang/rust/issues/60406) is implemented
601+
// TODO: enable once [parameter attributes are supported by proc macros]
602+
// (https://github.com/graphql-rust/juniper/pull/441)
597603
// #[test]
598604
// fn introspect_field_attr_arg_descr_collapse() {
599605
// run_args_info_query("attrArgDescrCollapse", |args| {

juniper/src/schema/schema.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ where
7777
}
7878

7979
#[cfg(feature = "async")]
80+
#[async_trait::async_trait]
8081
impl<'a, CtxT, S, QueryT, MutationT> crate::GraphQLTypeAsync<S>
8182
for RootNode<'a, QueryT, MutationT, S>
8283
where
@@ -85,16 +86,16 @@ where
8586
QueryT::TypeInfo: Send + Sync,
8687
MutationT: crate::GraphQLTypeAsync<S, Context = CtxT>,
8788
MutationT::TypeInfo: Send + Sync,
88-
CtxT: Send + Sync,
89-
for<'b> &'b S: ScalarRefValue<'b>,
89+
CtxT: Send + Sync + 'a,
90+
for<'c> &'c S: ScalarRefValue<'c>,
9091
{
91-
fn resolve_field_async<'b>(
92+
async fn resolve_field_async<'b>(
9293
&'b self,
93-
info: &'b Self::TypeInfo,
94+
info: &'b <Self as crate::GraphQLType<S>>::TypeInfo,
9495
field_name: &'b str,
95-
arguments: &'b Arguments<S>,
96-
executor: &'b Executor<Self::Context, S>,
97-
) -> crate::BoxFuture<'b, ExecutionResult<S>> {
96+
arguments: &'b Arguments<'b, S>,
97+
executor: &'b Executor<'b, <Self as crate::GraphQLType<S>>::Context, S>,
98+
) -> ExecutionResult<S> {
9899
use futures::future::{ready, FutureExt};
99100
match field_name {
100101
"__schema" | "__type" => {

juniper/src/types/async_await.rs

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,35 +12,50 @@ use crate::BoxFuture;
1212

1313
use super::base::{is_excluded, merge_key_into, Arguments, GraphQLType};
1414

15+
#[async_trait::async_trait]
1516
pub trait GraphQLTypeAsync<S>: GraphQLType<S> + Send + Sync
1617
where
1718
Self::Context: Send + Sync,
1819
Self::TypeInfo: Send + Sync,
1920
S: ScalarValue + Send + Sync,
2021
for<'b> &'b S: ScalarRefValue<'b>,
2122
{
22-
fn resolve_field_async<'a>(
23+
async fn resolve_field_async<'a>(
2324
&'a self,
2425
info: &'a Self::TypeInfo,
2526
field_name: &'a str,
26-
arguments: &'a Arguments<S>,
27-
executor: &'a Executor<Self::Context, S>,
28-
) -> BoxFuture<'a, ExecutionResult<S>> {
27+
arguments: &'a Arguments<'a, S>,
28+
executor: &'a Executor<'a, Self::Context, S>,
29+
) -> ExecutionResult<S> {
2930
panic!("resolve_field must be implemented by object types");
3031
}
3132

32-
fn resolve_async<'a>(
33+
async fn resolve_async<'a>(
3334
&'a self,
3435
info: &'a Self::TypeInfo,
35-
selection_set: Option<&'a [Selection<S>]>,
36-
executor: &'a Executor<Self::Context, S>,
37-
) -> BoxFuture<'a, Value<S>> {
36+
selection_set: Option<&'a [Selection<'a, S>]>,
37+
executor: &'a Executor<'a, Self::Context, S>,
38+
) -> Value<S> {
3839
if let Some(selection_set) = selection_set {
39-
resolve_selection_set_into_async(self, info, selection_set, executor)
40+
resolve_selection_set_into_async(self, info, selection_set, executor).await
4041
} else {
4142
panic!("resolve() must be implemented by non-object output types");
4243
}
4344
}
45+
46+
async fn resolve_into_type_async<'a>(
47+
&'a self,
48+
info: &'a Self::TypeInfo,
49+
type_name: &str,
50+
selection_set: Option<&'a [Selection<'a, S>]>,
51+
executor: &'a Executor<'a, Self::Context, S>,
52+
) -> ExecutionResult<S> {
53+
if Self::name(info).unwrap() == type_name {
54+
Ok(self.resolve_async(info, selection_set, executor).await)
55+
} else {
56+
panic!("resolve_into_type_async must be implemented by unions and interfaces");
57+
}
58+
}
4459
}
4560

4661
// Wrapper function around resolve_selection_set_into_async_recursive.
@@ -160,7 +175,7 @@ where
160175
let response_name = response_name.to_string();
161176
let field_future = async move {
162177
// TODO: implement custom future type instead of
163-
// two-level boxing.
178+
// two-level boxing.
164179
let res = instance
165180
.resolve_field_async(info, f.name.item, &args, &sub_exec)
166181
.await;
@@ -223,14 +238,14 @@ where
223238
);
224239

225240
if let Some(ref type_condition) = fragment.type_condition {
226-
// FIXME: implement async version.
227-
228-
let sub_result = instance.resolve_into_type(
229-
info,
230-
type_condition.item,
231-
Some(&fragment.selection_set[..]),
232-
&sub_exec,
233-
);
241+
let sub_result = instance
242+
.resolve_into_type_async(
243+
info,
244+
type_condition.item,
245+
Some(&fragment.selection_set[..]),
246+
&sub_exec,
247+
)
248+
.await;
234249

235250
if let Ok(Value::Object(obj)) = sub_result {
236251
for (k, v) in obj {

juniper/src/types/containers.rs

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,7 @@ where
257257
}
258258

259259
#[cfg(feature = "async")]
260+
#[async_trait::async_trait]
260261
impl<S, T, CtxT> crate::GraphQLTypeAsync<S> for Vec<T>
261262
where
262263
T: crate::GraphQLTypeAsync<S, Context = CtxT>,
@@ -265,18 +266,18 @@ where
265266
CtxT: Send + Sync,
266267
for<'b> &'b S: ScalarRefValue<'b>,
267268
{
268-
fn resolve_async<'a>(
269+
async fn resolve_async<'a>(
269270
&'a self,
270-
info: &'a Self::TypeInfo,
271-
selection_set: Option<&'a [Selection<S>]>,
272-
executor: &'a Executor<Self::Context, S>,
273-
) -> crate::BoxFuture<'a, Value<S>> {
274-
let f = resolve_into_list_async(executor, info, self.iter());
275-
Box::pin(f)
271+
info: &'a <Self as crate::GraphQLType<S>>::TypeInfo,
272+
selection_set: Option<&'a [Selection<'a, S>]>,
273+
executor: &'a Executor<'a, <Self as crate::GraphQLType<S>>::Context, S>,
274+
) -> Value<S> {
275+
resolve_into_list_async(executor, info, self.iter()).await
276276
}
277277
}
278278

279279
#[cfg(feature = "async")]
280+
#[async_trait::async_trait]
280281
impl<S, T, CtxT> crate::GraphQLTypeAsync<S> for &[T]
281282
where
282283
T: crate::GraphQLTypeAsync<S, Context = CtxT>,
@@ -285,18 +286,18 @@ where
285286
CtxT: Send + Sync,
286287
for<'b> &'b S: ScalarRefValue<'b>,
287288
{
288-
fn resolve_async<'a>(
289+
async fn resolve_async<'a>(
289290
&'a self,
290-
info: &'a Self::TypeInfo,
291-
selection_set: Option<&'a [Selection<S>]>,
292-
executor: &'a Executor<Self::Context, S>,
293-
) -> crate::BoxFuture<'a, Value<S>> {
294-
let f = resolve_into_list_async(executor, info, self.iter());
295-
Box::pin(f)
291+
info: &'a <Self as crate::GraphQLType<S>>::TypeInfo,
292+
selection_set: Option<&'a [Selection<'a, S>]>,
293+
executor: &'a Executor<'a, <Self as crate::GraphQLType<S>>::Context, S>,
294+
) -> Value<S> {
295+
resolve_into_list_async(executor, info, self.iter()).await
296296
}
297297
}
298298

299299
#[cfg(feature = "async")]
300+
#[async_trait::async_trait]
300301
impl<S, T, CtxT> crate::GraphQLTypeAsync<S> for Option<T>
301302
where
302303
T: crate::GraphQLTypeAsync<S, Context = CtxT>,
@@ -305,18 +306,15 @@ where
305306
CtxT: Send + Sync,
306307
for<'b> &'b S: ScalarRefValue<'b>,
307308
{
308-
fn resolve_async<'a>(
309+
async fn resolve_async<'a>(
309310
&'a self,
310-
info: &'a Self::TypeInfo,
311-
selection_set: Option<&'a [Selection<S>]>,
312-
executor: &'a Executor<Self::Context, S>,
313-
) -> crate::BoxFuture<'a, Value<S>> {
314-
let f = async move {
315-
match *self {
316-
Some(ref obj) => executor.resolve_into_value_async(info, obj).await,
317-
None => Value::null(),
318-
}
319-
};
320-
Box::pin(f)
311+
info: &'a <Self as crate::GraphQLType<S>>::TypeInfo,
312+
selection_set: Option<&'a [Selection<'a, S>]>,
313+
executor: &'a Executor<'a, <Self as crate::GraphQLType<S>>::Context, S>,
314+
) -> Value<S> {
315+
match *self {
316+
Some(ref obj) => executor.resolve_into_value_async(info, obj).await,
317+
None => Value::null(),
318+
}
321319
}
322320
}

juniper/src/types/pointers.rs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -137,31 +137,33 @@ where
137137
}
138138

139139
#[cfg(feature = "async")]
140+
#[async_trait::async_trait]
140141
impl<'e, S, T> crate::GraphQLTypeAsync<S> for &'e T
141142
where
142143
S: ScalarValue + Send + Sync,
143144
T: crate::GraphQLTypeAsync<S>,
144145
T::TypeInfo: Send + Sync,
145146
T::Context: Send + Sync,
146-
for<'b> &'b S: ScalarRefValue<'b>,
147+
for<'c> &'c S: ScalarRefValue<'c>,
147148
{
148-
fn resolve_field_async<'b>(
149+
async fn resolve_field_async<'b>(
149150
&'b self,
150-
info: &'b Self::TypeInfo,
151+
info: &'b <Self as crate::GraphQLType<S>>::TypeInfo,
151152
field_name: &'b str,
152-
arguments: &'b Arguments<S>,
153-
executor: &'b Executor<Self::Context, S>,
154-
) -> crate::BoxFuture<'b, ExecutionResult<S>> {
153+
arguments: &'b Arguments<'b, S>,
154+
executor: &'b Executor<'b, <Self as crate::GraphQLType<S>>::Context, S>,
155+
) -> ExecutionResult<S> {
155156
crate::GraphQLTypeAsync::resolve_field_async(&**self, info, field_name, arguments, executor)
157+
.await
156158
}
157159

158-
fn resolve_async<'a>(
160+
async fn resolve_async<'a>(
159161
&'a self,
160-
info: &'a Self::TypeInfo,
161-
selection_set: Option<&'a [Selection<S>]>,
162-
executor: &'a Executor<Self::Context, S>,
163-
) -> crate::BoxFuture<'a, Value<S>> {
164-
crate::GraphQLTypeAsync::resolve_async(&**self, info, selection_set, executor)
162+
info: &'a <Self as crate::GraphQLType<S>>::TypeInfo,
163+
selection_set: Option<&'a [Selection<'a, S>]>,
164+
executor: &'a Executor<'a, <Self as crate::GraphQLType<S>>::Context, S>,
165+
) -> Value<S> {
166+
crate::GraphQLTypeAsync::resolve_async(&**self, info, selection_set, executor).await
165167
}
166168
}
167169

juniper/src/types/scalars.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -197,19 +197,19 @@ where
197197
}
198198

199199
#[cfg(feature = "async")]
200+
#[async_trait::async_trait]
200201
impl<'e, S> crate::GraphQLTypeAsync<S> for &'e str
201202
where
202203
S: ScalarValue + Send + Sync,
203204
for<'b> &'b S: ScalarRefValue<'b>,
204205
{
205-
fn resolve_async<'a>(
206+
async fn resolve_async<'a>(
206207
&'a self,
207-
info: &'a Self::TypeInfo,
208-
selection_set: Option<&'a [Selection<S>]>,
209-
executor: &'a Executor<Self::Context, S>,
210-
) -> crate::BoxFuture<'a, crate::Value<S>> {
211-
use futures::future;
212-
future::FutureExt::boxed(future::ready(self.resolve(info, selection_set, executor)))
208+
info: &'a <Self as crate::GraphQLType<S>>::TypeInfo,
209+
selection_set: Option<&'a [Selection<'a, S>]>,
210+
executor: &'a Executor<'a, <Self as crate::GraphQLType<S>>::Context, S>,
211+
) -> crate::Value<S> {
212+
self.resolve(info, selection_set, executor)
213213
}
214214
}
215215

0 commit comments

Comments
 (0)