async_graphql/types/
empty_mutation.rs

1use std::borrow::Cow;
2
3use crate::{
4    Context, ContextSelectionSet, ObjectType, OutputType, Positioned, ServerError, ServerResult,
5    Value, parser::types::Field, registry, registry::MetaTypeId, resolver_utils::ContainerType,
6};
7
8/// Empty mutation
9///
10/// Only the parameters used to construct the Schema, representing an
11/// unconfigured mutation.
12///
13/// # Examples
14///
15/// ```rust
16/// use async_graphql::*;
17///
18/// struct Query;
19///
20/// #[Object]
21/// impl Query {
22///     async fn value(&self) -> i32 {
23///         // A GraphQL Object type must define one or more fields.
24///         100
25///     }
26/// }
27///
28/// let schema = Schema::new(Query, EmptyMutation, EmptySubscription);
29/// ```
30#[derive(Default, Copy, Clone)]
31pub struct EmptyMutation;
32
33#[cfg_attr(feature = "boxed-trait", async_trait::async_trait)]
34impl ContainerType for EmptyMutation {
35    fn is_empty() -> bool {
36        true
37    }
38
39    async fn resolve_field(&self, _ctx: &Context<'_>) -> ServerResult<Option<Value>> {
40        Ok(None)
41    }
42}
43
44#[cfg_attr(feature = "boxed-trait", async_trait::async_trait)]
45impl OutputType for EmptyMutation {
46    fn type_name() -> Cow<'static, str> {
47        Cow::Borrowed("EmptyMutation")
48    }
49
50    fn create_type_info(registry: &mut registry::Registry) -> String {
51        registry.create_output_type::<Self, _>(MetaTypeId::Object, |_| registry::MetaType::Object {
52            name: "EmptyMutation".to_string(),
53            description: None,
54            fields: Default::default(),
55            cache_control: Default::default(),
56            extends: false,
57            shareable: false,
58            resolvable: true,
59            keys: None,
60            visible: None,
61            inaccessible: false,
62            interface_object: false,
63            tags: Default::default(),
64            is_subscription: false,
65            rust_typename: Some(std::any::type_name::<Self>()),
66            directive_invocations: Default::default(),
67            requires_scopes: Default::default(),
68        })
69    }
70
71    async fn resolve(
72        &self,
73        _ctx: &ContextSelectionSet<'_>,
74        _field: &Positioned<Field>,
75    ) -> ServerResult<Value> {
76        Err(ServerError::new(
77            "Schema is not configured for mutations.",
78            None,
79        ))
80    }
81}
82
83impl ObjectType for EmptyMutation {}