Change the first parameter of OutputValueType::resolve to be &self

This commit is contained in:
sunli 2020-05-05 21:06:48 +08:00
parent ba0f68db0f
commit 35531b65f8
15 changed files with 42 additions and 92 deletions

View File

@ -162,8 +162,8 @@ pub fn generate(enum_args: &args::Enum, input: &DeriveInput) -> Result<TokenStre
#[#crate_name::async_trait::async_trait]
impl #crate_name::OutputValueType for #ident {
async fn resolve(value: &Self, _: &#crate_name::ContextSelectionSet<'_>, _pos: #crate_name::Pos) -> #crate_name::Result<#crate_name::serde_json::Value> {
#crate_name::EnumType::resolve_enum(value)
async fn resolve(&self, _: &#crate_name::ContextSelectionSet<'_>, _pos: #crate_name::Pos) -> #crate_name::Result<#crate_name::serde_json::Value> {
#crate_name::EnumType::resolve_enum(self)
}
}
};

View File

@ -299,8 +299,8 @@ pub fn generate(interface_args: &args::Interface, input: &DeriveInput) -> Result
#[#crate_name::async_trait::async_trait]
impl #generics #crate_name::OutputValueType for #ident #generics {
async fn resolve(value: &Self, ctx: &#crate_name::ContextSelectionSet<'_>, pos: #crate_name::Pos) -> #crate_name::Result<#crate_name::serde_json::Value> {
#crate_name::do_resolve(ctx, value).await
async fn resolve(&self, ctx: &#crate_name::ContextSelectionSet<'_>, pos: #crate_name::Pos) -> #crate_name::Result<#crate_name::serde_json::Value> {
#crate_name::do_resolve(ctx, self).await
}
}
};

View File

@ -172,11 +172,11 @@ pub fn Scalar(args: TokenStream, input: TokenStream) -> TokenStream {
#[#crate_name::async_trait::async_trait]
impl #generic #crate_name::OutputValueType for #self_ty #where_clause {
async fn resolve(
value: &Self,
&self,
_: &#crate_name::ContextSelectionSet<'_>,
_pos: #crate_name::Pos,
) -> #crate_name::Result<#crate_name::serde_json::Value> {
value.to_json()
self.to_json()
}
}
};

View File

@ -384,10 +384,11 @@ pub fn generate(object_args: &args::Object, item_impl: &mut ItemImpl) -> Result<
resolvers.push(quote! {
if ctx.name.as_str() == #field_name {
use #crate_name::OutputValueType;
#guard
#(#get_params)*
let ctx_obj = ctx.with_selection_set(&ctx.selection_set);
return #crate_name::OutputValueType::resolve(&#resolve_obj, &ctx_obj, ctx.position).await;
return OutputValueType::resolve(&#resolve_obj, &ctx_obj, ctx.position).await;
}
});
@ -478,8 +479,8 @@ pub fn generate(object_args: &args::Object, item_impl: &mut ItemImpl) -> Result<
#[#crate_name::async_trait::async_trait]
impl #generics #crate_name::OutputValueType for #self_ty #where_clause {
async fn resolve(value: &Self, ctx: &#crate_name::ContextSelectionSet<'_>, pos: #crate_name::Pos) -> #crate_name::Result<#crate_name::serde_json::Value> {
#crate_name::do_resolve(ctx, value).await
async fn resolve(&self, ctx: &#crate_name::ContextSelectionSet<'_>, pos: #crate_name::Pos) -> #crate_name::Result<#crate_name::serde_json::Value> {
#crate_name::do_resolve(ctx, self).await
}
}
};

View File

@ -184,8 +184,8 @@ pub fn generate(object_args: &args::Object, input: &mut DeriveInput) -> Result<T
#[#crate_name::async_trait::async_trait]
impl #generics #crate_name::OutputValueType for #ident #generics #where_clause {
async fn resolve(value: &Self, ctx: &#crate_name::ContextSelectionSet<'_>, _pos: #crate_name::Pos) -> #crate_name::Result<#crate_name::serde_json::Value> {
#crate_name::do_resolve(ctx, value).await
async fn resolve(&self, ctx: &#crate_name::ContextSelectionSet<'_>, _pos: #crate_name::Pos) -> #crate_name::Result<#crate_name::serde_json::Value> {
#crate_name::do_resolve(ctx, self).await
}
}
};

View File

