Struct async_graphql::dynamic::Scalar
source · pub struct Scalar { /* private fields */ }
Available on crate feature
dynamic-schema
only.Expand description
A GraphQL scalar type
Examples
use async_graphql::{dynamic::*, value, Value};
let my_scalar = Scalar::new("MyScalar");
let query = Object::new("Query").field(Field::new("value", TypeRef::named_nn(my_scalar.type_name()), |ctx| {
FieldFuture::new(async move { Ok(Some(Value::from("abc"))) })
}));
let schema = Schema::build(query.type_name(), None, None)
.register(my_scalar)
.register(query)
.finish()?;
assert_eq!(
schema
.execute("{ value }")
.await
.into_result()
.unwrap()
.data,
value!({ "value": "abc" })
);
Implementations§
source§impl Scalar
impl Scalar
sourcepub fn new(name: impl Into<String>) -> Self
pub fn new(name: impl Into<String>) -> Self
Create a GraphQL scalar type
Examples found in repository?
src/dynamic/schema.rs (line 159)
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
pub fn finish(mut self) -> Result<Schema, SchemaError> {
let mut registry = Registry {
types: Default::default(),
directives: Default::default(),
implements: Default::default(),
query_type: self.query_type,
mutation_type: self.mutation_type,
subscription_type: self.subscription_type,
introspection_mode: self.introspection_mode,
enable_federation: false,
federation_subscription: false,
ignore_name_conflicts: Default::default(),
enable_suggestions: self.enable_suggestions,
};
registry.add_system_types();
for ty in self.types.values() {
ty.register(&mut registry)?;
}
update_interface_possible_types(&mut self.types, &mut registry);
// create system scalars
for ty in ["Int", "Float", "Boolean", "String", "ID"] {
self.types
.insert(ty.to_string(), Type::Scalar(Scalar::new(ty)));
}
// create introspection types
if matches!(
self.introspection_mode,
IntrospectionMode::Enabled | IntrospectionMode::IntrospectionOnly
) {
registry.create_introspection_types();
}
// create entity types
if self.enable_federation || registry.has_entities() {
registry.enable_federation = true;
registry.create_federation_types();
}
let inner = SchemaInner {
env: SchemaEnv(Arc::new(SchemaEnvInner {
registry,
data: self.data,
custom_directives: Default::default(),
})),
extensions: self.extensions,
types: self.types,
recursive_depth: self.recursive_depth,
complexity: self.complexity,
depth: self.depth,
validation_mode: self.validation_mode,
entity_resolver: self.entity_resolver,
};
inner.check()?;
Ok(Schema(Arc::new(inner)))
}
sourcepub fn description(self, description: impl Into<String>) -> Self
pub fn description(self, description: impl Into<String>) -> Self
Set the description
sourcepub fn inaccessible(self) -> Self
pub fn inaccessible(self) -> Self
Indicate that an enum is not accessible from a supergraph when using Apollo Federation
Reference: https://www.apollographql.com/docs/federation/federated-types/federated-directives/#inaccessible
Arbitrary string metadata that will be propagated to the supergraph when using Apollo Federation. This attribute is repeatable
sourcepub fn validator(
self,
validator: impl Fn(&Value) -> bool + Send + Sync + 'static
) -> Self
pub fn validator(
self,
validator: impl Fn(&Value) -> bool + Send + Sync + 'static
) -> Self
Set the validator
sourcepub fn specified_by_url(self, specified_by_url: impl Into<String>) -> Self
pub fn specified_by_url(self, specified_by_url: impl Into<String>) -> Self
Set the specified by url