libgraphql_core/
directive_annotation.rs

1use crate::loc;
2use crate::schema::Schema;
3use crate::Value;
4use crate::types::Directive;
5use crate::types::NamedDirectiveRef;
6use indexmap::IndexMap;
7
8/// Represents a
9/// [directive annotation](https://spec.graphql.org/October2021/#sec-Language.Directives)
10/// placed somewhere within a [`GraphQLType`](crate::types::GraphQLType),
11/// [`Mutation`](crate::operation::Mutation),
12/// [`Query`](crate::operation::Query), or
13/// [`Subscription`](crate::operation::Subscription).
14///
15/// A [`DirectiveAnnotation`] can be thought of as a "pointer" to some
16/// [`Directive`] paired with a set of named arguments ([`Value`]s).
17#[derive(Clone, Debug, PartialEq, serde::Deserialize, serde::Serialize)]
18pub struct DirectiveAnnotation {
19    pub(crate) args: IndexMap<String, Value>,
20    pub(crate) directive_ref: NamedDirectiveRef,
21}
22impl DirectiveAnnotation {
23    /// A map from ParameterName -> [`Value`] for all arguments passed to this
24    /// [`DirectiveAnnotation`].
25    ///
26    /// This returns an [`IndexMap`] to guarantee that map entries retain the same
27    /// ordering as the order of arguments passed to this directive annotation.
28    pub fn args(&self) -> &IndexMap<String, Value> {
29        &self.args
30    }
31
32    /// The [`SourceLocation`](loc::SourceLocation) indicating where this
33    /// annotation was specified within some
34    /// [`GraphQLType`](crate::types::GraphQLType),
35    /// [`Mutation`](crate::operation::Mutation),
36    /// [`Query`](crate::operation::Query),
37    /// or [`Subscription`](crate::operation::Subscription).
38    pub fn def_location(&self) -> &loc::SourceLocation {
39        self.directive_ref.ref_location()
40    }
41
42    /// The [`Directive`] type for which this annotation refers to.
43    pub fn directive_type<'schema>(
44        &self,
45        schema: &'schema Schema,
46    ) -> &'schema Directive {
47        self.directive_ref.deref(schema).unwrap()
48    }
49
50    /// The name of the [`Directive`] type for which this annotation refers to.
51    ///
52    /// This can be useful when the [`Schema`] object is unavailable or
53    /// inconvenient to access but the type's name is all that is needed.
54    pub fn directive_type_name(&self) -> &str {
55        self.directive_ref.name()
56    }
57}