|
| 1 | +extern crate failure; |
| 2 | +extern crate reqwest; |
| 3 | +#[macro_use] |
| 4 | +extern crate structopt; |
| 5 | +#[macro_use] |
| 6 | +extern crate graphql_client; |
| 7 | +#[macro_use] |
| 8 | +extern crate serde_derive; |
| 9 | +extern crate serde; |
| 10 | +extern crate serde_json; |
| 11 | + |
| 12 | +use std::path::PathBuf; |
| 13 | +use structopt::StructOpt; |
| 14 | + |
| 15 | +const INTROSPECTION_QUERY: &'static str = include_str!("introspection_query.graphql"); |
| 16 | + |
| 17 | +#[derive(GraphQLQuery)] |
| 18 | +#[GraphQLQuery( |
| 19 | + schema_path = "src/introspection_schema.graphql", query_path = "src/introspection_query.graphql" |
| 20 | +)] |
| 21 | +struct IntrospectionQuery; |
| 22 | + |
| 23 | +#[derive(StructOpt)] |
| 24 | +enum Cli { |
| 25 | + #[structopt(name = "introspect-schema")] |
| 26 | + IntrospectSchema { |
| 27 | + /// The URL of a GraphQL endpoint to introspect. |
| 28 | + schema_location: String, |
| 29 | + /// Where to write the JSON for the introspected schema. |
| 30 | + #[structopt(parse(from_os_str))] |
| 31 | + #[structopt(long = "output")] |
| 32 | + output: Option<PathBuf>, |
| 33 | + }, |
| 34 | + #[structopt(name = "generate")] |
| 35 | + Generate { |
| 36 | + // should be a glob |
| 37 | + paths: String, |
| 38 | + #[structopt(parse(from_os_str))] |
| 39 | + schema: PathBuf, |
| 40 | + #[structopt(parse(from_os_str))] |
| 41 | + output: PathBuf, |
| 42 | + }, |
| 43 | +} |
| 44 | + |
| 45 | +fn main() -> Result<(), failure::Error> { |
| 46 | + let cli = Cli::from_args(); |
| 47 | + match cli { |
| 48 | + Cli::IntrospectSchema { |
| 49 | + schema_location, |
| 50 | + output, |
| 51 | + } => introspect_schema(schema_location, output), |
| 52 | + Cli::Generate { |
| 53 | + paths: _, |
| 54 | + schema: _, |
| 55 | + output: _, |
| 56 | + } => unimplemented!(), |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +fn introspect_schema(location: String, output: Option<PathBuf>) -> Result<(), failure::Error> { |
| 61 | + use reqwest::header::*; |
| 62 | + use reqwest::mime; |
| 63 | + use std::io::Write; |
| 64 | + |
| 65 | + let mut out: Box<Write> = match output { |
| 66 | + Some(path) => Box::new(::std::fs::File::create(path)?), |
| 67 | + None => Box::new(::std::io::stdout()), |
| 68 | + }; |
| 69 | + |
| 70 | + let request_body: graphql_client::GraphQLQueryBody<()> = graphql_client::GraphQLQueryBody { |
| 71 | + variables: (), |
| 72 | + query: INTROSPECTION_QUERY, |
| 73 | + }; |
| 74 | + |
| 75 | + let client = reqwest::Client::new(); |
| 76 | + let mut res = client |
| 77 | + .post(&location) |
| 78 | + .header(Accept(vec![qitem(mime::APPLICATION_JSON)])) |
| 79 | + .json(&request_body) |
| 80 | + .send()?; |
| 81 | + |
| 82 | + if res.status().is_success() { |
| 83 | + } else if res.status().is_server_error() { |
| 84 | + println!("server error!"); |
| 85 | + } else { |
| 86 | + println!("Something else happened. Status: {:?}", res.status()); |
| 87 | + } |
| 88 | + |
| 89 | + let json: graphql_client::GraphQLResponse<introspection_query::ResponseData> = res.json()?; |
| 90 | + let json = serde_json::to_string(&json)?; |
| 91 | + |
| 92 | + write!(out, "{}", json)?; |
| 93 | + |
| 94 | + Ok(()) |
| 95 | +} |
0 commit comments