obmarg / cynic

A bring your own types GraphQL client library for Rust
https://cynic-rs.dev
Mozilla Public License 2.0
374 stars 46 forks source link
gql graphql graphql-client graphql-library graphql-query macros rust

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:

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

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