reqwest_graphql/
lib.rs

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