Skip to content

Commit be9d712

Browse files
authored
Merge pull request #1002 from obmarg/source
Add cynic, a rust client library
2 parents 714aa84 + 9199a7e commit be9d712

File tree

1 file changed

+61
-0
lines changed
  • src/content/code/language-support/rust/client

1 file changed

+61
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
---
2+
name: cynic
3+
description: A bring your own types GraphQL client for Rust
4+
url: https://cynic-rs.dev
5+
github: obmarg/cynic
6+
---
7+
8+
A client library for rust that generates queries from types you provide,
9+
verifying that the types match the shape of your schema.
10+
11+
It provides [a generator](https://generator.cynic-rs.dev) to bootstrap types
12+
from existing GraphQL queries.
13+
14+
Usage example:
15+
16+
```rust
17+
#[derive(cynic::QueryFragment, Debug)]
18+
#[cynic(
19+
schema_path = "../schemas/starwars.schema.graphql",
20+
query_module = "query_dsl",
21+
graphql_type = "Root",
22+
argument_struct = "FilmArguments"
23+
)]
24+
struct FilmDirectorQuery {
25+
#[arguments(id = &args.id)]
26+
film: Option<Film>,
27+
}
28+
29+
#[derive(cynic::QueryFragment, Debug)]
30+
#[cynic(
31+
schema_path = "../schemas/starwars.schema.graphql",
32+
query_module = "query_dsl",
33+
graphql_type = "Film"
34+
)]
35+
struct Film {
36+
title: Option<String>,
37+
director: Option<String>,
38+
}
39+
40+
#[derive(cynic::FragmentArguments)]
41+
struct FilmArguments {
42+
id: Option<cynic::Id>,
43+
}
44+
45+
fn main() {
46+
use cynic::{QueryBuilder, http::ReqwestBlockingExt};
47+
48+
let query = FilmDirectorQuery::build(&FilmArguments {
49+
id: Some("ZmlsbXM6MQ==".into()),
50+
})
51+
52+
reqwest::blocking::Client::new()
53+
.post("https://swapi-graphql.netlify.com/.netlify/functions/index")
54+
.run_graphql(query)
55+
.unwrap()
56+
}
57+
58+
mod query_dsl {
59+
cynic::query_dsl!("../schemas/starwars.schema.graphql");
60+
}
61+
```

0 commit comments

Comments
 (0)