Skip to main content

libgraphql_parser/ast/
schema_extension.rs

1use crate::ast::ast_node::append_span_source_slice;
2use crate::ast::AstNode;
3use crate::ast::DelimiterPair;
4use crate::ast::DirectiveAnnotation;
5use crate::ast::RootOperationTypeDefinition;
6use crate::ByteSpan;
7use crate::SourceMap;
8use crate::SourceSpan;
9use crate::token::GraphQLToken;
10use inherent::inherent;
11
12/// A schema extension.
13///
14/// See
15/// [Schema Extension](https://spec.graphql.org/September2025/#sec-Schema-Extension)
16/// in the spec.
17#[derive(Clone, Debug, PartialEq)]
18pub struct SchemaExtension<'src> {
19    pub directives: Vec<DirectiveAnnotation<'src>>,
20    pub root_operations:
21        Vec<RootOperationTypeDefinition<'src>>,
22    pub span: ByteSpan,
23    pub syntax: Option<Box<SchemaExtensionSyntax<'src>>>,
24}
25
26/// Syntax detail for a [`SchemaExtension`].
27#[derive(Clone, Debug, PartialEq)]
28pub struct SchemaExtensionSyntax<'src> {
29    pub braces: Option<DelimiterPair<'src>>,
30    pub extend_keyword: GraphQLToken<'src>,
31    pub schema_keyword: GraphQLToken<'src>,
32}
33
34#[inherent]
35impl AstNode for SchemaExtension<'_> {
36    /// See [`AstNode::append_source()`](crate::ast::AstNode::append_source).
37    pub fn append_source(
38        &self,
39        sink: &mut String,
40        source: Option<&str>,
41    ) {
42        if let Some(src) = source {
43            append_span_source_slice(
44                self.span, sink, src,
45            );
46        }
47    }
48
49    /// Returns this schema extension's byte-offset span within the
50    /// source text.
51    ///
52    /// The returned [`ByteSpan`] can be resolved to line/column
53    /// positions via [`source_span()`](Self::source_span) or
54    /// [`ByteSpan::resolve()`].
55    #[inline]
56    pub fn byte_span(&self) -> ByteSpan {
57        self.span
58    }
59
60    /// Resolves this schema extension's position to line/column
61    /// coordinates using the given [`SourceMap`].
62    ///
63    /// Returns [`None`] if the byte offsets cannot be resolved
64    /// (e.g. the span was synthetically constructed without
65    /// valid position data).
66    #[inline]
67    pub fn source_span(
68        &self,
69        source_map: &SourceMap,
70    ) -> Option<SourceSpan> {
71        self.byte_span().resolve(source_map)
72    }
73}