#![deny(missing_docs)]
extern crate serde;
#[cfg(not(test))]
extern crate serde_json;
#[cfg(test)]
#[macro_use]
extern crate serde_json;
#[macro_use]
extern crate serde_derive;
#[cfg(test)]
#[macro_use]
extern crate maplit;
#[derive(Serialize, Deserialize, PartialEq, Debug)]
pub struct GraphQLConfiguration {
pub projects: Option<::std::collections::BTreeMap<String, GraphQLProjectConfiguration>>,
#[serde(flatten)]
pub root: GraphQLProjectConfiguration,
}
#[derive(Serialize, Deserialize, PartialEq, Debug)]
#[serde(rename_all = "camelCase")]
pub struct GraphQLProjectConfiguration {
pub name: Option<String>,
pub schema_path: Option<::std::path::PathBuf>,
pub includes: Option<Vec<String>>,
pub excludes: Option<Vec<String>>,
pub extensions: Option<::std::collections::BTreeMap<String, serde_json::Value>>,
}
#[cfg(test)]
mod tests {
use super::*;
fn test_deserialization(json: serde_json::Value, expected: GraphQLConfiguration) {
let deserialized = serde_json::from_value::<GraphQLConfiguration>(json).unwrap();
assert_eq!(deserialized, expected);
}
#[test]
fn it_works_with_schema_path() {
let config = json!({
"schemaPath": "./schema.graphql"
});
let expected = GraphQLConfiguration {
root: GraphQLProjectConfiguration {
name: None,
schema_path: Some("./schema.graphql".into()),
includes: None,
excludes: None,
extensions: None,
},
projects: None,
};
test_deserialization(config, expected);
}
#[test]
fn it_works_with_nested_project_configs() {
let config = json!({
"projects": {
"amazingLibrary": {
"schemaPath": "./amazingLibrary.schema.graphql"
}
}
});
let expected = GraphQLConfiguration {
root: GraphQLProjectConfiguration {
name: None,
schema_path: None,
includes: None,
excludes: None,
extensions: None,
},
projects: Some(btreemap!{
"amazingLibrary".to_owned() => GraphQLProjectConfiguration {
schema_path: Some("./amazingLibrary.schema.graphql".into()),
name: None,
includes: None,
excludes: None,
extensions: None,
}
}),
};
test_deserialization(config, expected);
}
#[test]
fn it_works_with_multiple_nested_project_configs() {
let config = json!({
"projects": {
"amazingLibrary": {
"schemaPath": "./amazingLibrary.schema.graphql"
},
"evenMoreAmazingLibrary": {
"schemaPath": "./evenMoreAmazingLibrary.schema.graphql"
}
}
});
let expected = GraphQLConfiguration {
root: GraphQLProjectConfiguration {
name: None,
schema_path: None,
includes: None,
excludes: None,
extensions: None,
},
projects: Some(btreemap!{
"amazingLibrary".to_owned() => GraphQLProjectConfiguration {
schema_path: Some("./amazingLibrary.schema.graphql".into()),
name: None,
includes: None,
excludes: None,
extensions: None,
},
"evenMoreAmazingLibrary".to_owned() => GraphQLProjectConfiguration {
schema_path: Some("./evenMoreAmazingLibrary.schema.graphql".into()),
name: None,
includes: None,
excludes: None,
extensions: None,
}
}),
};
test_deserialization(config, expected);
}
#[test]
fn it_works_with_multiple_nested_project_configs_and_a_root_config() {
let config = json!({
"schemaPath": "./greatRootLibrary.schema.graphql",
"projects": {
"amazingLibrary": {
"schemaPath": "./amazingLibrary.schema.graphql"
},
"evenMoreAmazingLibrary": {
"schemaPath": "./evenMoreAmazingLibrary.schema.graphql"
}
}
});
let expected = GraphQLConfiguration {
root: GraphQLProjectConfiguration {
name: None,
schema_path: Some("./greatRootLibrary.schema.graphql".into()),
includes: None,
excludes: None,
extensions: None,
},
projects: Some(btreemap!{
"amazingLibrary".to_owned() => GraphQLProjectConfiguration {
schema_path: Some("./amazingLibrary.schema.graphql".into()),
name: None,
includes: None,
excludes: None,
extensions: None,
},
"evenMoreAmazingLibrary".to_owned() => GraphQLProjectConfiguration {
schema_path: Some("./evenMoreAmazingLibrary.schema.graphql".into()),
name: None,
includes: None,
excludes: None,
extensions: None,
}
}),
};
test_deserialization(config, expected);
}
#[test]
fn it_works_with_extensions() {
let config = json!({
"extensions": {
"lastUpdatedAt": 1532367255884u64
}
});
let expected = GraphQLConfiguration {
root: GraphQLProjectConfiguration {
name: None,
schema_path: None,
includes: None,
excludes: None,
extensions: Some(
btreemap!{ "lastUpdatedAt".to_owned() => json!(1532367255884u64) },
),
},
projects: None,
};
test_deserialization(config, expected);
}
#[test]
fn it_works_with_excludes_and_includes() {
let config = json!({
"includes": ["./projectA/graphql/*.graphql"],
"excludes": ["./projectA/graphql/*.not_graphql"]
});
let expected = GraphQLConfiguration {
root: GraphQLProjectConfiguration {
name: None,
schema_path: None,
includes: Some(vec!["./projectA/graphql/*.graphql".to_owned()]),
excludes: Some(vec!["./projectA/graphql/*.not_graphql".to_owned()]),
extensions: None,
},
projects: None,
};
test_deserialization(config, expected);
}
}