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