-
Notifications
You must be signed in to change notification settings - Fork 94
/
Copy pathfetch.rs
92 lines (81 loc) · 3.03 KB
/
fetch.rs
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
use crate::blocking::StudioClient;
use crate::RoverClientError;
use graphql_client::*;
// I'm not sure where this should live long-term
/// this is because of the custom GraphQLDocument scalar in the schema
type GraphQLDocument = String;
#[derive(GraphQLQuery)]
// The paths are relative to the directory where your `Cargo.toml` is located.
// Both json and the GraphQL schema language are supported as sources for the schema
#[graphql(
query_path = "src/query/graph/fetch.graphql",
schema_path = ".schema/schema.graphql",
response_derives = "PartialEq, Debug, Serialize, Deserialize",
deprecated = "warn"
)]
/// This struct is used to generate the module containing `Variables` and
/// `ResponseData` structs.
/// Snake case of this name is the mod name. i.e. fetch_schema_query
pub struct FetchSchemaQuery;
/// The main function to be used from this module. This function fetches a
/// schema from apollo studio and returns it in either sdl (default) or json format
pub fn run(
variables: fetch_schema_query::Variables,
client: &StudioClient,
) -> Result<String, RoverClientError> {
let response_data = client.post::<FetchSchemaQuery>(variables)?;
get_schema_from_response_data(response_data)
// if we want json, we can parse & serialize it here
}
fn get_schema_from_response_data(
response_data: fetch_schema_query::ResponseData,
) -> Result<String, RoverClientError> {
let service_data = match response_data.service {
Some(data) => Ok(data),
None => Err(RoverClientError::NoService),
}?;
if let Some(schema) = service_data.schema {
Ok(schema.document)
} else {
Err(RoverClientError::HandleResponse {
msg: "No schema found for this variant".to_string(),
})
}
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
#[test]
fn get_schema_from_response_data_works() {
let json_response = json!({
"service": {
"schema": {
"document": "type Query { hello: String }"
}
}
});
let data: fetch_schema_query::ResponseData = serde_json::from_value(json_response).unwrap();
let output = get_schema_from_response_data(data);
assert!(output.is_ok());
assert_eq!(output.unwrap(), "type Query { hello: String }".to_string());
}
#[test]
fn get_schema_from_response_data_errs_on_no_service() {
let json_response = json!({ "service": null });
let data: fetch_schema_query::ResponseData = serde_json::from_value(json_response).unwrap();
let output = get_schema_from_response_data(data);
assert!(output.is_err());
}
#[test]
fn get_schema_from_response_data_errs_on_no_schema() {
let json_response = json!({
"service": {
"schema": null
}
});
let data: fetch_schema_query::ResponseData = serde_json::from_value(json_response).unwrap();
let output = get_schema_from_response_data(data);
assert!(output.is_err());
}
}