Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commita290ff5

Browse files
committed
Allow exporting SDL with spaces
1 parent75a9d14 commita290ff5

File tree

3 files changed

+135
-23
lines changed

3 files changed

+135
-23
lines changed

‎src/registry/export_sdl.rs

Lines changed: 70 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const SYSTEM_SCALARS: &[&str] = &["Int", "Float", "String", "Boolean", "ID"];
66
constFEDERATION_SCALARS:&[&str] =&["Any"];
77

88
/// Options for SDL export
9-
#[derive(Debug,Copy,Clone,Default)]
9+
#[derive(Debug,Copy,Clone)]
1010
pubstructSDLExportOptions{
1111
sorted_fields:bool,
1212
sorted_arguments:bool,
@@ -15,6 +15,24 @@ pub struct SDLExportOptions {
1515
prefer_single_line_descriptions:bool,
1616
include_specified_by:bool,
1717
compose_directive:bool,
18+
use_space_ident:bool,
19+
indent_width:u8,
20+
}
21+
22+
implDefaultforSDLExportOptions{
23+
fndefault() ->Self{
24+
Self{
25+
sorted_fields:false,
26+
sorted_arguments:false,
27+
sorted_enum_values:false,
28+
federation:false,
29+
prefer_single_line_descriptions:false,
30+
include_specified_by:false,
31+
compose_directive:false,
32+
use_space_ident:false,
33+
indent_width:2,
34+
}
35+
}
1836
}
1937

2038
implSDLExportOptions{
@@ -89,6 +107,23 @@ impl SDLExportOptions {
89107
..self
90108
}
91109
}
110+
111+
/// Use spaces for indentation instead of tabs
112+
pubfnuse_space_ident(self) ->Self{
113+
Self{
114+
use_space_ident:true,
115+
..self
116+
}
117+
}
118+
119+
/// Set the number of spaces to use for each indentation level (default: 2).
120+
/// Only applies when `use_space_indent` is true
121+
pubfnindent_width(self,width:u8) ->Self{
122+
Self{
123+
indent_width: width,
124+
..self
125+
}
126+
}
92127
}
93128

94129
implRegistry{
@@ -108,7 +143,6 @@ impl Registry {
108143
}
109144

110145
self.export_type(ty,&mut sdl,&options);
111-
writeln!(sdl).ok();
112146
}
113147

