Skip to main content

libgraphql_parser/ast/
input_value_definition.rs

1use crate::ast::ast_node::append_span_source_slice;
2use crate::ast::AstNode;
3use crate::ast::DirectiveAnnotation;
4use crate::ast::Name;
5use crate::ast::StringValue;
6use crate::ast::TypeAnnotation;
7use crate::ast::Value;
8use crate::ByteSpan;
9use crate::SourceMap;
10use crate::SourceSpan;
11use crate::token::GraphQLToken;
12use inherent::inherent;
13
14/// An input value definition, used for field arguments and
15/// input object fields.
16///
17/// See
18/// [Input Values Definitions](https://spec.graphql.org/September2025/#InputValueDefinition)
19/// in the spec.
20#[derive(Clone, Debug, PartialEq)]
21pub struct InputValueDefinition<'src> {
22    pub default_value: Option<Value<'src>>,
23    pub description: Option<StringValue<'src>>,
24    pub directives: Vec<DirectiveAnnotation<'src>>,
25    pub name: Name<'src>,
26    pub span: ByteSpan,
27    pub syntax: Option<Box<InputValueDefinitionSyntax<'src>>>,
28    pub value_type: TypeAnnotation<'src>,
29}
30
31/// Syntax detail for an [`InputValueDefinition`].
32#[derive(Clone, Debug, PartialEq)]
33pub struct InputValueDefinitionSyntax<'src> {
34    pub colon: GraphQLToken<'src>,
35    pub equals: Option<GraphQLToken<'src>>,
36}
37
38impl<'src> InputValueDefinition<'src> {
39    /// Returns the name of this input value definition as a string
40    /// slice.
41    ///
42    /// Convenience accessor for `self.name.value`.
43    #[inline]
44    pub fn name_value(&self) -> &str {
45        self.name.value.as_ref()
46    }
47}
48
49#[inherent]
50impl AstNode for InputValueDefinition<'_> {
51    /// See [`AstNode::append_source()`](crate::ast::AstNode::append_source).
52    pub fn append_source(
53        &self,
54        sink: &mut String,
55        source: Option<&str>,
56    ) {
57        if let Some(src) = source {
58            append_span_source_slice(
59                self.span, sink, src,
60            );
61        }
62    }
63
64    /// Returns this input value definition's byte-offset span within the
65    /// source text.
66    ///
67    /// The returned [`ByteSpan`] can be resolved to line/column
68    /// positions via [`source_span()`](Self::source_span) or
69    /// [`ByteSpan::resolve()`].
70    #[inline]
71    pub fn byte_span(&self) -> ByteSpan {
72        self.span
73    }
74
75    /// Resolves this input value definition's position to line/column
76    /// coordinates using the given [`SourceMap`].
77    ///
78    /// Returns [`None`] if the byte offsets cannot be resolved
79    /// (e.g. the span was synthetically constructed without
80    /// valid position data).
81    #[inline]
82    pub fn source_span(
83        &self,
84        source_map: &SourceMap,
85    ) -> Option<SourceSpan> {
86        self.byte_span().resolve(source_map)
87    }
88}