Skip to content

Commit

Permalink
graphql-rust#605 First attempt async interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
Julian VIVES committed Apr 23, 2020
1 parent c09be69 commit 6b11721
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 2 deletions.
112 changes: 112 additions & 0 deletions juniper/src/macros/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,118 @@ macro_rules! graphql_interface {
}
}
);

$crate::__juniper_impl_trait!(
impl<$($scalar)* $(, $lifetimes)* > GraphQLTypeAsync for $name where (
Self: $crate::GraphQLType<$crate::__juniper_insert_generic!($($scalar)+)> + Send + Sync,
Self::Context: Send + Sync,
Self::TypeInfo: Send + Sync,
)
{
#[allow(unused_variables)]
fn resolve_field_async<'__b>(
&'__b $main_self,
info: &'__b Self::TypeInfo,
field: &'__b str,
args: &'__b $crate::Arguments<$crate::__juniper_insert_generic!($($scalar)+)>,
executor: &'__b $crate::Executor<Self::Context, $crate::__juniper_insert_generic!($($scalar)+)>
) -> $crate::BoxFuture<'__b, $crate::ExecutionResult<$crate::__juniper_insert_generic!($($scalar)+)>>
where
$crate::__juniper_insert_generic!($($scalar)+): Send + Sync,
{
use $crate::GraphQLType;
let v = $main_self.resolve_field(info, field, args, executor);
Box::pin(futures::future::ready(v))
}
}
);
/*$crate::__juniper_impl_trait!(
impl<$($scalar)* $(, $lifetimes)* > GraphQLTypeAsync for $name
{
/*fn resolve_into_type_async<$($lifetimes)? >(
&$($lifetimes)? $main_self,
info: &$($lifetimes)? Self::TypeInfo,
type_name: &str,
_: Option<$($lifetimes)? [$crate::Selection<$($lifetimes,)? $crate::__juniper_insert_generic!($($scalar)*)>]>,
executor: &$($lifetimes)? $crate::Executor<$($lifetimes,)? $($lifetimes,)? Self::Context, $crate::__juniper_insert_generic!($($scalar)*)>,
) -> $crate::BoxFuture<$($lifetimes,)? $crate::ExecutionResult<$crate::__juniper_insert_generic!($($scalar)*)>> {
$(let $resolver_ctx = &executor.context();)*
$(
if type_name == (<$resolver_src as $crate::GraphQLType<_>>::name(&())).unwrap() {
return executor.resolve_async(&(), &$resolver_expr);
}
)*
panic!("Concrete type not handled by instance resolvers on {}", $($outname)*);
}*/
}
);*/
/*$crate::__juniper_impl_trait!(
impl<$($scalar)* $(, $lifetimes)* > GraphQLTypeAsync for $name {
fn resolve_into_type_async<$($lifetimes)* >(
&$($lifetimes)* $main_self,
info: &$($lifetimes)* Self::TypeInfo,
type_name: &str,
_: Option<'___a, [$crate::Selection<'___a, $crate::__juniper_insert_generic!($($scalar)*)>]>,
executor: &'___a $crate::Executor<'___a, '___a, Self::Context, $crate::__juniper_insert_generic!($($scalar)*)>,
) -> $crate::BoxFuture<'___a, $crate::ExecutionResult<$crate::__juniper_insert_generic!($($scalar)*)>> {
$(let $resolver_ctx = &executor.context();)*
$(
if type_name == (<$resolver_src as $crate::GraphQLType<_>>::name(&())).unwrap() {
return executor.resolve_async(&(), &$resolver_expr);
}
)*
panic!("Concrete type not handled by instance resolvers on {}", $($outname)*);
}
#[allow(unused_variables)]
fn resolve_field_async<'___a>(
&'___a $main_self,
_info: &'___a Self::TypeInfo,
_field_name: &'___a str,
_arguments: &'___a $crate::Arguments<$crate::__juniper_insert_generic!($($scalar)+)>,
_executor: &'___a $crate::Executor<Self::Context, $crate::__juniper_insert_generic!($($scalar)+)>,
) -> $crate::BoxFuture<'___a, $crate::ExecutionResult<$crate::__juniper_insert_generic!($($scalar)+)>> {
$(
if _field_name == &$crate::to_camel_case(stringify!($fn_name)) {
return Box::pin(async move {
let f = (|| {
$(
let $arg_name: $arg_ty = args.get(&$crate::to_camel_case(stringify!($arg_name)))
.expect(concat!(
"Argument ",
stringify!($arg_name),
" missing - validation must have failed"
));
)*
$(
let $executor = &_executor;
)*
$body
});
let result: $return_ty = f();
let resolve = $crate::IntoResolvable::into(result, _executor.context());
match resolve {
Ok(Some((ctx, r))) => {
_executor.replaced_context(ctx)
.resolve_with_ctx_async(&(), &r)
.await
},
Ok(None) => Ok($crate::Value::null()),
Err(e) => Err(e)
}
});
}
)*
panic!("Field {} not found on type {}", _field_name, $($outname)*)
}
}
);*/
};

(
Expand Down
4 changes: 2 additions & 2 deletions juniper/src/macros/tests/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,13 @@ graphql_interface!(ResolversWithTrailingComma: () |&self| {

#[crate::graphql_object_internal(
// FIXME: make async work
noasync
)]
impl<'a> Root {
fn custom_name() -> CustomName {
CustomName {}
}

fn with_lifetime() -> WithLifetime<'a> {
async fn with_lifetime() -> WithLifetime<'a> {
WithLifetime { data: PhantomData }
}
fn with_generics() -> WithGenerics<i32> {
Expand Down Expand Up @@ -254,6 +253,7 @@ async fn introspect_with_generics() {
.await;
}


#[tokio::test]
async fn introspect_description_first() {
run_type_info_query("DescriptionFirst", |object, fields| {
Expand Down

0 comments on commit 6b11721

Please sign in to comment.