#[graphql_scalar]
Expand description
#[graphql_scalar]
macro.is interchangeable with the #[derive(
GraphQLScalar
)]
macro,
and is used for deriving a GraphQL scalar implementation.
/// Doc comments are used for the GraphQL type description.
#[graphql_scalar]
#[graphql(
// Custom GraphQL name.
name = "MyUserId",
// Description can also specified in the attribute.
// This will the doc comment, if one exists.
description = "...",
// Optional specification URL.
specified_by_url = "https://tools.ietf.org/html/rfc4122",
// Explicit generic scalar.
scalar = S: juniper::ScalarValue,
transparent,
)]
struct UserId(String);
ยงForeign types
Additionally, #[graphql_scalar]
can be used directly on foreign types via type alias, without
using the newtype pattern.
NOTE: To satisfy orphan rules you should provide local
ScalarValue
implementation.
use juniper::{graphql_scalar, ScalarValue};
#[graphql_scalar]
#[graphql(
with = date_scalar,
to_output_with = ScalarValue::from_displayable, // use `Display` representation
parse_token(String),
scalar = CustomScalarValue,
)]
// ^^^^^^^^^^^^^^^^^ local `ScalarValue` implementation
type Date = date::Date;
// ^^^^^^^^^^ type from another crate
mod date_scalar {
use super::Date;
pub(super) fn from_input(s: &str) -> Result<Date, Box<str>> {
s.parse().map_err(|e| format!("Failed to parse `Date`: {e}").into())
}
}