114148
self.directives.values().for_each(|directive|{
@@ -157,8 +191,13 @@ impl Registry {
157191

158192
if options.federation{
159193
writeln!(sdl,"extend schema @link(").ok();
160-
writeln!(sdl,"\turl:\"https://specs.apollo.dev/federation/v2.3\",").ok();
161-
writeln!(sdl,"\timport: [\"@key\",\"@tag\",\"@shareable\",\"@inaccessible\",\"@override\",\"@external\",\"@provides\",\"@requires\",\"@composeDirective\",\"@interfaceObject\"]").ok();
194+
writeln!(
195+
sdl,
196+
"{}url:\"https://specs.apollo.dev/federation/v2.3\",",
197+
tab(&options)
198+
)
199+
.ok();
200+
writeln!(sdl,"{}import: [\"@key\",\"@tag\",\"@shareable\",\"@inaccessible\",\"@override\",\"@external\",\"@provides\",\"@requires\",\"@composeDirective\",\"@interfaceObject\"]", tab(&options)).ok();
162201
writeln!(sdl,")").ok();
163202

164203
if options.compose_directive{
@@ -176,23 +215,23 @@ impl Registry {
176215
});
177216
for(url, directives)in compose_directives{
178217
writeln!(sdl,"extend schema @link(").ok();
179-
writeln!(sdl,"\turl:\"{}\"", url).ok();
180-
writeln!(sdl,"\timport: [{}]", directives.join(",")).ok();
218+
writeln!(sdl,"{}url:\"{}\"", tab(&options), url).ok();
219+
writeln!(sdl,"{}import: [{}]", tab(&options), directives.join(",")).ok();
181220
writeln!(sdl,")").ok();
182221
for namein directives{
183-
writeln!(sdl,"\t@composeDirective(name: {})", name).ok();
222+
writeln!(sdl,"{}@composeDirective(name: {})", tab(&options), name).ok();
184223
}
185224
writeln!(sdl).ok();
186225
}
187226
}
188227
}else{
189228
writeln!(sdl,"schema {{").ok();
190-
writeln!(sdl,"\tquery: {}",self.query_type).ok();
229+
writeln!(sdl,"{}query: {}", tab(&options),self.query_type).ok();
191230
ifletSome(mutation_type) =self.mutation_type.as_deref(){
192-
writeln!(sdl,"\tmutation: {}", mutation_type).ok();
231+
writeln!(sdl,"{}mutation: {}", tab(&options), mutation_type).ok();
193232
}
194233
ifletSome(subscription_type) =self.subscription_type.as_deref(){
195-
writeln!(sdl,"\tsubscription: {}", subscription_type).ok();
234+
writeln!(sdl,"{}subscription: {}", tab(&options), subscription_type).ok();
196235
}
197236
writeln!(sdl,"}}").ok();
198237
}
@@ -223,7 +262,7 @@ impl Registry {
223262
}
224263

225264
if !field.args.is_empty(){
226-
write!(sdl,"\t{}(", field.name).ok();
265+
write!(sdl,"{}{}(", tab(&options), field.name).ok();
227266

228267
letmut args = field.args.values().collect::<Vec<_>>();
229268
if options.sorted_arguments{
@@ -243,7 +282,7 @@ impl Registry {
243282
}
244283

245284
if need_multiline{
246-
write!(sdl,"\t\t").ok();
285+
write!(sdl,"{0}{0}", tab(options)).ok();
247286
}elseif i !=0{
248287
sdl.push(' ');
249288
}
@@ -266,11 +305,11 @@ impl Registry {
266305
}
267306

268307
if need_multiline{
269-
sdl.push_str("\n\t");
308+
write!(sdl,"\n{}", tab(&options)).ok();
270309
}
271310
write!(sdl,"): {}", field.ty).ok();
272311
}else{
273-
write!(sdl,"\t{}: {}", field.name, field.ty).ok();
312+
write!(sdl,"{}{}: {}", tab(&options), field.name, field.ty).ok();
274313
}
275314

276315
write_deprecated(sdl,&field.deprecation);
@@ -352,7 +391,7 @@ impl Registry {
352391
write!(sdl," {}", directive.sdl()).ok();
353392
}
354393

355-
writeln!(sdl).ok();
394+
writeln!(sdl,"\n").ok();
356395
}
357396
}
358397
MetaType::Object{
@@ -437,7 +476,7 @@ impl Registry {
437476

438477
writeln!(sdl," {{").ok();
439478
Self::export_fields(sdl, fields.values(), options);
440-
writeln!(sdl,"}}").ok();
479+
writeln!(sdl,"}}\n").ok();
441480
}
442481
MetaType::Interface{
443482
name,
@@ -482,7 +521,7 @@ impl Registry {
482521

483522
writeln!(sdl," {{").ok();
484523
Self::export_fields(sdl, fields.values(), options);
485-
writeln!(sdl,"}}").ok();
524+
writeln!(sdl,"}}\n").ok();
486525
}
487526
MetaType::Enum{
488527
name,
@@ -522,7 +561,7 @@ impl Registry {
522561
ifletSome(description) =&value.description{
523562
write_description(sdl, options,1, description);
524563
}
525-
write!(sdl,"\t{}", value.name).ok();
564+
write!(sdl,"{}{}", tab(&options), value.name).ok();
526565
write_deprecated(sdl,&value.deprecation);
527566

528567
if options.federation{
@@ -542,7 +581,7 @@ impl Registry {
542581
writeln!(sdl).ok();
543582
}
544583

545-
writeln!(sdl,"}}").ok();
584+
writeln!(sdl,"}}\n").ok();
546585
}
547586
MetaType::InputObject{
548587
name,
@@ -587,7 +626,7 @@ impl Registry {
587626
ifletSome(ref description) =&field.description{
588627
write_description(sdl, options,1, description);
589628
}
590-
sdl.push('\t');
629+
write!(sdl,"{}", tab(options)).ok();
591630
write_input_value(sdl, field);
592631
if options.federation{
593632
if field.inaccessible{
@@ -603,7 +642,7 @@ impl Registry {
603642
writeln!(sdl).ok();
604643
}
605644

606-
writeln!(sdl,"}}").ok();
645+
writeln!(sdl,"}}\n").ok();
607646
}
608647
MetaType::Union{
609648
name,
@@ -641,7 +680,7 @@ impl Registry {
641680
write!(sdl," | {}", ty).ok();
642681
}
643682
}
644-
writeln!(sdl).ok();
683+
writeln!(sdl,"\n").ok();
645684
}
646685
}
647686
}
@@ -670,7 +709,7 @@ fn write_description(
670709
level:usize,
671710
description:&str,
672711
){
673-
let tabs ="\t".repeat(level);
712+
let tabs =tab(options).repeat(level);
674713

675714
if options.prefer_single_line_descriptions && !description.contains('\n'){
676715
let description = description.replace('"',r#"\""#);
@@ -730,6 +769,14 @@ fn escape_string(s: &str) -> String {
730769
res
731770
}
732771

772+
fntab(options:&SDLExportOptions) ->String{
773+
if options.use_space_ident{
774+
" ".repeat(options.indent_width.into())
775+
}else{
776+
"\t".to_string()
777+
}
778+
}
779+
733780
#[cfg(test)]
734781
mod tests{
735782
usesuper::*;

‎tests/export_sdl.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
use async_graphql::*;
2+
3+
#[tokio::test]
4+
asyncfntest_spaces(){
5+
#[derive(SimpleObject)]
6+
structA{
7+
a:i32,
8+
b:i32,
9+
}
10+
11+
#[derive(SimpleObject)]
12+
structB{
13+
a:A,
14+
b:i32,
15+
}
16+
17+
structQuery;
18+
19+
#[Object]
20+
implQuery{
21+
asyncfnobj(&self) ->B{
22+
B{
23+
a:A{a:100,b:200},
24+
b:300,
25+
}
26+
}
27+
28+
asyncfna(&self) ->A{
29+
A{a:100,b:200}
30+
}
31+
}
32+
33+
let schema =Schema::new(Query,EmptyMutation,EmptySubscription);
34+
let sdl = schema.sdl_with_options(
35+
SDLExportOptions::new()
36+
.use_space_ident()
37+
.indent_width(2)
38+
.sorted_fields()
39+
.sorted_enum_items(),
40+
);
41+
std::fs::write("./test_space_schema.graphql",&sdl).unwrap();
42+
43+
let expected =include_str!("schemas/test_space_schema.graphql");
44+
assert_eq!(sdl, expected);
45+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
typeA {
2+
a:Int!
3+
b:Int!
4+
}
5+
6+
typeB {
7+
a:A!
8+
b:Int!
9+
}
10+
11+
typeQuery {
12+
a:A!
13+
obj:B!
14+
}
15+
16+
directive@include(if:Boolean!)onFIELD |FRAGMENT_SPREAD |INLINE_FRAGMENT
17+
directive@skip(if:Boolean!)onFIELD |FRAGMENT_SPREAD |INLINE_FRAGMENT
18+
schema {
19+
query:Query
20+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp