Skip to content

Commit 61ccda9

Browse files
committed
feat: add description support to AST and executable structures
1 parent 35434b3 commit 61ccda9

File tree

6 files changed

+18
-0
lines changed

6 files changed

+18
-0
lines changed

crates/apollo-compiler/src/ast/from_cst.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ impl Convert for cst::OperationDefinition {
125125
ast::OperationType::Query
126126
};
127127
Some(Self::Target {
128+
description: self.description().convert(file_id)?,
128129
operation_type,
129130
name: self.name().convert(file_id)?,
130131
variables: collect_opt(file_id, self.variable_definitions(), |x| {
@@ -147,6 +148,7 @@ impl Convert for cst::FragmentDefinition {
147148

148149
fn convert(&self, file_id: FileId) -> Option<Self::Target> {
149150
Some(Self::Target {
151+
description: self.description().convert(file_id)?,
150152
name: self.fragment_name()?.name()?.convert(file_id)?,
151153
type_condition: self.type_condition()?.convert(file_id)?,
152154
directives: ast::DirectiveList(collect_opt(file_id, self.directives(), |x| {
@@ -530,6 +532,7 @@ impl Convert for cst::VariableDefinition {
530532
};
531533
let ty = &self.ty()?;
532534
Some(Self::Target {
535+
description: self.description().convert(file_id)?,
533536
name: self.variable()?.name()?.convert(file_id)?,
534537
ty: with_location(file_id, ty.syntax(), ty.convert(file_id)?),
535538
default_value,

crates/apollo-compiler/src/ast/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ pub enum Definition {
116116
/// [_OperationDefinition_](https://spec.graphql.org/draft/#OperationDefinition).
117117
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
118118
pub struct OperationDefinition {
119+
pub description: Option<Node<str>>,
119120
pub operation_type: OperationType,
120121
pub name: Option<Name>,
121122
pub variables: Vec<Node<VariableDefinition>>,
@@ -127,6 +128,7 @@ pub struct OperationDefinition {
127128
/// [_FragmentDefinition_](https://spec.graphql.org/draft/#FragmentDefinition).
128129
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
129130
pub struct FragmentDefinition {
131+
pub description: Option<Node<str>>,
130132
pub name: Name,
131133
pub type_condition: NamedType,
132134
pub directives: DirectiveList,
@@ -335,6 +337,7 @@ pub enum DirectiveLocation {
335337
/// in an [`OperationDefinition`].
336338
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
337339
pub struct VariableDefinition {
340+
pub description: Option<Node<str>>,
338341
pub name: Name,
339342
pub ty: Node<Type>,
340343
pub default_value: Option<Node<Value>>,

crates/apollo-compiler/src/ast/serialize.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,12 +194,14 @@ impl OperationDefinition {
194194
fn serialize_impl(&self, state: &mut State) -> fmt::Result {
195195
// Deconstruct to get a warning if we forget to serialize something
196196
let Self {
197+
description,
197198
operation_type,
198199
name,
199200
variables,
200201
directives,
201202
selection_set,
202203
} = self;
204+
serialize_description(state, description)?;
203205
// Only use shorthand when this is the first item.
204206
// If not, it might be following a `[lookahead != "{"]` grammar production
205207
let shorthand = state.output_empty
@@ -230,11 +232,13 @@ impl OperationDefinition {
230232
impl FragmentDefinition {
231233
fn serialize_impl(&self, state: &mut State) -> fmt::Result {
232234
let Self {
235+
description,
233236
name,
234237
type_condition,
235238
directives,
236239
selection_set,
237240
} = self;
241+
serialize_description(state, description)?;
238242
display!(state, "fragment {} on {}", name, type_condition)?;
239243
directives.serialize_impl(state)?;
240244
state.write(" ")?;
@@ -581,11 +585,13 @@ impl Directive {
581585
impl VariableDefinition {
582586
fn serialize_impl(&self, state: &mut State) -> fmt::Result {
583587
let Self {
588+
description,
584589
name,
585590
ty,
586591
default_value,
587592
directives,
588593
} = self;
594+
serialize_description(state, description)?;
589595
state.write("$")?;
590596
state.write(name)?;
591597
state.write(": ")?;

crates/apollo-compiler/src/executable/from_ast.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ impl Operation {
140140
let mut selection_set = SelectionSet::new(ty);
141141
selection_set.extend_from_ast(schema, errors, &ast.selection_set);
142142
Some(Self {
143+
description: ast.description.clone(),
143144
operation_type: ast.operation_type,
144145
name: ast.name.clone(),
145146
variables: ast.variables.clone(),
@@ -170,6 +171,7 @@ impl Fragment {
170171
let mut selection_set = SelectionSet::new(ast.type_condition.clone());
171172
selection_set.extend_from_ast(schema, errors, &ast.selection_set);
172173
Some(Self {
174+
description: ast.description.clone(),
173175
name: ast.name.clone(),
174176
directives: ast.directives.clone(),
175177
selection_set,

crates/apollo-compiler/src/executable/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ pub struct FieldSet {
123123
/// annotated with type information.
124124
#[derive(Debug, Clone, PartialEq, Eq)]
125125
pub struct Operation {
126+
pub description: Option<Node<str>>,
126127
pub operation_type: OperationType,
127128
pub name: Option<Name>,
128129
pub variables: Vec<Node<VariableDefinition>>,
@@ -134,6 +135,7 @@ pub struct Operation {
134135
/// annotated with type information.
135136
#[derive(Debug, Clone, PartialEq, Eq)]
136137
pub struct Fragment {
138+
pub description: Option<Node<str>>,
137139
pub name: Name,
138140
pub directives: DirectiveList,
139141
pub selection_set: SelectionSet,

crates/apollo-compiler/src/executable/serialize.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ impl ExecutableDocument {
2828
impl Operation {
2929
fn to_ast(&self, location: Option<SourceSpan>) -> ast::Definition {
3030
let def = ast::OperationDefinition {
31+
description: self.description.clone(),
3132
operation_type: self.operation_type,
3233
name: self.name.clone(),
3334
variables: self.variables.clone(),
@@ -45,6 +46,7 @@ impl Operation {
4546
impl Fragment {
4647
fn to_ast(&self, location: Option<SourceSpan>) -> ast::Definition {
4748
let def = ast::FragmentDefinition {
49+
description: self.description.clone(),
4850
name: self.name.clone(),
4951
type_condition: self.selection_set.ty.clone(),
5052
directives: self.directives.clone(),

0 commit comments

Comments
 (0)