Giter Site home page Giter Site logo

byo-graphql's Introduction

MIT Latest Version docs Chat on Miaou

A simple "bring your own queries and types" GraphQL client.

Design Principles

  • I want to use my own Rust structures and not have them generated by macros
  • I want to use my own queries and not have them generated by macros
  • I want to manage queries as strings in my Rust code

Why not use it

  • For now it's very young, the API may grow and change
  • There's already a popular and well tested GraphQL client in Rust

How to use it

Simple query

The github stars example demonstrates querying GitHub's GraphQL API to get the number of stars of a repository.

First create a client, that you may keep and reuse:

let mut graphql_client = GraphqlClient::new("https://api.github.com/graphql")?;
graphql_client.set_bearer_auth("your-github-api-token");

You need the struct into which to deserialize the server's answer:

#[derive(Deserialize)]
pub struct Repository {
    stargazers: Count,
}

(Count is a utility struct provided by byo_graphql, it's just struct Count { totalCount: usize })

And you need a query:

let query = r#"{
	repository(owner: "Canop", name: "bacon") {
	    stargazers {
		totalCount
	    }
	}
}"#;

note: in the example's complete code, the query is dynamically built with format!, as you'll usually do.

Now you can fetch the data:

let repo: Repository = graphql_client.get_first_item(query).await?;
let stars: usize = repo.stargazers.into();

Querying a long list

The github issues example demonstrates how to query a long list with a cursor based exchange.

byo-graphql's People

Contributors

canop avatar

Stargazers

 avatar

Watchers

 avatar  avatar  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.