Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add schema validation #29

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 103 additions & 1 deletion src/graphql_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,15 @@ use std::{fs, sync::LazyLock};
use anyhow::{anyhow, Result};
use diff_logger::DiffLogger;
use reqwest::Method;
use serde::{de::DeserializeOwned, Serialize};
use serde_json::Value;
use tracing::{error, info};

use crate::request::{MOCK_API_CLIENT, REFERENCE_GRAPHQL_CLIENT, TESTED_GRAPHQL_CLIENT};
use crate::{
query_info::Schema,
request::{MOCK_API_CLIENT, REFERENCE_GRAPHQL_CLIENT, TESTED_GRAPHQL_CLIENT},
type_info::Root,
};

use super::ROOT_DIR;

Expand Down Expand Up @@ -65,3 +71,99 @@ pub async fn run_graphql_tests() -> Result<()> {

Ok(())
}

fn query_builder(type_name: &str) -> String {
format!(
r#"
{{
__type(name: "{}") {{
name
kind
fields {{
name
args {{
name
}}
}}
}}
}}
"#,
type_name
)
}

fn compare<T: DeserializeOwned + Serialize + Clone>(
actual: Value,
expected: Value,
error_message: &str,
) -> Result<()> {
// in order to have sorting.
let actual: T = serde_json::from_value(actual)?;
let expected: T = serde_json::from_value(expected)?;

// with value we can compare with diff logger
let actual_value = serde_json::to_value(actual)?;
let expected_value = serde_json::to_value(expected)?;

let differ = DiffLogger::new();

let difference = differ.diff(&actual_value, &expected_value);
if !difference.is_empty() {
error!(error_message);
println!("{}", difference);
return Err(anyhow!(error_message.to_owned()));
}

Ok(())
}

pub async fn run_introspection_query() -> Result<()> {
info!("Run graphql introspection tests");
let query_info = include_str!("./query_info.graphql");

// check the root query is same or not.
let actual_value = TESTED_GRAPHQL_CLIENT.request(&query_info).await?;
let expected_value = REFERENCE_GRAPHQL_CLIENT.request(&query_info).await?;
let actual: Schema = serde_json::from_value(actual_value.clone())?;
let expected: Schema = serde_json::from_value(expected_value.clone())?;

let _ = compare::<Schema>(
actual_value,
expected_value,
"Query Operation type mismatch",
)?;

for (actual, expected) in actual
.data
.schema
.query_type
.fields
.iter()
.zip(expected.data.schema.query_type.fields.iter())
{
let actual_op_type = actual.field_type.get_name();
let expected_op_type = expected.field_type.get_name();

if actual_op_type.is_none() != expected_op_type.is_none() {
error!("Output type mismatch for field: {:?}", actual.name);
return Err(anyhow!("Output type mismatch for field: {:?}", actual.name));
}

let actual_op_type = actual_op_type.unwrap();
let expected_op_type = expected_op_type.unwrap();

let actual_op_type_query = query_builder(&actual_op_type);
let expected_op_type_query = query_builder(&expected_op_type);

let actual = TESTED_GRAPHQL_CLIENT.request(&actual_op_type_query).await?;
let expected = REFERENCE_GRAPHQL_CLIENT
.request(&expected_op_type_query)
.await?;

let _ = compare::<Root>(actual, expected, "Type Defination mismatch")?;
}

info!("Execution of graphql schema validation finished");

Ok(())
}
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,8 @@ mod graphql_tests;
pub mod project;
mod request;
mod utils;
mod introspection;
mod query_info;
mod type_info;

pub const ROOT_DIR: &str = env!("CARGO_MANIFEST_DIR");
4 changes: 3 additions & 1 deletion src/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use tracing::{error, info, instrument};
use crate::{
benchmarks::run_benchmarks,
command::{Command, CommandInstance},
graphql_tests::run_graphql_tests,
graphql_tests::{run_graphql_tests, run_introspection_query},
request::{REFERENCE_GRAPHQL_CLIENT, TESTED_GRAPHQL_CLIENT},
utils::env_default,
ROOT_DIR,
Expand Down Expand Up @@ -53,6 +53,8 @@ impl Project {
let reference_server = self.run_reference_server().await?;
let server = self.run_server().await?;

run_introspection_query().await?;

run_graphql_tests().await?;
run_benchmarks(&Path::new(ROOT_DIR).join(format!("results/{}", self.name()))).await?;
run_graphql_tests().await?;
Expand Down
41 changes: 41 additions & 0 deletions src/query_info.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
query {
__schema {
queryType {
fields {
name
type {
kind
name
ofType {
kind
name
ofType {
kind
name
ofType {
kind
name
}
}
}
}
args {
name
type {
kind
name
ofType {
kind
name
ofType {
kind
name
}
}
}
defaultValue
}
}
}
}
}
127 changes: 127 additions & 0 deletions src/query_info.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
use serde::{Deserialize, Serialize, Serializer, Deserializer};
use serde::ser::SerializeStruct;

#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
#[serde(rename_all = "camelCase")]
pub struct Schema {
pub data: SchemaData,
}

#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
pub struct SchemaData {
#[serde(rename = "__schema")]
pub schema: SchemaType,
}

#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
#[serde(rename_all = "camelCase")]
pub struct SchemaType {
pub query_type: QueryType,
}

#[derive(Debug, PartialEq, Clone)]
pub struct QueryType {
pub fields: Vec<Field>,
}

impl Serialize for QueryType {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let mut fields = self.fields.clone();
fields.sort_by(|a, b| a.name.cmp(&b.name));
let mut state = serializer.serialize_struct("queryType", 1)?;
state.serialize_field("fields", &fields)?;
state.end()
}
}

impl<'de> Deserialize<'de> for QueryType {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
#[derive(Deserialize)]
struct QueryTypeHelper {
fields: Vec<Field>,
}

let helper = QueryTypeHelper::deserialize(deserializer)?;
let mut fields = helper.fields;
fields.sort_by(|a, b| a.name.cmp(&b.name));
Ok(QueryType { fields })
}
}

#[derive(Debug, PartialEq, Clone)]
pub struct Field {
pub name: String,
pub field_type: FieldType,
pub args: Vec<Argument>,
}

impl Serialize for Field {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let mut args = self.args.clone();
args.sort_by(|a, b| a.name.cmp(&b.name));
let mut state = serializer.serialize_struct("field", 3)?;
state.serialize_field("name", &self.name)?;
state.serialize_field("type", &self.field_type)?;
state.serialize_field("args", &args)?;
state.end()
}
}

impl<'de> Deserialize<'de> for Field {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
#[derive(Deserialize)]
struct FieldHelper {
name: String,
#[serde(rename = "type")]
field_type: FieldType,
args: Vec<Argument>,
}

let helper = FieldHelper::deserialize(deserializer)?;
let mut args = helper.args;
args.sort_by(|a, b| a.name.cmp(&b.name));
Ok(Field {
name: helper.name,
field_type: helper.field_type,
args,
})
}
}

#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
pub struct Argument {
pub name: String,
#[serde(rename = "type")]
pub arg_type: FieldType,
pub default_value: Option<String>,
}

#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
#[serde(rename_all = "camelCase")]
pub struct FieldType {
pub kind: String,
pub name: Option<String>,
pub of_type: Option<Box<FieldType>>,
}


impl FieldType {
pub fn get_name(&self) -> Option<String> {
match &self.name {
Some(name) if !name.is_empty() => Some(name.clone()),
_ => self.of_type.as_ref().and_then(|t| t.get_name()),
}
}
}
Loading