Skip to content

Commit 6a5cdfe

Browse files
committed
example: variable inlining (but with cynic)
1 parent 97b5ed5 commit 6a5cdfe

File tree

2 files changed

+86
-1
lines changed

2 files changed

+86
-1
lines changed

cynic/tests/variables.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use cynic::{Operation, OperationBuilder};
21
use serde_json::json;
32

43
#[derive(cynic::QueryVariables, cynic::QueryVariableLiterals)]
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
//! An example of inlining variables into a GraphQL operation prior to sending
2+
//!
3+
//! This example uses the starwars API but the use case is primarily to support
4+
//! shopifies [bulkOperationRunQuery][1] which requires a document with no variables.
5+
//!
6+
//! [1]: https://shopify.dev/docs/api/admin-graphql/2024-07/mutations/bulkoperationrunquery
7+
8+
use cynic::OperationBuilder;
9+
10+
// Pull in the Star Wars schema we registered in build.rs
11+
#[cynic::schema("starwars")]
12+
mod schema {}
13+
14+
#[derive(cynic::QueryFragment, Debug)]
15+
struct Film {
16+
title: Option<String>,
17+
director: Option<String>,
18+
}
19+
20+
#[derive(cynic::QueryVariables, cynic::QueryVariableLiterals)]
21+
struct FilmVariables {
22+
id: Option<cynic::Id>,
23+
}
24+
25+
#[derive(cynic::QueryFragment, Debug)]
26+
#[cynic(graphql_type = "Root", variables = "FilmVariables")]
27+
struct FilmDirectorQuery {
28+
#[arguments(id: $id)]
29+
film: Option<Film>,
30+
}
31+
32+
fn main() {
33+
match run_query().data {
34+
Some(FilmDirectorQuery { film: Some(film) }) => {
35+
println!("{:?} was directed by {:?}", film.title, film.director)
36+
}
37+
_ => {
38+
println!("No film found");
39+
}
40+
}
41+
}
42+
43+
fn run_query() -> cynic::GraphQlResponse<FilmDirectorQuery> {
44+
use cynic::http::ReqwestBlockingExt;
45+
46+
let query = build_query();
47+
48+
reqwest::blocking::Client::new()
49+
.post("https://swapi-graphql.netlify.app/.netlify/functions/index")
50+
.run_graphql(query)
51+
.unwrap()
52+
}
53+
54+
fn build_query() -> cynic::Operation<FilmDirectorQuery, ()> {
55+
OperationBuilder::query()
56+
.with_variables(FilmVariables {
57+
id: Some("ZmlsbXM6MQ==".into()),
58+
})
59+
.build_with_variables_inlined()
60+
.unwrap()
61+
}
62+
63+
#[cfg(test)]
64+
mod test {
65+
use super::*;
66+
67+
#[test]
68+
fn snapshot_test_query() {
69+
// Running a snapshot test of the query building functionality as that gives us
70+
// a place to copy and paste the actual GQL we're using for running elsewhere,
71+
// and also helps ensure we don't change queries by mistake
72+
73+
let query = build_query();
74+
75+
insta::assert_snapshot!(query.query);
76+
}
77+
78+
#[test]
79+
fn test_running_query() {
80+
let result = run_query();
81+
if result.errors.is_some() {
82+
assert_eq!(result.errors.unwrap().len(), 0);
83+
}
84+
insta::assert_debug_snapshot!(result.data);
85+
}
86+
}

0 commit comments

Comments
 (0)