|
23 | 23 | /// echo "Dump $db"
|
24 | 24 | /// q=$(printf "$query" "$dir/$db.json")
|
25 | 25 | /// psql -qXt service=$db -c "$q"
|
| 26 | +/// sed -r -i -e 's/\\\\/\\/g' "$dir/$db.json" |
26 | 27 | /// done
|
27 | 28 | ///
|
28 | 29 | /// ```
|
| 30 | +use clap::Parser; |
| 31 | + |
29 | 32 | use graph::data::graphql::ext::DirectiveFinder;
|
30 | 33 | use graph::data::graphql::DirectiveExt;
|
31 | 34 | use graph::data::graphql::DocumentExt;
|
@@ -77,32 +80,75 @@ struct Entry {
|
77 | 80 | schema: String,
|
78 | 81 | }
|
79 | 82 |
|
| 83 | +#[derive(Parser)] |
| 84 | +#[clap( |
| 85 | + name = "validate", |
| 86 | + version = env!("CARGO_PKG_VERSION"), |
| 87 | + author = env!("CARGO_PKG_AUTHORS"), |
| 88 | + about = "Validate subgraph schemas" |
| 89 | +)] |
| 90 | +struct Opts { |
| 91 | + /// Validate a batch of schemas in bulk. When this is set, the input |
| 92 | + /// files must be JSONL files where each line has an `id` and a `schema` |
| 93 | + #[clap(short, long)] |
| 94 | + batch: bool, |
| 95 | + #[clap(long)] |
| 96 | + api: bool, |
| 97 | + /// Subgraph schemas to validate |
| 98 | + #[clap(required = true)] |
| 99 | + schemas: Vec<String>, |
| 100 | +} |
| 101 | + |
| 102 | +fn parse(raw: &str, name: &str, api: bool) { |
| 103 | + let schema = ensure( |
| 104 | + parse_schema(raw).map(|v| v.into_static()), |
| 105 | + &format!("Failed to parse schema sgd{}", name), |
| 106 | + ); |
| 107 | + let id = subgraph_id(&schema); |
| 108 | + let input_schema = match InputSchema::parse(raw, id.clone()) { |
| 109 | + Ok(schema) => schema, |
| 110 | + Err(e) => { |
| 111 | + println!("InputSchema: {}[{}]: {}", name, id, e); |
| 112 | + return; |
| 113 | + } |
| 114 | + }; |
| 115 | + if api { |
| 116 | + let _api_schema = match input_schema.api_schema() { |
| 117 | + Ok(schema) => schema, |
| 118 | + Err(e) => { |
| 119 | + println!("ApiSchema: {}[{}]: {}", name, id, e); |
| 120 | + return; |
| 121 | + } |
| 122 | + }; |
| 123 | + } |
| 124 | + println!("Schema {}[{}]: OK", name, id); |
| 125 | +} |
| 126 | + |
80 | 127 | pub fn main() {
|
81 | 128 | // Allow fulltext search in schemas
|
82 | 129 | std::env::set_var("GRAPH_ALLOW_NON_DETERMINISTIC_FULLTEXT_SEARCH", "true");
|
83 | 130 |
|
84 |
| - let args: Vec<String> = env::args().collect(); |
85 |
| - if args.len() < 2 { |
86 |
| - usage("please provide a subgraph schema"); |
87 |
| - } |
88 |
| - for arg in &args[1..] { |
89 |
| - println!("Validating schemas from {arg}"); |
90 |
| - let file = File::open(arg).expect("file exists"); |
91 |
| - let rdr = BufReader::new(file); |
92 |
| - for line in rdr.lines() { |
93 |
| - let line = line.expect("invalid line").replace("\\\\", "\\"); |
94 |
| - let entry = serde_json::from_str::<Entry>(&line).expect("line is valid json"); |
| 131 | + let opt = Opts::parse(); |
95 | 132 |
|
96 |
| - let raw = &entry.schema; |
97 |
| - let schema = ensure( |
98 |
| - parse_schema(raw).map(|v| v.into_static()), |
99 |
| - &format!("Failed to parse schema sgd{}", entry.id), |
100 |
| - ); |
101 |
| - let id = subgraph_id(&schema); |
102 |
| - match InputSchema::parse(raw, id.clone()) { |
103 |
| - Ok(_) => println!("sgd{}[{}]: OK", entry.id, id), |
104 |
| - Err(e) => println!("sgd{}[{}]: {}", entry.id, id, e), |
| 133 | + if opt.batch { |
| 134 | + for schema in &opt.schemas { |
| 135 | + println!("Validating schemas from {schema}"); |
| 136 | + let file = File::open(schema).expect("file exists"); |
| 137 | + let rdr = BufReader::new(file); |
| 138 | + for line in rdr.lines() { |
| 139 | + let line = line.expect("invalid line").replace("\\\\", "\\"); |
| 140 | + let entry = serde_json::from_str::<Entry>(&line).expect("line is valid json"); |
| 141 | + |
| 142 | + let raw = &entry.schema; |
| 143 | + let name = format!("sgd{}", entry.id); |
| 144 | + parse(raw, &name, opt.api); |
105 | 145 | }
|
106 | 146 | }
|
| 147 | + } else { |
| 148 | + for schema in &opt.schemas { |
| 149 | + println!("Validating schema from {schema}"); |
| 150 | + let raw = std::fs::read_to_string(schema).expect("file exists"); |
| 151 | + parse(&raw, schema, opt.api); |
| 152 | + } |
107 | 153 | }
|
108 | 154 | }
|
0 commit comments