|
1 |
| -use field_type::FieldType; |
2 |
| -use std::collections::BTreeMap; |
3 |
| - |
4 |
| -#[derive(Debug)] |
5 |
| -pub struct StructFieldDescriptor { |
6 |
| - pub attributes: TokenStream, |
7 |
| - pub name: String, |
8 |
| - pub ty: FieldType, |
9 |
| -} |
| 1 | +use failure; |
| 2 | +use fragments::GqlFragment; |
| 3 | +use graphql_parser::query; |
| 4 | +use operations::Operation; |
| 5 | +use proc_macro2::TokenStream; |
| 6 | +use query::QueryContext; |
| 7 | +use schema; |
| 8 | +use selection::Selection; |
10 | 9 |
|
11 |
| -#[derive(Debug)] |
12 |
| -pub struct StructDescriptor { |
13 |
| - pub attributes: TokenStream, |
14 |
| - pub fields: Vec<StructFieldDescriptor>, |
15 |
| - pub name: String, |
16 |
| -} |
| 10 | +pub(crate) fn response_for_query( |
| 11 | + schema: schema::Schema, |
| 12 | + query: query::Document, |
| 13 | + selected_operation: String, |
| 14 | +) -> Result<TokenStream, failure::Error> { |
| 15 | + let mut context = QueryContext::new(schema); |
| 16 | + let mut definitions = Vec::new(); |
| 17 | + let mut operations: Vec<Operation> = Vec::new(); |
17 | 18 |
|
18 |
| -impl StructDescriptor { |
19 |
| - pub fn field_by_name(&self, name: &str) -> Option<StructFieldDescriptor> { |
20 |
| - unimplemented!() |
| 19 | + for definition in query.definitions { |
| 20 | + match definition { |
| 21 | + query::Definition::Operation(op) => { |
| 22 | + operations.push(op.into()); |
| 23 | + } |
| 24 | + query::Definition::Fragment(fragment) => { |
| 25 | + let query::TypeCondition::On(on) = fragment.type_condition; |
| 26 | + context.fragments.insert( |
| 27 | + fragment.name.clone(), |
| 28 | + GqlFragment { |
| 29 | + name: fragment.name, |
| 30 | + selection: Selection::from(&fragment.selection_set), |
| 31 | + on, |
| 32 | + }, |
| 33 | + ); |
| 34 | + } |
| 35 | + } |
21 | 36 | }
|
22 |
| -} |
23 | 37 |
|
24 |
| -pub struct EnumVariantDescriptor { |
25 |
| - pub attributes: TokenStream, |
26 |
| - pub name: String, |
27 |
| -} |
| 38 | + context.selected_operation = operations |
| 39 | + .iter() |
| 40 | + .find(|op| op.name == selected_operation) |
| 41 | + .map(|i| i.to_owned()); |
| 42 | + |
| 43 | + let operation = context.selected_operation.clone().unwrap_or_else(|| { |
| 44 | + operations |
| 45 | + .iter() |
| 46 | + .next() |
| 47 | + .map(|i| i.to_owned()) |
| 48 | + .expect("no operation in query document") |
| 49 | + }); |
| 50 | + |
| 51 | + let response_data_fields = { |
| 52 | + let root_name: String = operation |
| 53 | + .root_name(&context.schema) |
| 54 | + .expect("operation type not in schema"); |
| 55 | + let definition = context |
| 56 | + .schema |
| 57 | + .objects |
| 58 | + .get(&root_name) |
| 59 | + .expect("schema declaration is invalid"); |
| 60 | + let prefix = format!("RUST_{}", operation.name); |
| 61 | + let selection = &operation.selection; |
| 62 | + |
| 63 | + if operation.is_subscription() && selection.0.len() > 1 { |
| 64 | + Err(format_err!( |
| 65 | + "{}", |
| 66 | + ::constants::MULTIPLE_SUBSCRIPTION_FIELDS_ERROR |
| 67 | + ))? |
| 68 | + } |
| 69 | + |
| 70 | + definitions.extend( |
| 71 | + definition |
| 72 | + .field_impls_for_selection(&context, &selection, &prefix) |
| 73 | + .unwrap(), |
| 74 | + ); |
| 75 | + definition |
| 76 | + .response_fields_for_selection(&context, &selection, &prefix) |
| 77 | + .unwrap() |
| 78 | + }; |
| 79 | + |
| 80 | + let enum_definitions = context.schema.enums.values().map(|enm| enm.to_rust()); |
| 81 | + let fragment_definitions: Result<Vec<TokenStream>, _> = context |
| 82 | + .fragments |
| 83 | + .values() |
| 84 | + .map(|fragment| fragment.to_rust(&context)) |
| 85 | + .collect(); |
| 86 | + let fragment_definitions = fragment_definitions?; |
| 87 | + let variables_struct = operation.expand_variables(&context); |
| 88 | + |
| 89 | + let input_object_definitions: Result<Vec<TokenStream>, _> = context |
| 90 | + .schema |
| 91 | + .inputs |
| 92 | + .values() |
| 93 | + .map(|i| i.to_rust(&context)) |
| 94 | + .collect(); |
| 95 | + let input_object_definitions = input_object_definitions?; |
| 96 | + |
| 97 | + let scalar_definitions: Vec<TokenStream> = context |
| 98 | + .schema |
| 99 | + .scalars |
| 100 | + .values() |
| 101 | + .map(|s| s.to_rust()) |
| 102 | + .collect(); |
| 103 | + |
| 104 | + Ok(quote! { |
| 105 | + type Boolean = bool; |
| 106 | + type Float = f64; |
| 107 | + type Int = i64; |
| 108 | + type ID = String; |
| 109 | + |
| 110 | + #(#scalar_definitions)* |
| 111 | + |
| 112 | + #(#input_object_definitions)* |
| 113 | + |
| 114 | + #(#enum_definitions)* |
| 115 | + |
| 116 | + #(#fragment_definitions)* |
| 117 | + |
| 118 | + #(#definitions)* |
| 119 | + |
| 120 | + #variables_struct |
| 121 | + |
| 122 | + #[derive(Debug, Serialize, Deserialize)] |
| 123 | + #[serde(rename_all = "camelCase")] |
| 124 | + pub struct ResponseData { |
| 125 | + #(#response_data_fields,)* |
| 126 | + } |
28 | 127 |
|
29 |
| -#[derive(Debug)] |
30 |
| -pub struct EnumDescriptor { |
31 |
| - pub name: String, |
32 |
| - pub variants: Vec<String>, |
| 128 | + }) |
33 | 129 | }
|
0 commit comments