@ -125,8 +125,8 @@ pub fn generate(interface_args: &args::Interface, input: &DeriveInput) -> Result
#[#crate_name::async_trait::async_trait]
impl #generics #crate_name::OutputValueType for #ident #generics {
async fn resolve(value: &Self, ctx: &#crate_name::ContextSelectionSet<'_>, pos: #crate_name::Pos) -> #crate_name::Result<#crate_name::serde_json::Value> {
#crate_name::do_resolve(ctx, value).await
async fn resolve(&self, ctx: &#crate_name::ContextSelectionSet<'_>, pos: #crate_name::Pos) -> #crate_name::Result<#crate_name::serde_json::Value> {
#crate_name::do_resolve(ctx, self).await
}
}
};

View File

@ -57,11 +57,7 @@ pub trait InputValueType: Type + Sized {
#[async_trait::async_trait]
pub trait OutputValueType: Type {
/// Resolve an output value to `serde_json::Value`.
async fn resolve(
value: &Self,
ctx: &ContextSelectionSet<'_>,
pos: Pos,
) -> Result<serde_json::Value>;
async fn resolve(&self, ctx: &ContextSelectionSet<'_>, pos: Pos) -> Result<serde_json::Value>;
}
#[allow(missing_docs)]
@ -185,12 +181,8 @@ impl<T: Type + Send + Sync> Type for &T {
#[async_trait::async_trait]
impl<T: OutputValueType + Send + Sync> OutputValueType for &T {
#[allow(clippy::trivially_copy_pass_by_ref)]
async fn resolve(
value: &Self,
ctx: &ContextSelectionSet<'_>,
pos: Pos,
) -> Result<serde_json::Value> {
T::resolve(*value, ctx, pos).await
async fn resolve(&self, ctx: &ContextSelectionSet<'_>, pos: Pos) -> Result<serde_json::Value> {
T::resolve(*self, ctx, pos).await
}
}
@ -208,12 +200,8 @@ impl<T: Type + Send + Sync> Type for Box<T> {
impl<T: OutputValueType + Send + Sync> OutputValueType for Box<T> {
#[allow(clippy::trivially_copy_pass_by_ref)]
#[allow(clippy::borrowed_box)]
async fn resolve(
value: &Self,
ctx: &ContextSelectionSet<'_>,
pos: Pos,
) -> Result<serde_json::Value> {
T::resolve(&*value, ctx, pos).await
async fn resolve(&self, ctx: &ContextSelectionSet<'_>, pos: Pos) -> Result<serde_json::Value> {
T::resolve(&*self, ctx, pos).await
}
}
@ -230,12 +218,8 @@ impl<T: Type + Send + Sync> Type for Arc<T> {
#[async_trait::async_trait]
impl<T: OutputValueType + Send + Sync> OutputValueType for Arc<T> {
#[allow(clippy::trivially_copy_pass_by_ref)]
async fn resolve(
value: &Self,
ctx: &ContextSelectionSet<'_>,
pos: Pos,
) -> Result<serde_json::Value> {
T::resolve(&*value, ctx, pos).await
async fn resolve(&self, ctx: &ContextSelectionSet<'_>, pos: Pos) -> Result<serde_json::Value> {
T::resolve(&*self, ctx, pos).await
}
}
@ -256,11 +240,11 @@ impl<T: Type> Type for FieldResult<T> {
#[async_trait::async_trait]
impl<T: OutputValueType + Sync> OutputValueType for FieldResult<T> {
async fn resolve(
value: &Self,
&self,
ctx: &ContextSelectionSet<'_>,
pos: Pos,
) -> crate::Result<serde_json::Value> where {
match value.as_ref() {
match self {
Ok(value) => Ok(OutputValueType::resolve(value, ctx, pos).await?),
Err(err) => Err(err.clone().into_error_with_path(
pos,

View File

@ -52,11 +52,7 @@ impl<'a> Type for &'a str {
#[async_trait::async_trait]
impl<'a> OutputValueType for &'a str {
async fn resolve(
value: &Self,
_: &ContextSelectionSet<'_>,
_pos: Pos,
) -> Result<serde_json::Value> {
Ok((*value).into())
async fn resolve(&self, _: &ContextSelectionSet<'_>, _pos: Pos) -> Result<serde_json::Value> {
Ok((*self).into())
}
}

View File

@ -203,11 +203,7 @@ impl<T: OutputValueType + Send + Sync, E: ObjectType + Sync + Send> ObjectType
impl<T: OutputValueType + Send + Sync, E: ObjectType + Sync + Send> OutputValueType
for Connection<T, E>
{
async fn resolve(
value: &Self,
ctx: &ContextSelectionSet<'_>,
_pos: Pos,
) -> Result<serde_json::Value> {
do_resolve(ctx, value).await
async fn resolve(&self, ctx: &ContextSelectionSet<'_>, _pos: Pos) -> Result<serde_json::Value> {
do_resolve(ctx, self).await
}
}

View File

@ -120,11 +120,7 @@ where
T: OutputValueType + Send + Sync + 'a,
E: ObjectType + Sync + Send + 'a,
{
async fn resolve(
value: &Self,
ctx: &ContextSelectionSet<'_>,
_pos: Pos,
) -> Result<serde_json::Value> {
do_resolve(ctx, value).await
async fn resolve(&self, ctx: &ContextSelectionSet<'_>, _pos: Pos) -> Result<serde_json::Value> {
do_resolve(ctx, self).await
}
}

View File

@ -55,11 +55,7 @@ impl ObjectType for EmptyMutation {
#[async_trait::async_trait]
impl OutputValueType for EmptyMutation {
async fn resolve(
_value: &Self,
_ctx: &ContextSelectionSet<'_>,
pos: Pos,
) -> Result<serde_json::Value> {
async fn resolve(&self, _ctx: &ContextSelectionSet<'_>, pos: Pos) -> Result<serde_json::Value> {
Err(Error::Query {
pos,
path: None,

View File

@ -58,11 +58,7 @@ impl SubscriptionType for EmptySubscription {
#[async_trait::async_trait]
impl OutputValueType for EmptySubscription {
async fn resolve(
_value: &Self,
_ctx: &ContextSelectionSet<'_>,
pos: Pos,
) -> Result<serde_json::Value> {
async fn resolve(&self, _ctx: &ContextSelectionSet<'_>, pos: Pos) -> Result<serde_json::Value> {
Err(Error::Query {
pos,
path: None,

View File

@ -35,13 +35,9 @@ impl<T: InputValueType> InputValueType for Vec<T> {
#[allow(clippy::ptr_arg)]
#[async_trait::async_trait]
impl<T: OutputValueType + Send + Sync> OutputValueType for Vec<T> {
async fn resolve(
value: &Self,
ctx: &ContextSelectionSet<'_>,
pos: Pos,
) -> Result<serde_json::Value> {
let mut futures = Vec::with_capacity(value.len());
for (idx, item) in value.iter().enumerate() {
async fn resolve(&self, ctx: &ContextSelectionSet<'_>, pos: Pos) -> Result<serde_json::Value> {
let mut futures = Vec::with_capacity(self.len());
for (idx, item) in self.iter().enumerate() {
let ctx_idx = ctx.with_index(idx);
futures.push(async move { OutputValueType::resolve(item, &ctx_idx, pos).await });
}
@ -61,13 +57,9 @@ impl<T: Type> Type for &[T] {
#[async_trait::async_trait]
impl<T: OutputValueType + Send + Sync> OutputValueType for &[T] {
async fn resolve(
value: &Self,
ctx: &ContextSelectionSet<'_>,
pos: Pos,
) -> Result<serde_json::Value> {
let mut futures = Vec::with_capacity(value.len());
for (idx, item) in (*value).iter().enumerate() {
async fn resolve(&self, ctx: &ContextSelectionSet<'_>, pos: Pos) -> Result<serde_json::Value> {
let mut futures = Vec::with_capacity(self.len());
for (idx, item) in (*self).iter().enumerate() {
let ctx_idx = ctx.with_index(idx);
futures.push(async move { OutputValueType::resolve(item, &ctx_idx, pos).await });
}

View File

@ -29,12 +29,9 @@ impl<T: InputValueType> InputValueType for Option<T> {
#[async_trait::async_trait]
impl<T: OutputValueType + Sync> OutputValueType for Option<T> {
async fn resolve(
value: &Self,
ctx: &ContextSelectionSet<'_>,
pos: Pos,
) -> Result<serde_json::Value> where {
if let Some(inner) = value {
async fn resolve(&self, ctx: &ContextSelectionSet<'_>, pos: Pos) -> Result<serde_json::Value> where
{
if let Some(inner) = self {
OutputValueType::resolve(inner, ctx, pos).await
} else {
Ok(serde_json::Value::Null)

View File

@ -140,11 +140,7 @@ impl<T: ObjectType + Send + Sync> ObjectType for QueryRoot<T> {
#[async_trait::async_trait]
impl<T: ObjectType + Send + Sync> OutputValueType for QueryRoot<T> {
async fn resolve(
value: &Self,
ctx: &ContextSelectionSet<'_>,
_pos: Pos,
) -> Result<serde_json::Value> {
do_resolve(ctx, value).await
async fn resolve(&self, ctx: &ContextSelectionSet<'_>, _pos: Pos) -> Result<serde_json::Value> {
do_resolve(ctx, self).await
}
}