use gqls::load;
use gqls::load::LoadOptions;
use gqls::model::Kind;
const ENDPOINT: &str = "https://countries.trevorblades.com/";
#[test]
#[ignore = "hits the network; run with --ignored"]
fn introspects_and_searches_a_live_endpoint() {
let opts = LoadOptions {
refresh: true,
..Default::default()
};
let records = load::load(ENDPOINT, &opts).expect("HTTP introspection should succeed");
assert!(!records.is_empty(), "expected a non-empty schema");
assert!(
records
.iter()
.any(|r| r.kind == Kind::Object && r.name == "Country"),
"expected a Country object type"
);
assert!(
records.iter().any(|r| r.path == "Query.country"),
"expected a Query.country root field"
);
let hits = gqls::search::search("contry", &records, Default::default());
assert!(
hits.iter().any(|h| h.record.name == "Country"),
"fuzzy search should surface Country despite the typo"
);
}
#[test]
#[ignore = "hits the network; run with --ignored"]
fn drafted_operation_executes_against_the_live_endpoint() {
let opts = LoadOptions {
refresh: true,
..Default::default()
};
let records = load::load(ENDPOINT, &opts).expect("HTTP introspection should succeed");
let target = records
.iter()
.find(|r| r.path == "Query.country")
.expect("expected a Query.country root field");
let drafted = gqls::example::build(target, &records, 1).expect("drafting should succeed");
assert_eq!(
drafted.variables,
serde_json::json!({ "code": "<ID!>" }),
"{}",
drafted.operation
);
let response: serde_json::Value = ureq::post(ENDPOINT)
.send_json(ureq::json!({
"query": drafted.operation,
"variables": { "code": "US" },
}))
.expect("the drafted operation should POST cleanly")
.into_json()
.expect("a JSON response");
assert!(
response.get("errors").is_none(),
"server rejected the drafted operation: {response}\n{}",
drafted.operation
);
assert_eq!(
response
.pointer("/data/country/code")
.and_then(|v| v.as_str()),
Some("US"),
"unexpected payload: {response}"
);
}