Giter VIP home page Giter VIP logo

gql-client-rs's Introduction

gql_client

Minimal GraphQL client for Rust

Build Status crates.io docs

  • 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
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::<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>

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()

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(())
}

gql-client-rs's People

Contributors

arthurkhlghatyan avatar fewensa avatar vyneer avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.