1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
//! This crate provides deserialize for GraphQL configs following the [graphql-config](https://github.com/prismagraphql/graphql-config/blob/master/specification.md) specification.
//!
//! `GraphQLConfiguration` is the type of the whole JSON document. It contains the top-level configuration (which serializes in the `root` field) and also optionally project-specific configuration in the `projects` field. The shapes of the top-level configuration and project-specific configurations are exactly the same.
//!
//! This library does not support [experimental configuration options](https://github.com/prismagraphql/graphql-config/blob/master/specification.md#experimental-configuration-options) yet.
//!
//! Currently, this library follows the spec as per [version 2.0.1 of the graphql-config specification](https://github.com/prismagraphql/graphql-config/tree/v2.0.1).
//!
//! ## Example
//!
//! ```
//! # extern crate serde;
//! # extern crate graphql_config;
//! # #[macro_use]
//! # extern crate serde_json;
//! # #[macro_use]
//! # extern crate maplit;
//! # use graphql_config::*;
//! # use std::io;
//! # fn main() -> io::Result<()> {
//! let config = json!({
//!     "schemaPath": "./schema.graphql",
//!     "includes": ["./graphql/*.graphql"],
//!     "projects": {                 
//!         "amazingLibrary": {
//!             "schemaPath": "./amazingLibrary.schema.graphql"
//!         }
//!     }
//! });
//!
//! let expected = GraphQLConfiguration {
//!     root: GraphQLProjectConfiguration {
//!         name: None,
//!         schema_path: Some("./schema.graphql".into()),
//!         includes: Some(vec!["./graphql/*.graphql".to_owned()]),
//!         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,
//!         },
//!     }),
//! };
//!
//! let deserialized = serde_json::from_value::<GraphQLConfiguration>(config)?;
//!
//! assert_eq!(deserialized, expected);
//! # Ok(())
//! # }
//! ```

#![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;

/// `GraphQLConfiguration` is the type of the whole JSON document. It contains
/// the top-level configuration (which serializes in the `root` field) and also
/// optionally project-specific configuration in the `projects` field. The shapes
/// of the top-level configuration and project-specific configurations are exactly
/// the same.
#[derive(Serialize, Deserialize, PartialEq, Debug)]
pub struct GraphQLConfiguration {
    /// A `BTreeMap` of project names as strings to `GraphQLProjectConfiguration`.
    /// Names of projects are not snake-cased during deserialization.
    pub projects: Option<::std::collections::BTreeMap<String, GraphQLProjectConfiguration>>,
    /// Top-level configuration goes into `root`.
    #[serde(flatten)]
    pub root: GraphQLProjectConfiguration,
}

/// The top-level configuration and project-specific
/// configurations share this shape.
#[derive(Serialize, Deserialize, PartialEq, Debug)]
#[serde(rename_all = "camelCase")]
pub struct GraphQLProjectConfiguration {
    /// The name of the project. The specification says this should default to
    /// the key of the project object if absent, this this not enforced.
    pub name: Option<String>,
    /// A file with schema IDL.
    pub schema_path: Option<::std::path::PathBuf>,
    /// For multiple applications with overlapping files,
    /// these configuration options may be helpful.
    pub includes: Option<Vec<String>>,
    /// For multiple applications with overlapping files,
    /// these configuration options may be helpful.
    pub excludes: Option<Vec<String>>,
    /// If you'd like to specify any other configurations,
    /// graphql-config provides a reserved namespace for it.
    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);
    }
}