gql_client

Minimal GraphQL client for Rust

Build Status crates.io docs

Features

Basic Usage

```rust use gql_client::Client; use serde::{Deserialize, Serialize};

[derive(Deserialize)]

pub struct Data { user: User }

[derive(Deserialize)]

pub struct User { id: String, name: String }

[derive(Serialize)]

pub struct Vars { id: u32 }

[tokio::main]

async fn main() -> Result<(), Box> { let endpoint = "https://graphqlzero.almansi.me/api"; let query = r#" query UserByIdQuery($id: ID!) { user(id: $id) { id name } }"#;

 let client = Client::new(endpoint);
 let vars = Vars { id: 1 };
 let data = client.query_with_vars::<Data, Vars>(query, vars).await.unwrap();

 println!("Id: {}, Name: {}", data.user.id, data.user.name);

 Ok(())

} ```

# Passing HTTP headers

Client exposes newwithheaders function to pass headers using simple HashMap<&str, &str>

```rust use gql_client::Client; use std::collections::HashMap;

[tokio::main]

async fn main() -> Result<(), Box> { let endpoint = "https://graphqlzero.almansi.me/api"; let mut headers = HashMap::new(); headers.insert("authorization", "Bearer ");

let client = Client::new_with_headers(endpoint, headers);

Ok(())

} ```