#proc #macros #graphql #mapper #macro #builder #query #client #changelog #types

macro cynic-proc-macros

Procedural macro crate for cynic - a GraphQL query builder & data mapper for Rust

15 releases (breaking)

0.12.2 Feb 22, 2021
0.11.1 Jan 30, 2021
0.11.0 Dec 31, 2020
0.10.1 Nov 4, 2020
0.7.0 Jun 23, 2020

#410 in Rust patterns

Download history 84/week @ 2020-11-09 130/week @ 2020-11-16 95/week @ 2020-11-23 109/week @ 2020-11-30 115/week @ 2020-12-07 52/week @ 2020-12-14 45/week @ 2020-12-21 26/week @ 2020-12-28 116/week @ 2021-01-04 71/week @ 2021-01-11 132/week @ 2021-01-18 119/week @ 2021-01-25 114/week @ 2021-02-01 177/week @ 2021-02-08 312/week @ 2021-02-15 176/week @ 2021-02-22

485 downloads per month
Used in 2 crates (via cynic)

MPL-2.0 license

4.5K SLoC


A bring your own types GraphQL client for Rust

Crate Info API Docs Discord Chat

Documentation | Examples | Changelog


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 it's 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.


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

The following features are not well supported or tested and may not work well, or at all:

  • Fetching union and interface types via inline fragments

The following features are not yet supported, though should be soon (if you want to help out with the project I'd be happy for someone else to try and implement these - if you open an issue I'd be happy to give pointers on how to go about implementing any of them)

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


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


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


~51K SLoC