|
1 | | -use std::fmt; |
| 1 | +use std::{cmp::Ordering, collections::HashSet, fmt}; |
2 | 2 |
|
3 | | -use super::{directive::Directive, r#enum::Enum, input::InputType, scalar::Scalar, r#type::Type}; |
| 3 | +use super::{ |
| 4 | + directive::{ArgumentValue, Directive}, |
| 5 | + r#enum::Enum, |
| 6 | + input::InputType, |
| 7 | + scalar::Scalar, |
| 8 | + r#type::Type, |
| 9 | +}; |
4 | 10 |
|
5 | 11 | #[derive(Default)] |
6 | 12 | pub struct Schema<'a> { |
@@ -35,6 +41,93 @@ impl<'a> Schema<'a> { |
35 | 41 | pub fn push_scalar(&mut self, scalar: Scalar<'a>) { |
36 | 42 | self.scalars.push(scalar); |
37 | 43 | } |
| 44 | + |
| 45 | + /// Kind of a yolo version of unused types check. But hey, it works. If somebody has lots of time |
| 46 | + /// in their hands, would be great to write this with cynic ;) |
| 47 | + pub fn remove_unused_types(&mut self) { |
| 48 | + let mut used_types = HashSet::new(); |
| 49 | + |
| 50 | + // First pass: collect type references from regular types |
| 51 | + for r#type in &self.types { |
| 52 | + for field in &r#type.fields { |
| 53 | + let type_name = field.r#type.replace("[", "").replace("]", "").replace("!", ""); |
| 54 | + used_types.insert(type_name); |
| 55 | + |
| 56 | + // Process any arguments |
| 57 | + for argument in &field.arguments { |
| 58 | + track_argument_inputs(&mut used_types, &argument.value); |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + // Keep resolving dependencies until we reach a fixed point |
| 64 | + let mut size_before = 0; |
| 65 | + while size_before != used_types.len() { |
| 66 | + size_before = used_types.len(); |
| 67 | + |
| 68 | + // Process input types that are already marked as used |
| 69 | + for input in &self.input_types { |
| 70 | + if used_types.contains(input.name.as_ref()) { |
| 71 | + for field in &input.fields { |
| 72 | + let type_name = field.r#type.replace("[", "").replace("]", "").replace("!", ""); |
| 73 | + used_types.insert(type_name); |
| 74 | + |
| 75 | + // Process any arguments |
| 76 | + for argument in &field.arguments { |
| 77 | + track_argument_inputs(&mut used_types, &argument.value); |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + // Filter scalars to only keep those that are used |
| 85 | + let mut scalars = Vec::new(); |
| 86 | + for scalar in self.scalars.drain(..) { |
| 87 | + if used_types.contains(scalar.name) { |
| 88 | + scalars.push(scalar); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + self.scalars = scalars; |
| 93 | + |
| 94 | + // Filter input types to only keep those that are used |
| 95 | + let mut input_types = Vec::new(); |
| 96 | + for input in self.input_types.drain(..) { |
| 97 | + if used_types.contains(input.name.as_ref()) { |
| 98 | + input_types.push(input); |
| 99 | + } |
| 100 | + } |
| 101 | + self.input_types = input_types; |
| 102 | + |
| 103 | + self.scalars.sort_by_key(|scalar| scalar.name); |
| 104 | + self.enums.sort_by_key(|r#enum| r#enum.name); |
| 105 | + self.input_types.sort_by(|a, b| a.name.cmp(&b.name)); |
| 106 | + |
| 107 | + self.types.sort_by(|a, b| match (a.name.as_ref(), b.name.as_ref()) { |
| 108 | + ("Query", "Mutation") => Ordering::Less, |
| 109 | + ("Mutation", "Query") => Ordering::Greater, |
| 110 | + ("Query", _) => Ordering::Greater, |
| 111 | + ("Mutation", _) => Ordering::Greater, |
| 112 | + _ => a.name.cmp(&b.name), |
| 113 | + }); |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +fn track_argument_inputs<'a>(used_inputs: &mut HashSet<String>, value: &'a ArgumentValue<'a>) { |
| 118 | + match value { |
| 119 | + ArgumentValue::Constant(constant) => { |
| 120 | + let type_name = constant.replace("[", "").replace("]", "").replace("!", ""); |
| 121 | + used_inputs.insert(type_name); |
| 122 | + } |
| 123 | + ArgumentValue::Array(values) => values |
| 124 | + .iter() |
| 125 | + .for_each(|value| track_argument_inputs(used_inputs, value)), |
| 126 | + ArgumentValue::MultiLineArray { values, .. } => values |
| 127 | + .iter() |
| 128 | + .for_each(|value| track_argument_inputs(used_inputs, value)), |
| 129 | + _ => todo!(), |
| 130 | + } |
38 | 131 | } |
39 | 132 |
|
40 | 133 | impl fmt::Display for Schema<'_> { |
|
0 commit comments