From b38c2abbc94a7d58f0aa0263704cb4f4d030dba9 Mon Sep 17 00:00:00 2001 From: Graeme Coupar Date: Tue, 12 Sep 2023 22:23:47 +0100 Subject: [PATCH] Add a test of spreads (#776) --- .../spread__snapshot_test_query.snap | 19 +++++ cynic/tests/spread.rs | 69 +++++++++++++++++++ 2 files changed, 88 insertions(+) create mode 100644 cynic/tests/snapshots/spread__snapshot_test_query.snap create mode 100644 cynic/tests/spread.rs diff --git a/cynic/tests/snapshots/spread__snapshot_test_query.snap b/cynic/tests/snapshots/spread__snapshot_test_query.snap new file mode 100644 index 00000000..58ebc0b5 --- /dev/null +++ b/cynic/tests/snapshots/spread__snapshot_test_query.snap @@ -0,0 +1,19 @@ +--- +source: cynic/tests/spread.rs +expression: query.query +--- +query FilmDirectorQuery($id: ID) { + film(id: $id) { + id + __typename + ... { + title + director + } + ... { + releaseDate + } + } +} + + diff --git a/cynic/tests/spread.rs b/cynic/tests/spread.rs new file mode 100644 index 00000000..65d131a7 --- /dev/null +++ b/cynic/tests/spread.rs @@ -0,0 +1,69 @@ +#![allow(dead_code)] + +mod schema { + cynic::use_schema!("../schemas/starwars.schema.graphql"); +} + +#[derive(cynic::QueryFragment, Debug)] +#[cynic( + graphql_type = "Film", + schema_path = "../schemas/starwars.schema.graphql" +)] +struct FilmDetails { + title: Option, + director: Option, +} + +#[derive(cynic::QueryFragment, Debug)] +#[cynic( + graphql_type = "Film", + schema_path = "../schemas/starwars.schema.graphql" +)] +struct FilmMoreDetails { + release_date: Option, +} + +#[derive(cynic::QueryFragment, Debug)] +#[cynic(schema_path = "../schemas/starwars.schema.graphql")] +struct Film { + id: cynic::Id, + #[cynic(spread)] + details: FilmDetails, + #[cynic(spread)] + more_details: FilmMoreDetails, +} + +#[derive(cynic::QueryVariables)] +struct FilmArguments { + id: Option, +} + +#[derive(cynic::QueryFragment, Debug)] +#[cynic( + graphql_type = "Root", + variables = "FilmArguments", + schema_path = "../schemas/starwars.schema.graphql" +)] +struct FilmDirectorQuery { + #[arguments(id: $id)] + film: Option, +} + +fn build_query() -> cynic::Operation { + use cynic::QueryBuilder; + + FilmDirectorQuery::build(FilmArguments { + id: Some("ZmlsbXM6MQ==".into()), + }) +} + +#[test] +fn snapshot_test_query() { + // Running a snapshot test of the query building functionality as that gives us + // a place to copy and paste the actual GQL we're using for running elsewhere, + // and also helps ensure we don't change queries by mistake + + let query = build_query(); + + insta::assert_snapshot!(query.query); +}