[][src]Crate lightql

LightQl

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:

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:
#[derive(Deserialize)]
struct Player {
    #[serde(default = "get_default_id")]
    id: i32,
    name: String
}

#[derive(Deserialize)]
struct Players {
    players: Vec<Player>
}

// This will work.
// But here if you want to request one player:

let response = json!(
    "data": {
        "player": {
            "id": 4,
            "name": "bouras"
        }
    }
);

#[derive(Deserialize)]
struct OnePlayer {
    player: Player
}

// default function
fn get_default_id() -> i32 {
    0
}

// You have to match the "data" content with field.

Usage

To use the library there is the Request struct that encapsulate the request.

    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.

From file

You can make complexe request request from a file for example:

let response = Request::from_path("path/to/ql/request.query")
    .send::<OnePlayer>("http://your-api/") // Note the turbo-fish
    .unwrap();

Parameter

If you need runtime parameter you can use prepare and an hasmap.

let user_id = get_user_id(&user);

let mut params = Hasmap::new();
params.insert(
    "_id",
    user_id.parse()
);

let response = Request::new("player(id: _id) { id, name }", QlType::Query)
    .prepare(Some(params)) // Should prepare before sending !
    .send::<OnePlayer>("https://your-api/")
    .unwrap(); // If not prepared return Err(NotPrepared)

Re-exports

pub extern crate serde_derive;

Structs

Locations

A location in a graphql Error.

QlErrorInternal

A Error message that have to be Deserialized. TODO add a pretty print.

QlRequestTyped

A wrapper around Request where the type is contained.

Request

The principal struct of the lib. It's the API that you should use.

Enums

QlError

Error type of the crate represent an backendError or a lib usage error.

QlType

Define the type of the request. either: a query, a subscription or a mutation.

Value

Represents any valid JSON value.

Traits

FromQl

Trait that will be derived in a near future.