Skip to main content

libgraphql_core/operation/
mutation.rs

1use crate::ast;
2use crate::types::GraphQLType;
3use crate::DirectiveAnnotation;
4use crate::loc;
5use crate::operation::FragmentRegistry;
6use crate::operation::MutationBuilder;
7use crate::operation::MutationBuildError;
8use crate::operation::OperationTrait;
9use crate::operation::OperationData;
10use crate::operation::SelectionSet;
11use crate::operation::Variable;
12use crate::schema::Schema;
13use indexmap::IndexMap;
14use inherent::inherent;
15
16/// Represents a Mutation operation over a given [Schema].
17#[derive(Clone, Debug, PartialEq)]
18pub struct Mutation<'schema: 'fragreg, 'fragreg>(
19    pub(super) OperationData<'schema, 'fragreg>,
20);
21
22#[inherent]
23impl<'schema: 'fragreg, 'fragreg> OperationTrait<
24    'schema,
25    'fragreg,
26    ast::operation::Mutation,
27    MutationBuildError,
28    MutationBuilder<'schema, 'fragreg>,
29> for Mutation<'schema, 'fragreg> {
30    /// Convenience wrapper around [MutationBuilder::new()].
31    pub fn builder(
32        schema: &'schema Schema,
33        fragment_registry: &'fragreg FragmentRegistry<'schema>,
34    ) -> MutationBuilder<'schema, 'fragreg> {
35        MutationBuilder::new(schema, fragment_registry)
36    }
37
38    /// The list of [`DirectiveAnnotation`]s applied to this [`Mutation`].
39    pub fn directives(&self) -> &Vec<DirectiveAnnotation> {
40        &self.0.directives
41    }
42
43    /// The [`loc::SourceLocation`] indicating where this [`Mutation`] operation
44    /// was defined.
45    pub fn def_location(&self) -> &loc::SourceLocation {
46        &self.0.def_location
47    }
48
49    /// Access the name of this [Mutation] (if one was specified).
50    pub fn name(&self) -> Option<&str> {
51       self.0.name.as_deref()
52    }
53
54    /// Returns the root `Mutation` [`GraphQLType`] defined in the schema for
55    /// this [`Mutation`] operation.
56    pub fn root_graphql_type(&self, schema: &'schema Schema) -> &GraphQLType {
57        // TODO: Define a test that asserts that it's impossible to build a
58        //       `Mutation` operation against a schema that doesn't define a
59        //       mutation root type.
60        schema.mutation_type().unwrap()
61    }
62
63    /// Access the [SelectionSet] defined for this [Mutation].
64    pub fn selection_set(&self) -> &SelectionSet<'fragreg> {
65        &self.0.selection_set
66    }
67
68    /// Access the [Variable]s defined on this [Mutation].
69    pub fn variables(&self) -> &IndexMap<String, Variable> {
70        &self.0.variables
71    }
72}