-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathvalidate.rs
35 lines (32 loc) · 1.17 KB
/
validate.rs
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
use std::io::Read;
/// A simple program to run the validations implemented by apollo-compiler
/// and report any errors.
///
/// To use, do:
/// cargo run --example validate path/to/input.graphql
fn main() {
let (source, filename) = match std::env::args().nth(1).as_deref() {
Some("-") | None => {
let mut source = String::new();
std::io::stdin().read_to_string(&mut source).unwrap();
(source, "stdin.graphql".to_string())
}
Some(filename) => (
std::fs::read_to_string(filename).unwrap(),
filename.to_string(),
),
};
let (schema, executable) = apollo_compiler::parse_mixed(source, filename);
let schema_result = schema.validate(Default::default());
let executable_result = executable.validate(&schema, Default::default());
let has_errors = schema_result.is_err() || executable_result.is_err();
match schema_result {
Ok(warnings) => println!("{warnings}"),
Err(errors) => println!("{errors}"),
}
match executable_result {
Ok(()) => {}
Err(errors) => println!("{errors}"),
}
std::process::exit(if has_errors { 1 } else { 0 });
}