1
1
use apollo_compiler:: schema:: { ExtendedType , Type } ;
2
2
use regex:: Regex ;
3
- use std:: sync:: OnceLock ;
3
+ use std:: { collections :: HashMap , sync:: OnceLock } ;
4
4
5
5
pub trait MinifyExt {
6
6
/// Serialize in minified form
@@ -72,6 +72,34 @@ fn minify_input_object(input_object_type: &apollo_compiler::schema::InputObjectT
72
72
format ! ( "I:{type_name}:{fields}" )
73
73
}
74
74
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
+
75
103
fn minify_fields (
76
104
fields : & apollo_compiler:: collections:: IndexMap <
77
105
apollo_compiler:: Name ,
@@ -99,6 +127,8 @@ fn minify_fields(
99
127
// Add field type
100
128
result. push ( ':' ) ;
101
129
result. push_str ( & type_name ( & field. ty ) ) ;
130
+ result. push_str ( & minify_directives ( & field. directives ) ) ;
131
+
102
132
result. push ( ',' ) ;
103
133
}
104
134
@@ -128,6 +158,7 @@ fn minify_input_fields(
128
158
result. push_str ( field_name. as_str ( ) ) ;
129
159
result. push ( ':' ) ;
130
160
result. push_str ( & type_name ( & field. ty ) ) ;
161
+ result. push_str ( & minify_directives ( & field. directives ) ) ;
131
162
result. push ( ',' ) ;
132
163
}
133
164
@@ -147,13 +178,19 @@ fn minify_arguments(
147
178
. map ( |arg| {
148
179
if let Some ( desc) = arg. description . as_ref ( ) {
149
180
format ! (
150
- "\" {}\" {}:{}" ,
181
+ "\" {}\" {}:{}{} " ,
151
182
normalize_description( desc) ,
152
183
arg. name. as_str( ) ,
153
- type_name( & arg. ty)
184
+ type_name( & arg. ty) ,
185
+ minify_directives( & arg. directives)
154
186
)
155
187
} 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
+ )
157
194
}
158
195
} )
159
196
. collect :: < Vec < String > > ( )
0 commit comments