50 lines
1.3 KiB
Rust
50 lines
1.3 KiB
Rust
use crate::parser::types::Field;
|
|
use crate::{
|
|
registry, ContextSelectionSet, GQLScalar, InputValueError, InputValueResult, OutputValueType,
|
|
Positioned, Result, ScalarType, Type, Value,
|
|
};
|
|
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.
|
|
#[GQLScalar(internal)]
|
|
impl ScalarType for String {
|
|
fn parse(value: Value) -> InputValueResult<Self> {
|
|
match value {
|
|
Value::String(s) => Ok(s),
|
|
_ => Err(InputValueError::ExpectedType(value)),
|
|
}
|
|
}
|
|
|
|
fn is_valid(value: &Value) -> bool {
|
|
match value {
|
|
Value::String(_) => true,
|
|
_ => false,
|
|
}
|
|
}
|
|
|
|
fn to_value(&self) -> Value {
|
|
Value::String(self.clone())
|
|
}
|
|
}
|
|
|
|
impl<'a> Type for &'a str {
|
|
fn type_name() -> Cow<'static, str> {
|
|
Cow::Borrowed("String")
|
|
}
|
|
|
|
fn create_type_info(registry: &mut registry::Registry) -> String {
|
|
<String as Type>::create_type_info(registry)
|
|
}
|
|
}
|
|
|
|
#[async_trait::async_trait]
|
|
impl<'a> OutputValueType for &'a str {
|
|
async fn resolve(
|
|
&self,
|
|
_: &ContextSelectionSet<'_>,
|
|
_field: &Positioned<Field>,
|
|
) -> Result<serde_json::Value> {
|
|
Ok((*self).into())
|
|
}
|
|
}
|