Skip to main content

libgraphql_parser/ast/
type_extension.rs

1use crate::ast::AstNode;
2use crate::ast::DirectiveAnnotation;
3use crate::ast::EnumTypeExtension;
4use crate::ast::InputObjectTypeExtension;
5use crate::ast::InterfaceTypeExtension;
6use crate::ast::Name;
7use crate::ast::ObjectTypeExtension;
8use crate::ast::ScalarTypeExtension;
9use crate::ast::UnionTypeExtension;
10use crate::ByteSpan;
11use crate::SourceMap;
12use crate::SourceSpan;
13use inherent::inherent;
14
15/// A type extension in a GraphQL schema.
16///
17/// See
18/// [Type Extensions](https://spec.graphql.org/September2025/#sec-Type-Extensions)
19/// in the spec.
20#[derive(Clone, Debug, PartialEq)]
21pub enum TypeExtension<'src> {
22    Enum(EnumTypeExtension<'src>),
23    InputObject(InputObjectTypeExtension<'src>),
24    Interface(InterfaceTypeExtension<'src>),
25    Object(ObjectTypeExtension<'src>),
26    Scalar(ScalarTypeExtension<'src>),
27    Union(UnionTypeExtension<'src>),
28}
29
30impl<'src> TypeExtension<'src> {
31    /// Returns the directive annotations applied to this type extension.
32    pub fn directive_annotations(
33        &self,
34    ) -> &[DirectiveAnnotation<'src>] {
35        match self {
36            Self::Enum(ext) => &ext.directives,
37            Self::InputObject(ext) => &ext.directives,
38            Self::Interface(ext) => &ext.directives,
39            Self::Object(ext) => &ext.directives,
40            Self::Scalar(ext) => &ext.directives,
41            Self::Union(ext) => &ext.directives,
42        }
43    }
44
45    /// Returns the [`Name`] of this type extension.
46    pub fn name(&self) -> &Name<'src> {
47        match self {
48            Self::Enum(ext) => &ext.name,
49            Self::InputObject(ext) => &ext.name,
50            Self::Interface(ext) => &ext.name,
51            Self::Object(ext) => &ext.name,
52            Self::Scalar(ext) => &ext.name,
53            Self::Union(ext) => &ext.name,
54        }
55    }
56
57    /// Returns the name of this type extension as a string
58    /// slice.
59    ///
60    /// Convenience accessor for `self.name().value`.
61    pub fn name_value(&self) -> &str {
62        self.name().value.as_ref()
63    }
64}
65
66#[inherent]
67impl AstNode for TypeExtension<'_> {
68    /// See [`AstNode::append_source()`](crate::ast::AstNode::append_source).
69    pub fn append_source(
70        &self,
71        sink: &mut String,
72        source: Option<&str>,
73    ) {
74        match self {
75            TypeExtension::Enum(d) => {
76                d.append_source(sink, source)
77            },
78            TypeExtension::InputObject(d) => {
79                d.append_source(sink, source)
80            },
81            TypeExtension::Interface(d) => {
82                d.append_source(sink, source)
83            },
84            TypeExtension::Object(d) => {
85                d.append_source(sink, source)
86            },
87            TypeExtension::Scalar(d) => {
88                d.append_source(sink, source)
89            },
90            TypeExtension::Union(d) => {
91                d.append_source(sink, source)
92            },
93        }
94    }
95
96    /// Returns this type extension's byte-offset span within
97    /// the source text.
98    ///
99    /// The returned [`ByteSpan`] can be resolved to line/column
100    /// positions via [`source_span()`](Self::source_span) or
101    /// [`ByteSpan::resolve()`].
102    pub fn byte_span(&self) -> ByteSpan {
103        match self {
104            Self::Enum(ext) => ext.span,
105            Self::InputObject(ext) => ext.span,
106            Self::Interface(ext) => ext.span,
107            Self::Object(ext) => ext.span,
108            Self::Scalar(ext) => ext.span,
109            Self::Union(ext) => ext.span,
110        }
111    }
112
113    /// Resolves this type extension's position to line/column
114    /// coordinates using the given [`SourceMap`].
115    ///
116    /// Returns [`None`] if the byte offsets cannot be resolved
117    /// (e.g. the span was synthetically constructed without
118    /// valid position data).
119    pub fn source_span(
120        &self,
121        source_map: &SourceMap,
122    ) -> Option<SourceSpan> {
123        self.byte_span().resolve(source_map)
124    }
125}