|
| 1 | +use apollo_compiler::schema::{ExtendedType, Type}; |
| 2 | +use regex::Regex; |
| 3 | +use std::sync::OnceLock; |
| 4 | + |
| 5 | +pub trait MinifyExt { |
| 6 | + /// Serialize in minified form |
| 7 | + fn minify(&self) -> String; |
| 8 | +} |
| 9 | + |
| 10 | +impl MinifyExt for ExtendedType { |
| 11 | + fn minify(&self) -> String { |
| 12 | + match self { |
| 13 | + ExtendedType::Scalar(scalar_type) => minify_scalar(scalar_type), |
| 14 | + ExtendedType::Object(object_type) => minify_object(object_type), |
| 15 | + ExtendedType::Interface(interface_type) => minify_interface(interface_type), |
| 16 | + ExtendedType::Union(union_type) => minify_union(union_type), |
| 17 | + ExtendedType::Enum(enum_type) => minify_enum(enum_type), |
| 18 | + ExtendedType::InputObject(input_object_type) => minify_input_object(input_object_type), |
| 19 | + } |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +fn minify_scalar(scalar_type: &apollo_compiler::schema::ScalarType) -> String { |
| 24 | + shorten_scalar_names(scalar_type.name.as_str()).to_string() |
| 25 | +} |
| 26 | + |
| 27 | +fn minify_object(object_type: &apollo_compiler::schema::ObjectType) -> String { |
| 28 | + let fields = minify_fields(&object_type.fields); |
| 29 | + let type_name = format_type_name_with_description(&object_type.name, &object_type.description); |
| 30 | + let interfaces = format_interfaces(&object_type.implements_interfaces); |
| 31 | + |
| 32 | + if interfaces.is_empty() { |
| 33 | + format!("T:{type_name}:{fields}") |
| 34 | + } else { |
| 35 | + format!("T:{type_name}<{interfaces}>:{fields}") |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +fn minify_interface(interface_type: &apollo_compiler::schema::InterfaceType) -> String { |
| 40 | + let fields = minify_fields(&interface_type.fields); |
| 41 | + let type_name = |
| 42 | + format_type_name_with_description(&interface_type.name, &interface_type.description); |
| 43 | + format!("F:{type_name}:{fields}") |
| 44 | +} |
| 45 | + |
| 46 | +fn minify_union(union_type: &apollo_compiler::schema::UnionType) -> String { |
| 47 | + let member_types = union_type |
| 48 | + .members |
| 49 | + .iter() |
| 50 | + .map(|member| member.as_str()) |
| 51 | + .collect::<Vec<&str>>() |
| 52 | + .join(","); |
| 53 | + let type_name = format_type_name_with_description(&union_type.name, &union_type.description); |
| 54 | + format!("U:{type_name}:{member_types}") |
| 55 | +} |
| 56 | + |
| 57 | +fn minify_enum(enum_type: &apollo_compiler::schema::EnumType) -> String { |
| 58 | + let values = enum_type |
| 59 | + .values |
| 60 | + .keys() |
| 61 | + .map(|value| value.as_str()) |
| 62 | + .collect::<Vec<&str>>() |
| 63 | + .join(","); |
| 64 | + let type_name = format_type_name_with_description(&enum_type.name, &enum_type.description); |
| 65 | + format!("E:{type_name}:{values}") |
| 66 | +} |
| 67 | + |
| 68 | +fn minify_input_object(input_object_type: &apollo_compiler::schema::InputObjectType) -> String { |
| 69 | + let fields = minify_input_fields(&input_object_type.fields); |
| 70 | + let type_name = |
| 71 | + format_type_name_with_description(&input_object_type.name, &input_object_type.description); |
| 72 | + format!("I:{type_name}:{fields}") |
| 73 | +} |
| 74 | + |
| 75 | +fn minify_fields( |
| 76 | + fields: &apollo_compiler::collections::IndexMap< |
| 77 | + apollo_compiler::Name, |
| 78 | + apollo_compiler::schema::Component<apollo_compiler::ast::FieldDefinition>, |
| 79 | + >, |
| 80 | +) -> String { |
| 81 | + let mut result = String::new(); |
| 82 | + |
| 83 | + for (field_name, field) in fields.iter() { |
| 84 | + // Add description if present |
| 85 | + if let Some(desc) = field.description.as_ref() { |
| 86 | + result.push_str(&format!("\"{}\"", normalize_description(desc))); |
| 87 | + } |
| 88 | + |
| 89 | + // Add field name |
| 90 | + result.push_str(field_name.as_str()); |
| 91 | + |
| 92 | + // Add arguments if present |
| 93 | + if !field.arguments.is_empty() { |
| 94 | + result.push('('); |
| 95 | + result.push_str(&minify_arguments(&field.arguments)); |
| 96 | + result.push(')'); |
| 97 | + } |
| 98 | + |
| 99 | + // Add field type |
| 100 | + result.push(':'); |
| 101 | + result.push_str(&type_name(&field.ty)); |
| 102 | + result.push(','); |
| 103 | + } |
| 104 | + |
| 105 | + // Remove trailing comma |
| 106 | + if !result.is_empty() { |
| 107 | + result.pop(); |
| 108 | + } |
| 109 | + |
| 110 | + result |
| 111 | +} |
| 112 | + |
| 113 | +fn minify_input_fields( |
| 114 | + fields: &apollo_compiler::collections::IndexMap< |
| 115 | + apollo_compiler::Name, |
| 116 | + apollo_compiler::schema::Component<apollo_compiler::ast::InputValueDefinition>, |
| 117 | + >, |
| 118 | +) -> String { |
| 119 | + let mut result = String::new(); |
| 120 | + |
| 121 | + for (field_name, field) in fields.iter() { |
| 122 | + // Add description if present |
| 123 | + if let Some(desc) = field.description.as_ref() { |
| 124 | + result.push_str(&format!("\"{}\"", normalize_description(desc))); |
| 125 | + } |
| 126 | + |
| 127 | + // Add field name and type |
| 128 | + result.push_str(field_name.as_str()); |
| 129 | + result.push(':'); |
| 130 | + result.push_str(&type_name(&field.ty)); |
| 131 | + result.push(','); |
| 132 | + } |
| 133 | + |
| 134 | + // Remove trailing comma |
| 135 | + if !result.is_empty() { |
| 136 | + result.pop(); |
| 137 | + } |
| 138 | + |
| 139 | + result |
| 140 | +} |
| 141 | + |
| 142 | +fn minify_arguments( |
| 143 | + arguments: &[apollo_compiler::Node<apollo_compiler::ast::InputValueDefinition>], |
| 144 | +) -> String { |
| 145 | + arguments |
| 146 | + .iter() |
| 147 | + .map(|arg| { |
| 148 | + if let Some(desc) = arg.description.as_ref() { |
| 149 | + format!( |
| 150 | + "\"{}\"{}:{}", |
| 151 | + normalize_description(desc), |
| 152 | + arg.name.as_str(), |
| 153 | + type_name(&arg.ty) |
| 154 | + ) |
| 155 | + } else { |
| 156 | + format!("{}:{}", arg.name.as_str(), type_name(&arg.ty)) |
| 157 | + } |
| 158 | + }) |
| 159 | + .collect::<Vec<String>>() |
| 160 | + .join(",") |
| 161 | +} |
| 162 | + |
| 163 | +fn format_type_name_with_description( |
| 164 | + name: &apollo_compiler::Name, |
| 165 | + description: &Option<apollo_compiler::Node<str>>, |
| 166 | +) -> String { |
| 167 | + if let Some(desc) = description.as_ref() { |
| 168 | + format!("\"{}\"{}", normalize_description(desc), name) |
| 169 | + } else { |
| 170 | + name.to_string() |
| 171 | + } |
| 172 | +} |
| 173 | + |
| 174 | +fn format_interfaces( |
| 175 | + interfaces: &apollo_compiler::collections::IndexSet<apollo_compiler::schema::ComponentName>, |
| 176 | +) -> String { |
| 177 | + interfaces |
| 178 | + .iter() |
| 179 | + .map(|interface| interface.as_str()) |
| 180 | + .collect::<Vec<&str>>() |
| 181 | + .join(",") |
| 182 | +} |
| 183 | + |
| 184 | +fn type_name(ty: &Type) -> String { |
| 185 | + let name = shorten_scalar_names(ty.inner_named_type().as_str()); |
| 186 | + if ty.is_list() { |
| 187 | + format!("[{name}]") |
| 188 | + } else if ty.is_non_null() { |
| 189 | + format!("{name}!") |
| 190 | + } else { |
| 191 | + name.to_string() |
| 192 | + } |
| 193 | +} |
| 194 | + |
| 195 | +fn shorten_scalar_names(name: &str) -> &str { |
| 196 | + match name { |
| 197 | + "String" => "s", |
| 198 | + "Int" => "i", |
| 199 | + "Float" => "f", |
| 200 | + "Boolean" => "b", |
| 201 | + "ID" => "d", |
| 202 | + _ => name, |
| 203 | + } |
| 204 | +} |
| 205 | + |
| 206 | +/// Normalize description formatting |
| 207 | +#[allow(clippy::expect_used)] |
| 208 | +fn normalize_description(desc: &str) -> String { |
| 209 | + // LLMs can typically process descriptions just fine without whitespace |
| 210 | + static WHITESPACE_PATTERN: OnceLock<Regex> = OnceLock::new(); |
| 211 | + let re = WHITESPACE_PATTERN.get_or_init(|| Regex::new(r"\s+").expect("regex pattern compiles")); |
| 212 | + re.replace_all(desc, "").to_string() |
| 213 | +} |
0 commit comments