Skip to content

Commit d5bf6dd

Browse files
committed
graph: Make the 'validate' helper/example more usable
1 parent 6a843af commit d5bf6dd

File tree

1 file changed

+66
-20
lines changed

1 file changed

+66
-20
lines changed

graph/examples/validate.rs

Lines changed: 66 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,12 @@
2323
/// echo "Dump $db"
2424
/// q=$(printf "$query" "$dir/$db.json")
2525
/// psql -qXt service=$db -c "$q"
26+
/// sed -r -i -e 's/\\\\/\\/g' "$dir/$db.json"
2627
/// done
2728
///
2829
/// ```
30+
use clap::Parser;
31+
2932
use graph::data::graphql::ext::DirectiveFinder;
3033
use graph::data::graphql::DirectiveExt;
3134
use graph::data::graphql::DocumentExt;
@@ -77,32 +80,75 @@ struct Entry {
7780
schema: String,
7881
}
7982

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+
80127
pub fn main() {
81128
// Allow fulltext search in schemas
82129
std::env::set_var("GRAPH_ALLOW_NON_DETERMINISTIC_FULLTEXT_SEARCH", "true");
83130

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();
95132

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);
105145
}
106146
}
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+
}
107153
}
108154
}

0 commit comments

Comments
 (0)