1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
use apollo_parser::{Parser as ApolloParser, SyntaxTree};
use rowan::GreenNode;
use crate::{database::inputs::InputDatabase, diagnostics::SyntaxError, ApolloDiagnostic, FileId};
#[salsa::query_group(AstStorage)]
pub trait AstDatabase: InputDatabase {
fn ast(&self, file_id: FileId) -> SyntaxTree;
fn document(&self, file_id: FileId) -> GreenNode;
fn syntax_errors(&self) -> Vec<ApolloDiagnostic>;
}
fn ast(db: &dyn AstDatabase, file_id: FileId) -> SyntaxTree {
let input = db.input(file_id).text();
let parser = ApolloParser::new(&input);
let parser = if let Some(limit) = db.recursion_limit() {
parser.recursion_limit(limit)
} else {
parser
};
parser.parse()
}
fn document(db: &dyn AstDatabase, file_id: FileId) -> GreenNode {
db.ast(file_id).green()
}
fn syntax_errors(db: &dyn AstDatabase) -> Vec<ApolloDiagnostic> {
db.source_files()
.into_iter()
.flat_map(|file_id| {
db.ast(file_id)
.errors()
.into_iter()
.map(|err| {
ApolloDiagnostic::SyntaxError(SyntaxError {
src: db.source_code(file_id),
span: (err.index(), err.data().len()).into(), message: err.message().into(),
})
})
.collect::<Vec<ApolloDiagnostic>>()
})
.collect()
}
#[cfg(test)]
mod tests {
use super::*;
use crate::ApolloCompiler;
#[test]
fn it_errors_when_selection_set_recursion_limit_exceeded() {
let schema = r#"
query {
Q1 {
url
}
}
"#;
let mut compiler = ApolloCompiler::with_recursion_limit(1);
let doc_id = compiler.add_document(schema, "schema.graphql");
let ast = compiler.db.ast(doc_id);
assert_eq!(ast.recursion_limit().high, 2);
assert_eq!(ast.errors().len(), 1);
assert_eq!(ast.document().definitions().into_iter().count(), 2);
}
#[test]
fn it_passes_when_selection_set_recursion_limit_is_not_exceeded() {
let schema = r#"
query {
Q1 {
url
}
}
"#;
let mut compiler = ApolloCompiler::with_recursion_limit(7);
let doc_id = compiler.add_document(schema, "schema.graphql");
let ast = compiler.db.ast(doc_id);
assert_eq!(ast.recursion_limit().high, 4);
assert_eq!(ast.errors().len(), 0);
assert_eq!(ast.document().definitions().into_iter().count(), 1);
}
}