Skip to content

Commit f0d6912

Browse files
committed
add minify support for directives and specify deprecated
1 parent 1ceb0f6 commit f0d6912

File tree

1 file changed

+41
-4
lines changed
  • crates/apollo-mcp-server/src/introspection

1 file changed

+41
-4
lines changed

crates/apollo-mcp-server/src/introspection/minify.rs

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use apollo_compiler::schema::{ExtendedType, Type};
22
use regex::Regex;
3-
use std::sync::OnceLock;
3+
use std::{collections::HashMap, sync::OnceLock};
44

55
pub trait MinifyExt {
66
/// Serialize in minified form
@@ -72,6 +72,34 @@ fn minify_input_object(input_object_type: &apollo_compiler::schema::InputObjectT
7272
format!("I:{type_name}:{fields}")
7373
}
7474

75+
// We should only minify directives that assist the LLM in understanding the schema. This included @deprecated
76+
fn minify_directives(directives: &apollo_compiler::ast::DirectiveList) -> String {
77+
let mut result = String::new();
78+
79+
static DIRECTIVES_TO_MINIFY: OnceLock<HashMap<&str, &str>> = OnceLock::new();
80+
let directives_to_minify =
81+
DIRECTIVES_TO_MINIFY.get_or_init(|| HashMap::from([("deprecated", "D")]));
82+
83+
for directive in directives.iter() {
84+
if let Some(minified_name) = directives_to_minify.get(directive.name.as_str()) {
85+
if !directive.arguments.is_empty() {
86+
// Since we're only handling @deprecated right now we can just add the reason and minify it
87+
let reason = directive
88+
.arguments
89+
.iter()
90+
.find(|a| a.name == "reason")
91+
.and_then(|a| a.value.as_str())
92+
.unwrap_or("No longer supported")
93+
.to_string();
94+
result.push_str(&format!("@{}(\"{}\")", minified_name, reason));
95+
} else {
96+
result.push_str(&format!("@{}", minified_name));
97+
}
98+
}
99+
}
100+
result
101+
}
102+
75103
fn minify_fields(
76104
fields: &apollo_compiler::collections::IndexMap<
77105
apollo_compiler::Name,
@@ -99,6 +127,8 @@ fn minify_fields(
99127
// Add field type
100128
result.push(':');
101129
result.push_str(&type_name(&field.ty));
130+
result.push_str(&minify_directives(&field.directives));
131+
102132
result.push(',');
103133
}
104134

@@ -128,6 +158,7 @@ fn minify_input_fields(
128158
result.push_str(field_name.as_str());
129159
result.push(':');
130160
result.push_str(&type_name(&field.ty));
161+
result.push_str(&minify_directives(&field.directives));
131162
result.push(',');
132163
}
133164

@@ -147,13 +178,19 @@ fn minify_arguments(
147178
.map(|arg| {
148179
if let Some(desc) = arg.description.as_ref() {
149180
format!(
150-
"\"{}\"{}:{}",
181+
"\"{}\"{}:{}{}",
151182
normalize_description(desc),
152183
arg.name.as_str(),
153-
type_name(&arg.ty)
184+
type_name(&arg.ty),
185+
minify_directives(&arg.directives)
154186
)
155187
} else {
156-
format!("{}:{}", arg.name.as_str(), type_name(&arg.ty))
188+
format!(
189+
"{}:{}{}",
190+
arg.name.as_str(),
191+
type_name(&arg.ty),
192+
minify_directives(&arg.directives)
193+
)
157194
}
158195
})
159196
.collect::<Vec<String>>()

0 commit comments

Comments
 (0)