Skip to main content

libgraphql_parser/ast/
definition.rs

1use crate::ast::AstNode;
2use crate::ast::DirectiveAnnotation;
3use crate::ast::DirectiveDefinition;
4use crate::ast::FragmentDefinition;
5use crate::ast::Name;
6use crate::ast::OperationDefinition;
7use crate::ast::SchemaDefinition;
8use crate::ast::SchemaExtension;
9use crate::ast::StringValue;
10use crate::ast::TypeDefinition;
11use crate::ast::TypeExtension;
12use crate::ByteSpan;
13use crate::SourceMap;
14use crate::SourceSpan;
15use inherent::inherent;
16
17/// A top-level definition in a GraphQL document.
18///
19/// Covers both type-system definitions (schema, types,
20/// directives, extensions) and executable definitions
21/// (operations, fragments).
22///
23/// See
24/// [Document](https://spec.graphql.org/September2025/#sec-Document)
25/// in the spec.
26#[derive(Clone, Debug, PartialEq)]
27pub enum Definition<'src> {
28    DirectiveDefinition(DirectiveDefinition<'src>),
29    FragmentDefinition(FragmentDefinition<'src>),
30    OperationDefinition(OperationDefinition<'src>),
31    SchemaDefinition(SchemaDefinition<'src>),
32    SchemaExtension(SchemaExtension<'src>),
33    TypeDefinition(TypeDefinition<'src>),
34    TypeExtension(TypeExtension<'src>),
35}
36
37impl<'src> Definition<'src> {
38    /// Returns the description string for this definition,
39    /// if one is present.
40    pub fn description(&self) -> Option<&StringValue<'src>> {
41        match self {
42            Self::DirectiveDefinition(def) => def.description.as_ref(),
43            Self::FragmentDefinition(def) => def.description.as_ref(),
44            Self::OperationDefinition(def) => def.description.as_ref(),
45            Self::SchemaDefinition(def) => def.description.as_ref(),
46            Self::SchemaExtension(_) => None,
47            Self::TypeDefinition(def) => def.description(),
48            Self::TypeExtension(_) => None,
49        }
50    }
51
52    /// Returns the directive annotations applied to this
53    /// definition.
54    pub fn directive_annotations(&self) -> &[DirectiveAnnotation<'src>] {
55        match self {
56            Self::DirectiveDefinition(_) => &[],
57            Self::FragmentDefinition(def) => &def.directives,
58            Self::OperationDefinition(def) => &def.directives,
59            Self::SchemaDefinition(def) => &def.directives,
60            Self::SchemaExtension(def) => &def.directives,
61            Self::TypeDefinition(def) => def.directive_annotations(),
62            Self::TypeExtension(def) => def.directive_annotations(),
63        }
64    }
65
66    /// Returns the [`Name`] of this definition, or [`None`]
67    /// for schema definitions/extensions (which have no name).
68    pub fn name(&self) -> Option<&Name<'src>> {
69        match self {
70            Self::DirectiveDefinition(def) => Some(&def.name),
71            Self::FragmentDefinition(def) => Some(&def.name),
72            Self::OperationDefinition(def) => def.name.as_ref(),
73            Self::SchemaDefinition(_) => None,
74            Self::SchemaExtension(_) => None,
75            Self::TypeDefinition(def) => Some(def.name()),
76            Self::TypeExtension(def) => Some(def.name()),
77        }
78    }
79
80    /// Returns the name of this definition as a string slice,
81    /// or [`None`] for unnamed definitions (schema
82    /// definitions/extensions, anonymous operations).
83    ///
84    /// Convenience accessor for `self.name().value`.
85    pub fn name_value(&self) -> Option<&str> {
86        self.name().map(|n| n.value.as_ref())
87    }
88}
89
90#[inherent]
91impl AstNode for Definition<'_> {
92    /// See [`AstNode::append_source()`](crate::ast::AstNode::append_source).
93    pub fn append_source(
94        &self,
95        sink: &mut String,
96        source: Option<&str>,
97    ) {
98        match self {
99            Definition::DirectiveDefinition(d) => {
100                d.append_source(sink, source)
101            },
102            Definition::FragmentDefinition(d) => {
103                d.append_source(sink, source)
104            },
105            Definition::OperationDefinition(d) => {
106                d.append_source(sink, source)
107            },
108            Definition::SchemaDefinition(d) => {
109                d.append_source(sink, source)
110            },
111            Definition::SchemaExtension(d) => {
112                d.append_source(sink, source)
113            },
114            Definition::TypeDefinition(d) => {
115                d.append_source(sink, source)
116            },
117            Definition::TypeExtension(d) => {
118                d.append_source(sink, source)
119            },
120        }
121    }
122
123    /// Returns this definition's byte-offset span within the
124    /// source text.
125    ///
126    /// The returned [`ByteSpan`] can be resolved to line/column
127    /// positions via [`source_span()`](Self::source_span) or
128    /// [`ByteSpan::resolve()`].
129    pub fn byte_span(&self) -> ByteSpan {
130        match self {
131            Self::DirectiveDefinition(def) => def.span,
132            Self::FragmentDefinition(def) => def.span,
133            Self::OperationDefinition(def) => def.span,
134            Self::SchemaDefinition(def) => def.span,
135            Self::SchemaExtension(def) => def.span,
136            Self::TypeDefinition(def) => def.byte_span(),
137            Self::TypeExtension(def) => def.byte_span(),
138        }
139    }
140
141    /// Resolves this definition's position to line/column
142    /// coordinates using the given [`SourceMap`].
143    ///
144    /// Returns [`None`] if the byte offsets cannot be resolved
145    /// (e.g. the span was synthetically constructed without
146    /// valid position data).
147    pub fn source_span(
148        &self,
149        source_map: &SourceMap,
150    ) -> Option<SourceSpan> {
151        self.byte_span().resolve(source_map)
152    }
153}