Skip to content

Commit

Permalink
chore: remove reliance on hosted swapi
Browse files Browse the repository at this point in the history
The hosted swapi GraphQL endpoint doesn't seem to be working at the
moment, and it's causing all the tests to fail.  Not ideal.

This adds a graphql-mocks crate (ported & simplified from an
implementation in the grafbase repo - probably not the cleanest since I
did the port quick n dirty, but it works) and updates the tests to use
that
  • Loading branch information
obmarg committed Dec 31, 2024
1 parent 448904c commit e3267f7
Show file tree
Hide file tree
Showing 30 changed files with 6,751 additions and 5,417 deletions.
655 changes: 534 additions & 121 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ members = [
"cynic-parser/ast-generator",
"cynic-parser-deser",
"cynic-parser-deser-macros",
"graphql-mocks",
]
exclude = ["cynic-parser/parser-generator"]
resolver = "2"
Expand Down Expand Up @@ -42,6 +43,7 @@ rust-version = "1.76"
cynic-parser = { path = "cynic-parser", version = "0.8.7" }
cynic-parser-deser-macros = { path = "cynic-parser-deser-macros", version = "0.8.7" }
darling = "0.20"
graphql-mocks.path = "graphql-mocks"
rstest = "0.23"
syn = "2"

Expand Down
2 changes: 2 additions & 0 deletions cynic-introspection/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ version = "3"

[dev-dependencies]
assert_matches = "1.4"
graphql-mocks.workspace = true
insta = "1.4"
maplit = "1.0.2"
tokio = { version = "1", features = ["macros"] }
reqwest = "0.12"
serde_json = "1"

Expand Down
13 changes: 10 additions & 3 deletions cynic-introspection/src/detection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,25 @@ use super::query::schema;
/// server has implemented support for.
///
/// ```rust
/// use cynic::{QueryBuilder, http::ReqwestBlockingExt};
/// use cynic::{QueryBuilder, http::ReqwestExt};
/// use cynic_introspection::CapabilitiesQuery;
/// # #[tokio::main]
/// # async fn main() {
/// # let server = graphql_mocks::mocks::swapi::serve().await;
/// # let url = server.url();
/// # let url = url.as_ref();
///
/// let data = reqwest::blocking::Client::new()
/// .post("https://swapi-graphql.netlify.app/.netlify/functions/index")
/// let data = reqwest::Client::new()
/// .post(url)
/// .run_graphql(CapabilitiesQuery::build(()))
/// .await
/// .unwrap()
/// .data
/// .unwrap();
///
/// let capabilities = data.capabilities();
/// println!("This server supports {capabilities:?}");
/// # }
/// ```
pub struct CapabilitiesQuery {
#[cynic(rename = "__type")]
Expand Down
32 changes: 23 additions & 9 deletions cynic-introspection/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,19 @@
//! results directly.
//!
//! ```rust
//! use cynic::{QueryBuilder, http::ReqwestBlockingExt};
//! use cynic::{QueryBuilder, http::ReqwestExt};
//! use cynic_introspection::IntrospectionQuery;
//! # #[tokio::main]
//! # async fn main() {
//! # let server = graphql_mocks::mocks::swapi::serve().await;
//! # let url = server.url();
//! # let url = url.as_ref();
//!
//! // We can run an introspection query and unwrap the data contained within
//! let introspection_data = reqwest::blocking::Client::new()
//! .post("https://swapi-graphql.netlify.app/.netlify/functions/index")
//! let introspection_data = reqwest::Client::new()
//! .post(url)
//! .run_graphql(IntrospectionQuery::build(()))
//! .await
//! .unwrap()
//! .data
//! .unwrap();
Expand All @@ -25,6 +31,7 @@
//! let schema = introspection_data.into_schema().unwrap();
//!
//! assert_eq!(schema.query_type, "Root");
//! # }
//! ```
//!
//! ### GraphQL Versions
Expand Down Expand Up @@ -68,23 +75,29 @@
//! `Introspection::with_capabilities`:
//!
//! ```rust
//!
//! use cynic::{QueryBuilder, http::ReqwestBlockingExt};
//! use cynic::{QueryBuilder, http::ReqwestExt};
//! use cynic_introspection::{CapabilitiesQuery, IntrospectionQuery};
//! # #[tokio::main]
//! # async fn main() {
//! # let server = graphql_mocks::mocks::swapi::serve().await;
//! # let url = server.url();
//! # let url = url.as_ref();
//!
//! // First we run a capabilites query to check what the server supports
//! let capabilities = reqwest::blocking::Client::new()
//! .post("https://swapi-graphql.netlify.app/.netlify/functions/index")
//! let capabilities = reqwest::Client::new()
//! .post(url)
//! .run_graphql(CapabilitiesQuery::build(()))
//! .await
//! .unwrap()
//! .data
//! .unwrap()
//! .capabilities();
//!
//! // Now we can safely run introspection, only querying for what the server supports.
//! let introspection_data = reqwest::blocking::Client::new()
//! .post("https://swapi-graphql.netlify.app/.netlify/functions/index")
//! let introspection_data = reqwest::Client::new()
//! .post(url)
//! .run_graphql(IntrospectionQuery::with_capabilities(capabilities))
//! .await
//! .unwrap()
//! .data
//! .unwrap();
Expand All @@ -93,6 +106,7 @@
//! let schema = introspection_data.into_schema().unwrap();
//!
//! assert_eq!(schema.query_type, "Root");
//! # }
//! ```
//!
//! [1]: http://spec.graphql.org/October2021/#sec-Introspection
Expand Down
13 changes: 8 additions & 5 deletions cynic-introspection/tests/sdl_tests.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
use cynic::http::ReqwestExt;
use cynic_introspection::{IntrospectionQuery, SpecificationVersion};
use graphql_mocks::mocks;

#[test]
fn test_starwars_sdl_conversion() {
use cynic::http::ReqwestBlockingExt;
#[tokio::test]
async fn test_starwars_sdl_conversion() {
let mock_server = mocks::swapi::serve().await;

let query =
IntrospectionQuery::with_capabilities(SpecificationVersion::June2018.capabilities());

let result = reqwest::blocking::Client::new()
.post("https://swapi-graphql.netlify.app/.netlify/functions/index")
let result = reqwest::Client::new()
.post(mock_server.url())
.run_graphql(query)
.await
.unwrap();

if result.errors.is_some() {
Expand Down
Loading

0 comments on commit e3267f7

Please sign in to comment.