1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#![cfg_attr(docsrs, feature(doc_cfg))]
//! Minimal GraphQL client for rust
//!
//! * Simple API, supports queries and mutations
//! * Does not require schema file for introspection
//! * Supports WebAssembly
//!
//! # Basic Usage
//!
//! * Use client.query_with_vars for queries with variables
//! * There's also a wrapper client.query if there is no need to pass variables
//!
//! ```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<dyn std::error::Error>> {
//!    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_unwrap::<Data, Vars>(query, vars).await.unwrap();
//!
//!    println!("Id: {}, Name: {}", data.user.id, data.user.name);
//!
//!    Ok(())
//!}
//! ```
//!
//!
//! # Passing HTTP headers
//!
//! Client exposes new_with_headers 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<dyn std::error::Error>> {
//!    let endpoint = "https://graphqlzero.almansi.me/api";
//!    let mut headers = HashMap::new();
//!    headers.insert("authorization", "Bearer <some_token>");
//!
//!    let client = Client::new_with_headers(endpoint, headers);
//!
//!    Ok(())
//!}
//! ```
//!
//! # Error handling
//! There are two types of errors that can possibly occur. HTTP related errors (for example, authentication problem)
//! or GraphQL query errors in JSON response.
//! Debug, Display implementation of GraphQLError struct properly displays those error messages.
//! Additionally, you can also look at JSON content for more detailed output by calling err.json()
//!
//! ```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<dyn std::error::Error>> {
//!    let endpoint = "https://graphqlzero.almansi.me/api";
//!
//!    // Send incorrect request
//!    let query = r#"
//!        query UserByIdQuery($id: ID!) {
//!            user(id: $id) {
//!                id1
//!                name
//!            }
//!        }
//!    "#;
//!
//!    let client = Client::new(endpoint);
//!    let vars = Vars { id: 1 };
//!    let error = client.query_with_vars::<Data, Vars>(query, vars).await.err();
//!
//!    println!("{:?}", error);
//!
//!    Ok(())
//!}
//! ```

mod client;
mod error;
mod types;

pub use client::GQLClient as Client;
pub use error::GraphQLError;
pub use error::GraphQLErrorMessage;
pub use types::*;