Remove the redundant field
parameter for OutputType::resolve_field.
This commit is contained in:
parent
70b8ecc311
commit
a4b4e69ea7
|
@ -137,7 +137,7 @@ pub fn generate(interface_args: &args::Interface, input: &DeriveInput) -> Result
|
|||
None => quote! { || #crate_name::Value::Null },
|
||||
};
|
||||
get_params.push(quote! {
|
||||
let #ident: #ty = ctx.param_value(#name, field.position, #param_default)?;
|
||||
let #ident: #ty = ctx.param_value(#name, ctx.position, #param_default)?;
|
||||
});
|
||||
|
||||
let desc = desc
|
||||
|
@ -220,16 +220,16 @@ pub fn generate(interface_args: &args::Interface, input: &DeriveInput) -> Result
|
|||
OutputType::Result(_, _) => {
|
||||
quote! {
|
||||
self.#method_name(#(#use_params),*).await.
|
||||
map_err(|err| err.into_error_with_path(field.position, ctx.path_node.as_ref().unwrap().to_json()))?
|
||||
map_err(|err| err.into_error_with_path(ctx.position, ctx.path_node.as_ref().unwrap().to_json()))?
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
resolvers.push(quote! {
|
||||
if field.name.as_str() == #name {
|
||||
if ctx.name.as_str() == #name {
|
||||
#(#get_params)*
|
||||
let ctx_obj = ctx.with_selection_set(&field.selection_set);
|
||||
return #crate_name::OutputValueType::resolve(&#resolve_obj, &ctx_obj, field.position).await;
|
||||
let ctx_obj = ctx.with_selection_set(&ctx.selection_set);
|
||||
return #crate_name::OutputValueType::resolve(&#resolve_obj, &ctx_obj, ctx.position).await;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
@ -289,13 +289,12 @@ pub fn generate(interface_args: &args::Interface, input: &DeriveInput) -> Result
|
|||
|
||||
#[#crate_name::async_trait::async_trait]
|
||||
impl #generics #crate_name::ObjectType for #ident #generics {
|
||||
async fn resolve_field(&self, ctx: &#crate_name::Context<'_>, field: &#crate_name::graphql_parser::query::Field) -> #crate_name::Result<#crate_name::serde_json::Value> {
|
||||
async fn resolve_field(&self, ctx: &#crate_name::Context<'_>) -> #crate_name::Result<#crate_name::serde_json::Value> {
|
||||
#(#resolvers)*
|
||||
|
||||
Err(#crate_name::QueryError::FieldNotFound {
|
||||
field_name: field.name.clone(),
|
||||
field_name: ctx.name.clone(),
|
||||
object: #gql_typename.to_string(),
|
||||
}.into_error(field.position))
|
||||
}.into_error(ctx.position))
|
||||
}
|
||||
|
||||
fn collect_inline_fields<'a>(
|
||||
|
|
|
@ -187,7 +187,7 @@ pub fn generate(object_args: &args::Object, item_impl: &mut ItemImpl) -> Result<
|
|||
};
|
||||
|
||||
get_params.push(quote! {
|
||||
let #ident: #ty = ctx.param_value(#name, field.position, #default)?;
|
||||
let #ident: #ty = ctx.param_value(#name, ctx.position, #default)?;
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -226,17 +226,17 @@ pub fn generate(object_args: &args::Object, item_impl: &mut ItemImpl) -> Result<
|
|||
quote! {
|
||||
{
|
||||
let res:#crate_name::FieldResult<_> = self.#field_ident(#ctx_param #(#use_params),*).await;
|
||||
res.map_err(|err| err.into_error_with_path(field.position, ctx.path_node.as_ref().unwrap().to_json()))?
|
||||
res.map_err(|err| err.into_error_with_path(ctx.position, ctx.path_node.as_ref().unwrap().to_json()))?
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
resolvers.push(quote! {
|
||||
if field.name.as_str() == #field_name {
|
||||
if ctx.name.as_str() == #field_name {
|
||||
#(#get_params)*
|
||||
let ctx_obj = ctx.with_selection_set(&field.selection_set);
|
||||
return #crate_name::OutputValueType::resolve(&#resolve_obj, &ctx_obj, field.position).await;
|
||||
let ctx_obj = ctx.with_selection_set(&ctx.selection_set);
|
||||
return #crate_name::OutputValueType::resolve(&#resolve_obj, &ctx_obj, ctx.position).await;
|
||||
}
|
||||
});
|
||||
|
||||
|
@ -419,12 +419,12 @@ pub fn generate(object_args: &args::Object, item_impl: &mut ItemImpl) -> Result<
|
|||
|
||||
#[#crate_name::async_trait::async_trait]
|
||||
impl#generics #crate_name::ObjectType for #self_ty {
|
||||
async fn resolve_field(&self, ctx: &#crate_name::Context<'_>, field: &#crate_name::graphql_parser::query::Field) -> #crate_name::Result<#crate_name::serde_json::Value> {
|
||||
async fn resolve_field(&self, ctx: &#crate_name::Context<'_>) -> #crate_name::Result<#crate_name::serde_json::Value> {
|
||||
#(#resolvers)*
|
||||
Err(#crate_name::QueryError::FieldNotFound {
|
||||
field_name: field.name.clone(),
|
||||
field_name: ctx.name.clone(),
|
||||
object: #gql_typename.to_string(),
|
||||
}.into_error(field.position))
|
||||
}.into_error(ctx.position))
|
||||
}
|
||||
|
||||
async fn find_entity(&self, ctx: &#crate_name::Context<'_>, pos: #crate_name::Pos, params: &#crate_name::Value) -> #crate_name::Result<#crate_name::serde_json::Value> {
|
||||
|
|
|
@ -108,9 +108,9 @@ pub fn generate(object_args: &args::Object, input: &mut DeriveInput) -> Result<T
|
|||
}
|
||||
|
||||
resolvers.push(quote! {
|
||||
if field.name.as_str() == #field_name {
|
||||
let ctx_obj = ctx.with_selection_set(&field.selection_set);
|
||||
return #crate_name::OutputValueType::resolve(&self.#ident, &ctx_obj, field.position).await;
|
||||
if ctx.name.as_str() == #field_name {
|
||||
let ctx_obj = ctx.with_selection_set(&ctx.selection_set);
|
||||
return #crate_name::OutputValueType::resolve(&self.#ident, &ctx_obj, ctx.position).await;
|
||||
}
|
||||
});
|
||||
|
||||
|
@ -167,13 +167,12 @@ pub fn generate(object_args: &args::Object, input: &mut DeriveInput) -> Result<T
|
|||
|
||||
#[#crate_name::async_trait::async_trait]
|
||||
impl #generics #crate_name::ObjectType for #ident #generics {
|
||||
async fn resolve_field(&self, ctx: &#crate_name::Context<'_>, field: &#crate_name::graphql_parser::query::Field) -> #crate_name::Result<#crate_name::serde_json::Value> {
|
||||
async fn resolve_field(&self, ctx: &#crate_name::Context<'_>) -> #crate_name::Result<#crate_name::serde_json::Value> {
|
||||
#(#resolvers)*
|
||||
|
||||
Err(#crate_name::QueryError::FieldNotFound {
|
||||
field_name: field.name.clone(),
|
||||
field_name: ctx.name.clone(),
|
||||
object: #gql_typename.to_string(),
|
||||
}.into_error(field.position))
|
||||
}.into_error(ctx.position))
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -104,11 +104,11 @@ pub fn generate(interface_args: &args::Interface, input: &DeriveInput) -> Result
|
|||
|
||||
#[#crate_name::async_trait::async_trait]
|
||||
impl #generics #crate_name::ObjectType for #ident #generics {
|
||||
async fn resolve_field(&self, ctx: &#crate_name::Context<'_>, field: &#crate_name::graphql_parser::query::Field) -> #crate_name::Result<#crate_name::serde_json::Value> {
|
||||
async fn resolve_field(&self, ctx: &#crate_name::Context<'_>) -> #crate_name::Result<#crate_name::serde_json::Value> {
|
||||
Err(#crate_name::QueryError::FieldNotFound {
|
||||
field_name: field.name.clone(),
|
||||
field_name: ctx.name.clone(),
|
||||
object: #gql_typename.to_string(),
|
||||
}.into_error(field.position))
|
||||
}.into_error(ctx.position))
|
||||
}
|
||||
|
||||
fn collect_inline_fields<'a>(
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
use crate::registry::Registry;
|
||||
use crate::{registry, Context, ContextSelectionSet, QueryError, Result, ID};
|
||||
use graphql_parser::query::{Field, Value};
|
||||
use graphql_parser::query::Value;
|
||||
use graphql_parser::Pos;
|
||||
use std::borrow::Cow;
|
||||
use std::future::Future;
|
||||
|
@ -78,7 +78,7 @@ pub trait ObjectType: OutputValueType {
|
|||
}
|
||||
|
||||
/// Resolves a field value and outputs it as a json value `serde_json::Value`.
|
||||
async fn resolve_field(&self, ctx: &Context<'_>, field: &Field) -> Result<serde_json::Value>;
|
||||
async fn resolve_field(&self, ctx: &Context<'_>) -> Result<serde_json::Value>;
|
||||
|
||||
/// Collect the fields with the `name` inline object
|
||||
fn collect_inline_fields<'a>(
|
||||
|
|
|
@ -83,7 +83,7 @@ fn do_resolve<'a, T: ObjectType + Send + Sync>(
|
|||
.for_each(|e| e.resolve_field_start(&resolve_info));
|
||||
}
|
||||
|
||||
let value = root.resolve_field(&ctx_field, field).await?;
|
||||
let value = root.resolve_field(&ctx_field).await?;
|
||||
values.insert(field_name, value);
|
||||
|
||||
if !ctx_field.extensions.is_empty() {
|
||||
|
|
|
@ -93,7 +93,7 @@ pub fn collect_fields<'a, T: ObjectType + Send + Sync>(
|
|||
}
|
||||
|
||||
let res = root
|
||||
.resolve_field(&ctx_field, field)
|
||||
.resolve_field(&ctx_field)
|
||||
.map_ok(move |value| (field_name, value))
|
||||
.await?;
|
||||
|
||||
|
|
|
@ -4,7 +4,6 @@ use crate::{
|
|||
do_resolve, registry, Context, ContextSelectionSet, EmptyEdgeFields, Error, ObjectType,
|
||||
OutputValueType, Pos, QueryError, Result, Type,
|
||||
};
|
||||
use graphql_parser::query::Field;
|
||||
use inflector::Inflector;
|
||||
use itertools::Itertools;
|
||||
use std::borrow::Cow;
|
||||
|
@ -171,29 +170,28 @@ impl<T: OutputValueType + Send + Sync, E: ObjectType + Sync + Send> Type for Con
|
|||
impl<T: OutputValueType + Send + Sync, E: ObjectType + Sync + Send> ObjectType
|
||||
for Connection<T, E>
|
||||
{
|
||||
async fn resolve_field(&self, ctx: &Context<'_>, field: &Field) -> Result<serde_json::Value> {
|
||||
if field.name.as_str() == "pageInfo" {
|
||||
let ctx_obj = ctx.with_selection_set(&field.selection_set);
|
||||
return OutputValueType::resolve(self.page_info().await, &ctx_obj, field.position)
|
||||
async fn resolve_field(&self, ctx: &Context<'_>) -> Result<serde_json::Value> {
|
||||
if ctx.name.as_str() == "pageInfo" {
|
||||
let ctx_obj = ctx.with_selection_set(&ctx.selection_set);
|
||||
return OutputValueType::resolve(self.page_info().await, &ctx_obj, ctx.position).await;
|
||||
} else if ctx.name.as_str() == "edges" {
|
||||
let ctx_obj = ctx.with_selection_set(&ctx.selection_set);
|
||||
return OutputValueType::resolve(&self.edges().await, &ctx_obj, ctx.position).await;
|
||||
} else if ctx.name.as_str() == "totalCount" {
|
||||
let ctx_obj = ctx.with_selection_set(&ctx.selection_set);
|
||||
return OutputValueType::resolve(&self.total_count().await, &ctx_obj, ctx.position)
|
||||
.await;
|
||||
} else if field.name.as_str() == "edges" {
|
||||
let ctx_obj = ctx.with_selection_set(&field.selection_set);
|
||||
return OutputValueType::resolve(&self.edges().await, &ctx_obj, field.position).await;
|
||||
} else if field.name.as_str() == "totalCount" {
|
||||
let ctx_obj = ctx.with_selection_set(&field.selection_set);
|
||||
return OutputValueType::resolve(&self.total_count().await, &ctx_obj, field.position)
|
||||
.await;
|
||||
} else if field.name.as_str() == T::type_name().to_plural().to_camel_case() {
|
||||
let ctx_obj = ctx.with_selection_set(&field.selection_set);
|
||||
} else if ctx.name.as_str() == T::type_name().to_plural().to_camel_case() {
|
||||
let ctx_obj = ctx.with_selection_set(&ctx.selection_set);
|
||||
let items = self.nodes.iter().map(|(_, _, item)| item).collect_vec();
|
||||
return OutputValueType::resolve(&items, &ctx_obj, field.position).await;
|
||||
return OutputValueType::resolve(&items, &ctx_obj, ctx.position).await;
|
||||
}
|
||||
|
||||
Err(Error::Query {
|
||||
pos: field.position,
|
||||
pos: ctx.position,
|
||||
path: None,
|
||||
err: QueryError::FieldNotFound {
|
||||
field_name: field.name.clone(),
|
||||
field_name: ctx.name.clone(),
|
||||
object: Connection::<T, E>::type_name().to_string(),
|
||||
},
|
||||
})
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
use crate::{
|
||||
do_resolve, registry, Context, ContextSelectionSet, ObjectType, OutputValueType, Result, Type,
|
||||
};
|
||||
use graphql_parser::query::Field;
|
||||
use graphql_parser::Pos;
|
||||
use std::borrow::Cow;
|
||||
use std::collections::HashMap;
|
||||
|
@ -104,15 +103,15 @@ where
|
|||
T: OutputValueType + Send + Sync + 'a,
|
||||
E: ObjectType + Sync + Send + 'a,
|
||||
{
|
||||
async fn resolve_field(&self, ctx: &Context<'_>, field: &Field) -> Result<serde_json::Value> {
|
||||
if field.name.as_str() == "node" {
|
||||
let ctx_obj = ctx.with_selection_set(&field.selection_set);
|
||||
return OutputValueType::resolve(self.node().await, &ctx_obj, field.position).await;
|
||||
} else if field.name.as_str() == "cursor" {
|
||||
async fn resolve_field(&self, ctx: &Context<'_>) -> Result<serde_json::Value> {
|
||||
if ctx.name.as_str() == "node" {
|
||||
let ctx_obj = ctx.with_selection_set(&ctx.selection_set);
|
||||
return OutputValueType::resolve(self.node().await, &ctx_obj, ctx.position).await;
|
||||
} else if ctx.name.as_str() == "cursor" {
|
||||
return Ok(self.cursor().await.into());
|
||||
}
|
||||
|
||||
self.extra_type.resolve_field(ctx, field).await
|
||||
self.extra_type.resolve_field(ctx).await
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -2,7 +2,6 @@ use crate::{
|
|||
registry, Context, ContextSelectionSet, Error, ObjectType, OutputValueType, QueryError, Result,
|
||||
Type,
|
||||
};
|
||||
use graphql_parser::query::Field;
|
||||
use graphql_parser::Pos;
|
||||
use std::borrow::Cow;
|
||||
|
||||
|
@ -49,7 +48,7 @@ impl ObjectType for EmptyMutation {
|
|||
true
|
||||
}
|
||||
|
||||
async fn resolve_field(&self, _ctx: &Context<'_>, _name: &Field) -> Result<serde_json::Value> {
|
||||
async fn resolve_field(&self, _ctx: &Context<'_>) -> Result<serde_json::Value> {
|
||||
unreachable!()
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,7 +5,6 @@ use crate::{
|
|||
QueryError, Result, Type, Value,
|
||||
};
|
||||
use async_graphql_derive::SimpleObject;
|
||||
use graphql_parser::query::Field;
|
||||
use graphql_parser::Pos;
|
||||
use std::borrow::Cow;
|
||||
use std::collections::HashMap;
|
||||
|
@ -82,61 +81,61 @@ impl<T: Type> Type for QueryRoot<T> {
|
|||
|
||||
#[async_trait::async_trait]
|
||||
impl<T: ObjectType + Send + Sync> ObjectType for QueryRoot<T> {
|
||||
async fn resolve_field(&self, ctx: &Context<'_>, field: &Field) -> Result<serde_json::Value> {
|
||||
if field.name.as_str() == "__schema" {
|
||||
async fn resolve_field(&self, ctx: &Context<'_>) -> Result<serde_json::Value> {
|
||||
if ctx.name.as_str() == "__schema" {
|
||||
if self.disable_introspection {
|
||||
return Err(Error::Query {
|
||||
pos: field.position,
|
||||
pos: ctx.position,
|
||||
path: Some(ctx.path_node.as_ref().unwrap().to_json()),
|
||||
err: QueryError::FieldNotFound {
|
||||
field_name: field.name.clone(),
|
||||
field_name: ctx.name.clone(),
|
||||
object: Self::type_name().to_string(),
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
let ctx_obj = ctx.with_selection_set(&field.selection_set);
|
||||
let ctx_obj = ctx.with_selection_set(&ctx.selection_set);
|
||||
return OutputValueType::resolve(
|
||||
&__Schema {
|
||||
registry: &ctx.registry,
|
||||
},
|
||||
&ctx_obj,
|
||||
field.position,
|
||||
ctx.position,
|
||||
)
|
||||
.await;
|
||||
} else if field.name.as_str() == "__type" {
|
||||
let type_name: String = ctx.param_value("name", field.position, || Value::Null)?;
|
||||
let ctx_obj = ctx.with_selection_set(&field.selection_set);
|
||||
} else if ctx.name.as_str() == "__type" {
|
||||
let type_name: String = ctx.param_value("name", ctx.position, || Value::Null)?;
|
||||
let ctx_obj = ctx.with_selection_set(&ctx.selection_set);
|
||||
return OutputValueType::resolve(
|
||||
&ctx.registry
|
||||
.types
|
||||
.get(&type_name)
|
||||
.map(|ty| __Type::new_simple(ctx.registry, ty)),
|
||||
&ctx_obj,
|
||||
field.position,
|
||||
ctx.position,
|
||||
)
|
||||
.await;
|
||||
} else if field.name.as_str() == "_entities" {
|
||||
} else if ctx.name.as_str() == "_entities" {
|
||||
let representations: Vec<Any> =
|
||||
ctx.param_value("representations", field.position, || Value::Null)?;
|
||||
ctx.param_value("representations", ctx.position, || Value::Null)?;
|
||||
let mut res = Vec::new();
|
||||
for item in representations {
|
||||
res.push(self.inner.find_entity(ctx, field.position, &item.0).await?);
|
||||
res.push(self.inner.find_entity(ctx, ctx.position, &item.0).await?);
|
||||
}
|
||||
return Ok(res.into());
|
||||
} else if field.name.as_str() == "_service" {
|
||||
let ctx_obj = ctx.with_selection_set(&field.selection_set);
|
||||
} else if ctx.name.as_str() == "_service" {
|
||||
let ctx_obj = ctx.with_selection_set(&ctx.selection_set);
|
||||
return OutputValueType::resolve(
|
||||
&Service {
|
||||
sdl: Some(ctx.registry.create_federation_sdl()),
|
||||
},
|
||||
&ctx_obj,
|
||||
field.position,
|
||||
ctx.position,
|
||||
)
|
||||
.await;
|
||||
}
|
||||
|
||||
self.inner.resolve_field(ctx, field).await
|
||||
self.inner.resolve_field(ctx).await
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user