Skip to main content

libgraphql_parser/ast/
inline_fragment.rs

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