This library is a lightweight library that let you make graphql request and serialize them into the given type. The fields of the given type should match with the "data" content of the reponses. Examples:
```no_test use lightql::{Request, QlType};
let response = json!( "data": { "players": [ { "id": 1, "name": "fred" }, { "id": 2, "name": "marcel" }, { "id": 3, "name": "roger" } ] } )
// The given type should look like that:
struct Player { #[serde(default = "getdefaultid")] id: i32, name: String }
struct Players {
players: Vec
// This will work. // But here if you want to request one player:
let response = json!( "data": { "player": { "id": 4, "name": "bouras" } } );
struct OnePlayer { player: Player }
// default function fn getdefaultid() -> i32 { 0 }
// You have to match the "data" content with field. ```
To use the library there is the Request
struct that encapsulate the request.
```no_test
let player: OnePlayer = Request::new("player(id: 1) { name }", QlType::Query)
.send("https://your-api/")
.expect("Cannot fetch player");
// print: Player { id: 0, name: "fred" }
println!("{:?}", player);
``` Send automatically infer type of deserialisation.
You can make complexe request request from a file for example:
no_test
let response = Request::from_path("path/to/ql/request.query")
.send::<OnePlayer>("http://your-api/") // Note the turbo-fish
.unwrap();
If you need runtime parameter you can use prepare and an hasmap. ```notest let userid = getuserid(&user);
let mut params = Hasmap::new(); params.insert( "id", userid.parse() );
let response = Request::new("player(id: _id) { id, name }", QlType::Query)
.prepare(Some(params)) // Should prepare before sending !
.send::
```
You can post issues and pull requests I will read them. Please respect the Rust style.