|
| 1 | +use failure; |
| 2 | +use graphql_parser; |
| 3 | +use introspection_response; |
| 4 | +use objects::GqlObjectField; |
| 5 | +use proc_macro2::{Ident, Span, TokenStream}; |
| 6 | +use query::QueryContext; |
| 7 | +use std::collections::HashMap; |
| 8 | + |
1 | 9 | #[derive(Debug, PartialEq)] |
2 | | -pub struct GqlInput; |
| 10 | +pub struct GqlInput { |
| 11 | + pub name: String, |
| 12 | + pub fields: HashMap<String, GqlObjectField>, |
| 13 | +} |
| 14 | + |
| 15 | +impl GqlInput { |
| 16 | + pub fn to_rust(&self, context: &QueryContext) -> Result<TokenStream, failure::Error> { |
| 17 | + let name = Ident::new(&self.name, Span::call_site()); |
| 18 | + let mut fields: Vec<&GqlObjectField> = self.fields.values().collect(); |
| 19 | + fields.sort_unstable_by(|a, b| a.name.cmp(&b.name)); |
| 20 | + let fields = fields.iter().map(|field| { |
| 21 | + let ty = field.type_.to_rust(&context, ""); |
| 22 | + let name = Ident::new(&field.name, Span::call_site()); |
| 23 | + quote!(pub #name: #ty) |
| 24 | + }); |
| 25 | + |
| 26 | + Ok(quote! { |
| 27 | + #[derive(Debug, Serialize)] |
| 28 | + #[serde(rename_all = "camelCase")] |
| 29 | + pub struct #name { |
| 30 | + #(#fields,)* |
| 31 | + } |
| 32 | + }) |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +impl ::std::convert::From<graphql_parser::schema::InputObjectType> for GqlInput { |
| 37 | + fn from(schema_input: graphql_parser::schema::InputObjectType) -> GqlInput { |
| 38 | + GqlInput { |
| 39 | + name: schema_input.name, |
| 40 | + fields: schema_input |
| 41 | + .fields |
| 42 | + .into_iter() |
| 43 | + .map(|field| { |
| 44 | + let name = field.name.clone(); |
| 45 | + let field = GqlObjectField { |
| 46 | + name: field.name, |
| 47 | + type_: field.value_type.into(), |
| 48 | + }; |
| 49 | + (name, field) |
| 50 | + }) |
| 51 | + .collect(), |
| 52 | + } |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +impl ::std::convert::From<introspection_response::FullType> for GqlInput { |
| 57 | + fn from(schema_input: introspection_response::FullType) -> GqlInput { |
| 58 | + GqlInput { |
| 59 | + name: schema_input.name.expect("unnamed input object"), |
| 60 | + fields: schema_input |
| 61 | + .input_fields |
| 62 | + .expect("fields on input object") |
| 63 | + .into_iter() |
| 64 | + .filter_map(|a| a) |
| 65 | + .map(|f| { |
| 66 | + let name = f.input_value.name.expect("unnamed input object field"); |
| 67 | + let field = GqlObjectField { |
| 68 | + name: name.clone(), |
| 69 | + type_: f |
| 70 | + .input_value |
| 71 | + .type_ |
| 72 | + .expect("type on input object field") |
| 73 | + .into(), |
| 74 | + }; |
| 75 | + (name, field) |
| 76 | + }) |
| 77 | + .collect(), |
| 78 | + } |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +#[cfg(test)] |
| 83 | +mod tests { |
| 84 | + use super::*; |
| 85 | + use constants::*; |
| 86 | + use field_type::FieldType; |
| 87 | + |
| 88 | + #[test] |
| 89 | + fn gql_input_to_rust() { |
| 90 | + let cat = GqlInput { |
| 91 | + name: "Cat".to_string(), |
| 92 | + fields: vec![ |
| 93 | + ( |
| 94 | + "pawsCount".to_string(), |
| 95 | + GqlObjectField { |
| 96 | + name: "pawsCount".to_string(), |
| 97 | + type_: FieldType::Named(float_type()), |
| 98 | + }, |
| 99 | + ), |
| 100 | + ( |
| 101 | + "offsprings".to_string(), |
| 102 | + GqlObjectField { |
| 103 | + name: "offsprings".to_string(), |
| 104 | + type_: FieldType::Vector(Box::new(FieldType::Named(Ident::new( |
| 105 | + "Cat", |
| 106 | + Span::call_site(), |
| 107 | + )))), |
| 108 | + }, |
| 109 | + ), |
| 110 | + ( |
| 111 | + "requirements".to_string(), |
| 112 | + GqlObjectField { |
| 113 | + name: "requirements".to_string(), |
| 114 | + type_: FieldType::Optional(Box::new(FieldType::Named(Ident::new( |
| 115 | + "CatRequirements", |
| 116 | + Span::call_site(), |
| 117 | + )))), |
| 118 | + }, |
| 119 | + ), |
| 120 | + ].into_iter() |
| 121 | + .collect(), |
| 122 | + }; |
| 123 | + |
| 124 | + let expected: String = vec![ |
| 125 | + "# [ derive ( Debug , Serialize ) ] ", |
| 126 | + "# [ serde ( rename_all = \"camelCase\" ) ] ", |
| 127 | + "pub struct Cat { ", |
| 128 | + "pub offsprings : Vec < Cat > , ", |
| 129 | + "pub pawsCount : Float , ", |
| 130 | + "pub requirements : Option < CatRequirements > , ", |
| 131 | + "}", |
| 132 | + ].into_iter() |
| 133 | + .collect(); |
| 134 | + |
| 135 | + let mut context = QueryContext::new_empty(); |
| 136 | + context.schema.inputs.insert(cat.name.clone(), cat); |
| 137 | + |
| 138 | + assert_eq!( |
| 139 | + format!( |
| 140 | + "{}", |
| 141 | + context |
| 142 | + .schema |
| 143 | + .inputs |
| 144 | + .get("Cat") |
| 145 | + .unwrap() |
| 146 | + .to_rust(&context) |
| 147 | + .unwrap() |
| 148 | + ), |
| 149 | + expected |
| 150 | + ); |
| 151 | + } |
| 152 | +} |
0 commit comments