Skip to content

Commit e8105a7

Browse files
committed
feat(compiler, parser): support executable document descriptions
Support for parsing and serializing descriptions on: - (longhand) operation definitions - fragment definitions - variable definitions As described by the latest GraphQL spec draft: graphql/graphql-spec#1170 Adding a description on a query shorthand (bare braced selection set) raises a parse error. This is a breaking change for apollo-compiler due to the addition of new fields: - `apollo_compiler::ast::OperationDefinition::description` - `apollo_compiler::ast::FragmentDefinition::description` - `apollo_compiler::ast::VariableDefinition::description` - `apollo_compiler::executable::Operation::description` - `apollo_compiler::executable::Fragment::description` For most users, adding `description: None` anywhere they create one of these structures is good enough.
1 parent 88c48c3 commit e8105a7

File tree

46 files changed

+823
-35
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+823
-35
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,6 +194,7 @@ 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,
@@ -208,6 +209,7 @@ impl OperationDefinition {
208209
&& variables.is_empty()
209210
&& directives.is_empty();
210211
if !shorthand {
212+
serialize_description(state, description)?;
211213
state.write(operation_type.name())?;
212214
if let Some(name) = &name {
213215
state.write(" ")?;
@@ -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(),
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
"Get all our time machine models."
2+
{
3+
timeMachines {
4+
model
5+
}
6+
}
7+
8+
type TimeMachine {
9+
id: ID!
10+
model: String
11+
}
12+
type Query {
13+
timeMachines: [TimeMachine]
14+
}

crates/apollo-compiler/test_data/ok/0001_annonymous_operation_definition.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ ExecutableDocument {
9898
operations: OperationMap {
9999
anonymous: Some(
100100
1..31 @3 Operation {
101+
description: None,
101102
operation_type: Query,
102103
name: None,
103104
variables: [],

crates/apollo-compiler/test_data/ok/0002_multiple_named_operation_definitions.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ ExecutableDocument {
133133
anonymous: None,
134134
named: {
135135
"getCatName": 0..41 @4 Operation {
136+
description: None,
136137
operation_type: Query,
137138
name: Some(
138139
"getCatName",
@@ -189,6 +190,7 @@ ExecutableDocument {
189190
},
190191
},
191192
"getOwnerName": 43..106 @4 Operation {
193+
description: None,
192194
operation_type: Query,
193195
name: Some(
194196
"getOwnerName",

crates/apollo-compiler/test_data/ok/0010_operation_with_defined_fields.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ ExecutableDocument {
252252
anonymous: None,
253253
named: {
254254
"getProduct": 0..68 @12 Operation {
255+
description: None,
255256
operation_type: Query,
256257
name: Some(
257258
"getProduct",

0 commit comments

Comments
 (0)