async-graphql/src/types/external/string.rs

50 lines
1.3 KiB
Rust
Raw Normal View History

2020-09-06 06:16:36 +00:00
use crate::parser::types::Field;
use crate::{
registry, ContextSelectionSet, InputValueError, InputValueResult, OutputValueType, Positioned,
Result, Scalar, ScalarType, Type, Value,
};
2020-03-02 00:24:49 +00:00
use std::borrow::Cow;
/// The `String` scalar type represents textual data, represented as UTF-8 character sequences. The String type is most often used by GraphQL to represent free-form human-readable text.
#[Scalar(internal)]
impl ScalarType for String {
fn parse(value: Value) -> InputValueResult<Self> {
2020-03-02 00:24:49 +00:00
match value {
2020-05-16 13:14:26 +00:00
Value::String(s) => Ok(s),
_ => Err(InputValueError::ExpectedType(value)),
2020-03-02 00:24:49 +00:00
}
}
2020-03-08 12:35:36 +00:00
fn is_valid(value: &Value) -> bool {
match value {
Value::String(_) => true,
_ => false,
}
}
fn to_value(&self) -> Value {
Value::String(self.clone())
2020-03-02 00:24:49 +00:00
}
}
2020-03-19 09:20:12 +00:00
impl<'a> Type for &'a str {
2020-03-02 00:24:49 +00:00
fn type_name() -> Cow<'static, str> {
2020-03-03 11:15:18 +00:00
Cow::Borrowed("String")
2020-03-02 00:24:49 +00:00
}
2020-03-03 03:48:00 +00:00
2020-03-03 11:15:18 +00:00
fn create_type_info(registry: &mut registry::Registry) -> String {
<String as Type>::create_type_info(registry)
2020-03-03 03:48:00 +00:00
}
2020-03-02 00:24:49 +00:00
}
#[async_trait::async_trait]
2020-03-19 09:20:12 +00:00
impl<'a> OutputValueType for &'a str {
2020-05-20 00:18:28 +00:00
async fn resolve(
&self,
_: &ContextSelectionSet<'_>,
_field: &Positioned<Field>,
) -> Result<serde_json::Value> {
Ok((*self).into())
2020-03-02 00:24:49 +00:00
}
}