Format
This commit is contained in:
parent
00dd1dc391
commit
e3c12e31fc
9 changed files with 122 additions and 132 deletions
|
@ -2,13 +2,11 @@
|
||||||
//! This example demonstrates async/await usage with warp.
|
//! This example demonstrates async/await usage with warp.
|
||||||
//! NOTE: this uses tokio 0.1 , not the alpha tokio 0.2.
|
//! NOTE: this uses tokio 0.1 , not the alpha tokio 0.2.
|
||||||
|
|
||||||
use juniper::{EmptyMutation, RootNode, FieldError};
|
use juniper::{EmptyMutation, FieldError, RootNode};
|
||||||
use warp::{http::Response, Filter};
|
use warp::{http::Response, Filter};
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
struct Context {
|
struct Context {}
|
||||||
|
|
||||||
}
|
|
||||||
impl juniper::Context for Context {}
|
impl juniper::Context for Context {}
|
||||||
|
|
||||||
#[derive(juniper::GraphQLEnum, Clone, Copy)]
|
#[derive(juniper::GraphQLEnum, Clone, Copy)]
|
||||||
|
@ -48,18 +46,19 @@ struct Query;
|
||||||
#[juniper::object(Context = Context)]
|
#[juniper::object(Context = Context)]
|
||||||
impl Query {
|
impl Query {
|
||||||
async fn users() -> Vec<User> {
|
async fn users() -> Vec<User> {
|
||||||
vec![
|
vec![User {
|
||||||
User{
|
|
||||||
id: 1,
|
id: 1,
|
||||||
kind: UserKind::Admin,
|
kind: UserKind::Admin,
|
||||||
name: "user1".into(),
|
name: "user1".into(),
|
||||||
},
|
}]
|
||||||
]
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Fetch a URL and return the response body text.
|
/// Fetch a URL and return the response body text.
|
||||||
async fn request(url: String) -> Result<String, FieldError> {
|
async fn request(url: String) -> Result<String, FieldError> {
|
||||||
use futures::{ compat::{Stream01CompatExt, Future01CompatExt}, stream::TryStreamExt};
|
use futures::{
|
||||||
|
compat::{Future01CompatExt, Stream01CompatExt},
|
||||||
|
stream::TryStreamExt,
|
||||||
|
};
|
||||||
|
|
||||||
let res = reqwest::r#async::Client::new()
|
let res = reqwest::r#async::Client::new()
|
||||||
.get(&url)
|
.get(&url)
|
||||||
|
@ -95,7 +94,7 @@ fn main() {
|
||||||
|
|
||||||
log::info!("Listening on 127.0.0.1:8080");
|
log::info!("Listening on 127.0.0.1:8080");
|
||||||
|
|
||||||
let state = warp::any().map(move || Context{} );
|
let state = warp::any().map(move || Context {});
|
||||||
let graphql_filter = juniper_warp::make_graphql_filter_async(schema(), state.boxed());
|
let graphql_filter = juniper_warp::make_graphql_filter_async(schema(), state.boxed());
|
||||||
|
|
||||||
warp::serve(
|
warp::serve(
|
||||||
|
|
|
@ -1,4 +1 @@
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -73,12 +73,12 @@ impl Root {
|
||||||
0
|
0
|
||||||
}
|
}
|
||||||
|
|
||||||
fn attr_arg_descr(#[doc = "The arg"] arg: i32) -> i32 { 0 }
|
fn attr_arg_descr(arg: i32) -> i32 {
|
||||||
fn attr_arg_descr_collapse(
|
0
|
||||||
#[doc = "The arg"]
|
}
|
||||||
#[doc = "and more details"]
|
fn attr_arg_descr_collapse(arg: i32) -> i32 {
|
||||||
arg: i32,
|
0
|
||||||
) -> i32 { 0 }
|
}
|
||||||
|
|
||||||
#[graphql(arguments(arg(default = 123,),))]
|
#[graphql(arguments(arg(default = 123,),))]
|
||||||
fn arg_with_default(arg: i32) -> i32 {
|
fn arg_with_default(arg: i32) -> i32 {
|
||||||
|
@ -558,8 +558,8 @@ fn introspect_field_multi_args_descr_trailing_comma() {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn introspect_field_attr_arg_descr() {
|
fn introspect_field_attr_arg_descr() {
|
||||||
run_args_info_query("attrArgDescr", |args| {
|
run_args_info_query("attrArgDescr", |args| {
|
||||||
assert_eq!(args.len(), 1);
|
assert_eq!(args.len(), 1);
|
||||||
|
|
||||||
|
@ -589,10 +589,10 @@ fn introspect_field_multi_args_descr_trailing_comma() {
|
||||||
.collect(),
|
.collect(),
|
||||||
)));
|
)));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn introspect_field_attr_arg_descr_collapse() {
|
fn introspect_field_attr_arg_descr_collapse() {
|
||||||
run_args_info_query("attrArgDescrCollapse", |args| {
|
run_args_info_query("attrArgDescrCollapse", |args| {
|
||||||
assert_eq!(args.len(), 1);
|
assert_eq!(args.len(), 1);
|
||||||
|
|
||||||
|
@ -622,7 +622,7 @@ fn introspect_field_multi_args_descr_trailing_comma() {
|
||||||
.collect(),
|
.collect(),
|
||||||
)));
|
)));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn introspect_field_arg_with_default() {
|
fn introspect_field_arg_with_default() {
|
||||||
|
|
|
@ -98,13 +98,12 @@ where
|
||||||
) -> ExecutionResult<S> {
|
) -> ExecutionResult<S> {
|
||||||
use futures::future::{ready, FutureExt};
|
use futures::future::{ready, FutureExt};
|
||||||
match field_name {
|
match field_name {
|
||||||
"__schema" | "__type" => {
|
"__schema" | "__type" => self.resolve_field(info, field_name, arguments, executor),
|
||||||
self.resolve_field(info, field_name, arguments, executor)
|
_ => {
|
||||||
}
|
self.query_type
|
||||||
_ => self
|
|
||||||
.query_type
|
|
||||||
.resolve_field_async(info, field_name, arguments, executor)
|
.resolve_field_async(info, field_name, arguments, executor)
|
||||||
.await,
|
.await
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
use async_trait::async_trait;
|
|
||||||
use crate::{
|
use crate::{
|
||||||
ast::{Directive, FromInputValue, InputValue, Selection},
|
ast::{Directive, FromInputValue, InputValue, Selection},
|
||||||
value::{Object, ScalarRefValue, ScalarValue, Value},
|
value::{Object, ScalarRefValue, ScalarValue, Value},
|
||||||
};
|
};
|
||||||
|
use async_trait::async_trait;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
executor::{ExecutionResult, Executor},
|
executor::{ExecutionResult, Executor},
|
||||||
|
@ -239,12 +239,14 @@ where
|
||||||
);
|
);
|
||||||
|
|
||||||
if let Some(ref type_condition) = fragment.type_condition {
|
if let Some(ref type_condition) = fragment.type_condition {
|
||||||
let sub_result = instance.resolve_into_type_async(
|
let sub_result = instance
|
||||||
|
.resolve_into_type_async(
|
||||||
info,
|
info,
|
||||||
type_condition.item,
|
type_condition.item,
|
||||||
Some(&fragment.selection_set[..]),
|
Some(&fragment.selection_set[..]),
|
||||||
&sub_exec,
|
&sub_exec,
|
||||||
).await;
|
)
|
||||||
|
.await;
|
||||||
|
|
||||||
if let Ok(Value::Object(obj)) = sub_result {
|
if let Ok(Value::Object(obj)) = sub_result {
|
||||||
for (k, v) in obj {
|
for (k, v) in obj {
|
||||||
|
|
|
@ -153,13 +153,8 @@ where
|
||||||
arguments: &'b Arguments<'b, S>,
|
arguments: &'b Arguments<'b, S>,
|
||||||
executor: &'b Executor<'b, <Self as crate::GraphQLType<S>>::Context, S>,
|
executor: &'b Executor<'b, <Self as crate::GraphQLType<S>>::Context, S>,
|
||||||
) -> ExecutionResult<S> {
|
) -> ExecutionResult<S> {
|
||||||
crate::GraphQLTypeAsync::resolve_field_async(
|
crate::GraphQLTypeAsync::resolve_field_async(&**self, info, field_name, arguments, executor)
|
||||||
&**self,
|
.await
|
||||||
info,
|
|
||||||
field_name,
|
|
||||||
arguments,
|
|
||||||
executor
|
|
||||||
).await
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn resolve_async<'a>(
|
async fn resolve_async<'a>(
|
||||||
|
@ -168,12 +163,7 @@ where
|
||||||
selection_set: Option<&'a [Selection<'a, S>]>,
|
selection_set: Option<&'a [Selection<'a, S>]>,
|
||||||
executor: &'a Executor<'a, <Self as crate::GraphQLType<S>>::Context, S>,
|
executor: &'a Executor<'a, <Self as crate::GraphQLType<S>>::Context, S>,
|
||||||
) -> Value<S> {
|
) -> Value<S> {
|
||||||
crate::GraphQLTypeAsync::resolve_async(
|
crate::GraphQLTypeAsync::resolve_async(&**self, info, selection_set, executor).await
|
||||||
&**self,
|
|
||||||
info,
|
|
||||||
selection_set,
|
|
||||||
executor
|
|
||||||
).await
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -41,12 +41,9 @@ pub fn build_object(args: TokenStream, body: TokenStream, is_internal: bool) ->
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let name = if let Some(name) = impl_attrs.name.as_ref() {
|
||||||
let name =
|
|
||||||
if let Some(name) = impl_attrs.name.as_ref(){
|
|
||||||
name.to_string()
|
name.to_string()
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
if let Some(ident) = util::name_of_type(&*_impl.self_ty) {
|
if let Some(ident) = util::name_of_type(&*_impl.self_ty) {
|
||||||
ident.to_string()
|
ident.to_string()
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
use proc_macro::TokenStream;
|
use proc_macro::TokenStream;
|
||||||
|
|
||||||
use proc_macro_error::MacroError;
|
use proc_macro_error::MacroError;
|
||||||
use quote::{quote};
|
use quote::quote;
|
||||||
use syn::spanned::Spanned;
|
use syn::spanned::Spanned;
|
||||||
|
|
||||||
use crate::util;
|
use crate::util;
|
||||||
|
@ -67,7 +67,6 @@ pub fn impl_union(
|
||||||
attrs: TokenStream,
|
attrs: TokenStream,
|
||||||
body: TokenStream,
|
body: TokenStream,
|
||||||
) -> Result<TokenStream, MacroError> {
|
) -> Result<TokenStream, MacroError> {
|
||||||
|
|
||||||
// We are re-using the object attributes since they are almost the same.
|
// We are re-using the object attributes since they are almost the same.
|
||||||
let attrs = syn::parse::<util::ObjectAttributes>(attrs)?;
|
let attrs = syn::parse::<util::ObjectAttributes>(attrs)?;
|
||||||
|
|
||||||
|
@ -76,7 +75,8 @@ pub fn impl_union(
|
||||||
if item.items.len() != 1 {
|
if item.items.len() != 1 {
|
||||||
return Err(MacroError::new(
|
return Err(MacroError::new(
|
||||||
item.span(),
|
item.span(),
|
||||||
"Invalid impl body: expected one method with signature: fn resolve(&self) { ... }".to_string(),
|
"Invalid impl body: expected one method with signature: fn resolve(&self) { ... }"
|
||||||
|
.to_string(),
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -130,7 +130,9 @@ pub fn impl_union(
|
||||||
.scalar
|
.scalar
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.map(|s| quote!( #s ))
|
.map(|s| quote!( #s ))
|
||||||
.unwrap_or_else(|| { quote! { #juniper::DefaultScalarValue } });
|
.unwrap_or_else(|| {
|
||||||
|
quote! { #juniper::DefaultScalarValue }
|
||||||
|
});
|
||||||
|
|
||||||
let mut generics = item.generics.clone();
|
let mut generics = item.generics.clone();
|
||||||
if attrs.scalar.is_some() {
|
if attrs.scalar.is_some() {
|
||||||
|
@ -139,10 +141,12 @@ pub fn impl_union(
|
||||||
// compatible with ScalarValueRef.
|
// compatible with ScalarValueRef.
|
||||||
// This is done to prevent the user from having to specify this
|
// This is done to prevent the user from having to specify this
|
||||||
// manually.
|
// manually.
|
||||||
let where_clause = generics.where_clause.get_or_insert(syn::parse_quote!(where));
|
let where_clause = generics
|
||||||
where_clause.predicates.push(
|
.where_clause
|
||||||
syn::parse_quote!(for<'__b> &'__b #scalar: #juniper::ScalarRefValue<'__b>),
|
.get_or_insert(syn::parse_quote!(where));
|
||||||
);
|
where_clause
|
||||||
|
.predicates
|
||||||
|
.push(syn::parse_quote!(for<'__b> &'__b #scalar: #juniper::ScalarRefValue<'__b>));
|
||||||
}
|
}
|
||||||
|
|
||||||
let (impl_generics, _, where_clause) = generics.split_for_impl();
|
let (impl_generics, _, where_clause) = generics.split_for_impl();
|
||||||
|
@ -151,7 +155,10 @@ pub fn impl_union(
|
||||||
Some(value) => quote!( .description( #value ) ),
|
Some(value) => quote!( .description( #value ) ),
|
||||||
None => quote!(),
|
None => quote!(),
|
||||||
};
|
};
|
||||||
let context = attrs.context.map(|c| quote!{ #c } ).unwrap_or_else(|| quote!{ () });
|
let context = attrs
|
||||||
|
.context
|
||||||
|
.map(|c| quote! { #c })
|
||||||
|
.unwrap_or_else(|| quote! { () });
|
||||||
|
|
||||||
let output = quote! {
|
let output = quote! {
|
||||||
impl #impl_generics #juniper::GraphQLType<#scalar> for #ty #where_clause
|
impl #impl_generics #juniper::GraphQLType<#scalar> for #ty #where_clause
|
||||||
|
|
|
@ -389,4 +389,3 @@ pub fn union_internal(attrs: TokenStream, body: TokenStream) -> TokenStream {
|
||||||
};
|
};
|
||||||
output
|
output
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue