67 releases (37 stable)

3.7.3 Jun 4, 2024
3.7.0 Apr 28, 2024
3.5.0 Mar 25, 2024
3.3.0 Nov 14, 2023
0.1.2 Feb 4, 2020

#1 in #graphql-client

Download history 9271/week @ 2024-03-02 10870/week @ 2024-03-09 11537/week @ 2024-03-16 9932/week @ 2024-03-23 11900/week @ 2024-03-30 10113/week @ 2024-04-06 10639/week @ 2024-04-13 12073/week @ 2024-04-20 11487/week @ 2024-04-27 11694/week @ 2024-05-04 12076/week @ 2024-05-11 11364/week @ 2024-05-18 10663/week @ 2024-05-25 10908/week @ 2024-06-01 10913/week @ 2024-06-08 9645/week @ 2024-06-15

43,925 downloads per month
Used in 49 crates (22 directly)

MPL-2.0 license

155KB
3.5K SLoC

Cynic

A bring your own types GraphQL client for Rust

Crate Info API Docs Discord Chat

Documentation | Examples | Changelog

Overview

Cynic is a GraphQL library for Rust. It's not the first but it takes a different approach from the existing libraries.

Existing libraries take a query first approach to GQL - you write a query using GraphQL and libraries use that to generate Rust structs for you using macros. This is really easy and great for getting going quickly. However, if you want to use structs that aren't quite what the macros output you're out of luck. Some more complex use cases like sharing structs among queries are also commonly not supported.

Cynic takes a different approach - it uses Rust structs to define queries and generates GraphQL from them. This gives you freedom to control the structs you'll be working with while still enjoying type safe queries, checked against the GraphQL schema. When its built in derives don't do exactly what you want it provides lower level APIs to hook in and fetch the data you want in the format you want to work with it.

Of course writing out all the structs to represent a large GraphQL query can be quite challenging, and GraphQL has excellent tooling for building queries usually. Cynic provides querygen to help with this - you write a GraphQL query using the existing GQL tooling and it'll generate some cynic structs to make that query. You can use this as a starting point for your projects - either adding on to the rust structs directly, or re-using querygen as appropriate.

Features

Cynic is currently a work in progress, but the following features are supported:

  • Typesafe queries & mutations.
  • Defining custom scalars.
  • Building dynamic (but still type checked) queries at run time.
  • Query arguments including input objects
  • Interfaces & union types
  • Introspection via cynic-cli or cynic-introspection
  • GraphQL Subscriptions via graphql-ws-client.

The following features are not currently supported, but may be one day.

  • Directives
  • Potentially other things (please open an issue if you find anything obviously missing)

Documentation

Cynic is documented in a few places:

  1. There's a guide to using cynic on cynic-rs.dev
  2. The reference documentation on docs.rs

Inspiration

  • graphql-client, the original Rust GraphQL client. This is a great library for using GraphQL from Rust. It wasn't quite what I wanted but it might be what you want.
  • The idea of encoding the GraphQL typesystem into a DSL was taken from elm-graphql.
  • Most of the JSON decoding APIs were taken from Json.Decode in Elm.
  • Deriving code from structs is a fairly common Rust pattern, though serde in particular provided inspiration for the derive APIs.

Dependencies

~5–20MB
~226K SLoC