Publicize resolver utils and move Scalar there

This commit is contained in:
Koxiaet 2020-09-23 19:50:35 +01:00
parent 70d2dbc39c
commit e97a7d9def
6 changed files with 57 additions and 51 deletions

View File

@ -48,48 +48,6 @@ pub trait OutputValueType: Type {
) -> Result<serde_json::Value>;
}
/// Represents a GraphQL scalar
///
/// You can implement the trait to create a custom scalar.
///
/// # Examples
///
/// ```rust
/// use async_graphql::*;
///
/// struct MyInt(i32);
///
/// #[Scalar]
/// impl ScalarType for MyInt {
/// fn parse(value: Value) -> InputValueResult<Self> {
/// if let Value::Number(n) = &value {
/// if let Some(n) = n.as_i64() {
/// return Ok(MyInt(n as i32));
/// }
/// }
/// Err(InputValueError::ExpectedType(value))
/// }
///
/// fn to_value(&self) -> Value {
/// Value::Number(self.0.into())
/// }
/// }
/// ```
pub trait ScalarType: Sized + Send {
/// Parse a scalar value, return `Some(Self)` if successful, otherwise return `None`.
fn parse(value: Value) -> InputValueResult<Self>;
/// Checks for a valid scalar value.
///
/// Implementing this function can find incorrect input values during the verification phase, which can improve performance.
fn is_valid(_value: &Value) -> bool {
true
}
/// Convert the scalar to `Value`.
fn to_value(&self) -> Value;
}
impl<T: Type + Send + Sync> Type for &T {
fn type_name() -> Cow<'static, str> {
T::type_name()

View File

@ -128,14 +128,13 @@ mod validation;
pub mod extensions;
pub mod guard;
pub mod http;
pub mod resolver_utils;
pub mod types;
pub mod validators;
#[doc(hidden)]
pub mod registry;
#[doc(hidden)]
pub mod resolver_utils;
#[doc(hidden)]
pub use async_stream;
#[doc(hidden)]
pub use async_trait;
@ -151,7 +150,7 @@ pub use serde_json;
pub use subscription::SubscriptionType;
pub use async_graphql_parser as parser;
pub use base::{InputValueType, OutputValueType, ScalarType, Type};
pub use base::{InputValueType, OutputValueType, Type};
pub use context::{
Context, ContextBase, Data, QueryEnv, QueryPathNode, QueryPathSegment, Variables,
};
@ -163,7 +162,6 @@ pub use look_ahead::Lookahead;
pub use parser::types::{ConstValue as Value, Number};
pub use registry::CacheControl;
pub use request::{BatchRequest, Request};
pub use resolver_utils::ObjectType;
pub use response::{BatchResponse, Response};
pub use schema::{Schema, SchemaBuilder, SchemaEnv};
pub use validation::ValidationMode;
@ -171,6 +169,8 @@ pub use validation::ValidationMode;
#[doc(no_inline)]
pub use parser::{Pos, Positioned};
pub use types::*;
#[doc(no_inline)]
pub use resolver_utils::{EnumType, ObjectType, ScalarType};
/// Result type
pub type Result<T> = std::result::Result<T, Error>;

View File

@ -9,9 +9,9 @@ pub struct EnumItem<T> {
pub value: T,
}
/// An enum value.
/// A GraphQL enum.
pub trait EnumType: Type + Sized + Eq + Send + Copy + Sized + 'static {
/// Get a list of the variants of the enum value.
/// Get a list of possible variants of the enum and their values.
fn items() -> &'static [EnumItem<Self>];
}

View File

@ -1,7 +1,10 @@
//! Utilities for implementing `OutputValueType::resolve`.
//! Utilities for implementing
//! [`OutputValueType::resolve`](trait.OutputValueType.html#tymethod.resolve).
mod r#enum;
mod object;
mod scalar;
pub use object::*;
pub use r#enum::*;
pub use scalar::*;

View File

@ -36,7 +36,9 @@ pub trait ObjectType: OutputValueType {
fields.add_set(ctx, self)
}
/// Query entities with params
/// Find the GraphQL entity with the given name from the parameter.
///
/// Objects should override this in case they are the query root.
async fn find_entity(&self, ctx: &Context<'_>, _params: &Value) -> Result<serde_json::Value> {
Err(QueryError::EntityNotFound.into_error(ctx.item.pos))
}
@ -105,7 +107,7 @@ pub async fn resolve_object_serial<'a, T: ObjectType + Send + Sync>(
type BoxFieldFuture<'a> =
Pin<Box<dyn Future<Output = Result<(String, serde_json::Value)>> + 'a + Send>>;
/// A set of fields on an object.
/// A set of fields on an object that are being selected.
pub struct Fields<'a>(Vec<BoxFieldFuture<'a>>);
impl<'a> Fields<'a> {

View File

@ -0,0 +1,43 @@
use crate::{Value, InputValueResult};
/// A GraphQL scalar.
///
/// You can implement the trait to create a custom scalar.
///
/// # Examples
///
/// ```rust
/// use async_graphql::*;
///
/// struct MyInt(i32);
///
/// #[Scalar]
/// impl ScalarType for MyInt {
/// fn parse(value: Value) -> InputValueResult<Self> {
/// if let Value::Number(n) = &value {
/// if let Some(n) = n.as_i64() {
/// return Ok(MyInt(n as i32));
/// }
/// }
/// Err(InputValueError::ExpectedType(value))
/// }
///
/// fn to_value(&self) -> Value {
/// Value::Number(self.0.into())
/// }
/// }
/// ```
pub trait ScalarType: Sized + Send {
/// Parse a scalar value, return `Some(Self)` if successful, otherwise return `None`.
fn parse(value: Value) -> InputValueResult<Self>;
/// Checks for a valid scalar value.
///
/// Implementing this function can find incorrect input values during the verification phase, which can improve performance.
fn is_valid(_value: &Value) -> bool {
true
}
/// Convert the scalar to `Value`.
fn to_value(&self) -> Value;
}