@@ -244,7 +244,8 @@ fn render_response_data_fields<'a>(
244
244
let mut variants = Vec :: new ( ) ;
245
245
246
246
render_selection (
247
- operation. selection ( ) ,
247
+ operation. refocus ( ( ) ) ,
248
+ operation. selection_ids ( ) ,
248
249
& mut fields,
249
250
& mut variants,
250
251
& mut response_types,
@@ -256,7 +257,8 @@ fn render_response_data_fields<'a>(
256
257
}
257
258
258
259
fn render_selection < ' a > (
259
- selection : impl Iterator < Item = WithQuery < ' a , SelectionId > > ,
260
+ q : WithQuery < ' a , ( ) > ,
261
+ selection : & [ SelectionId ] ,
260
262
field_buffer : & mut Vec < TokenStream > ,
261
263
variants_buffer : & mut Vec < TokenStream > ,
262
264
response_type_buffer : & mut Vec < TokenStream > ,
@@ -266,9 +268,9 @@ fn render_selection<'a>(
266
268
// TODO: if the selection has one item, we can sometimes generate fewer structs (e.g. single fragment spread)
267
269
268
270
for select in selection {
269
- match & select. get ( ) {
271
+ match q . refocus ( * select) . get ( ) {
270
272
Selection :: Field ( field) => {
271
- let field = select . refocus ( field) ;
273
+ let field = q . refocus ( field) ;
272
274
273
275
let deprecation_annotation = match (
274
276
field. schema_field ( ) . is_deprecated ( ) ,
@@ -305,7 +307,7 @@ fn render_selection<'a>(
305
307
field_buffer. push ( quote ! ( #deprecation_annotation #ident: #type_name) ) ;
306
308
}
307
309
TypeId :: Object ( _) | TypeId :: Interface ( _) => {
308
- let struct_name_string = select. full_path_prefix ( ) ;
310
+ let struct_name_string = q . refocus ( * select) . full_path_prefix ( ) ;
309
311
let struct_name = Ident :: new ( & struct_name_string, Span :: call_site ( ) ) ;
310
312
let field_type =
311
313
decorate_type ( & struct_name, field. schema_field ( ) . type_qualifiers ( ) ) ;
@@ -315,7 +317,8 @@ fn render_selection<'a>(
315
317
let mut fields = Vec :: new ( ) ;
316
318
let mut variants = Vec :: new ( ) ;
317
319
render_selection (
318
- select. subselection ( ) ,
320
+ q,
321
+ q. refocus ( * select) . subselection_ids ( ) ,
319
322
& mut fields,
320
323
& mut variants,
321
324
response_type_buffer,
@@ -338,7 +341,7 @@ fn render_selection<'a>(
338
341
// We want a struct, because we want to preserve fragments in the output,
339
342
// and there can be fragment and inline spreads for a given selection set
340
343
// on an enum.
341
- let struct_name = select. full_path_prefix ( ) ;
344
+ let struct_name = q . refocus ( * select) . full_path_prefix ( ) ;
342
345
let struct_name_ident = Ident :: new ( & struct_name, Span :: call_site ( ) ) ;
343
346
let field_type = decorate_type (
344
347
& struct_name_ident,
@@ -350,21 +353,18 @@ fn render_selection<'a>(
350
353
let mut fields = Vec :: new ( ) ;
351
354
let mut variants = Vec :: new ( ) ;
352
355
render_selection (
353
- select. subselection ( ) ,
356
+ q,
357
+ q. refocus ( * select) . subselection_ids ( ) ,
354
358
& mut fields,
355
359
& mut variants,
356
360
response_type_buffer,
357
361
response_derives,
358
362
options,
359
363
) ;
360
364
361
- let struct_definition = render_object_like_struct (
362
- response_derives,
363
- & struct_name,
364
- & fields,
365
- & variants,
366
- ) ;
367
- response_type_buffer. push ( struct_definition) ;
365
+ let enum_definition =
366
+ render_union_enum ( response_derives, & struct_name, & variants) ;
367
+ response_type_buffer. push ( enum_definition) ;
368
368
}
369
369
TypeId :: Input ( _) => unreachable ! ( "field selection on input type" ) ,
370
370
} ;
@@ -376,29 +376,46 @@ fn render_selection<'a>(
376
376
) ) ;
377
377
}
378
378
Selection :: InlineFragment ( inline) => {
379
- let variant_name = select. refocus ( inline) . on ( ) . name ( ) ;
380
- let variant_name = Ident :: new ( variant_name, Span :: call_site ( ) ) ;
381
- let variant_struct_name = select. full_path_prefix ( ) ;
382
- let variant_struct_name = Ident :: new ( & variant_struct_name, Span :: call_site ( ) ) ;
383
-
384
- let variant = quote ! ( #variant_name( #variant_struct_name) ) ;
385
- variants_buffer. push ( variant) ;
379
+ let variant_name_str = q. refocus ( inline) . on ( ) . name ( ) ;
380
+ let variant_name = Ident :: new ( variant_name_str, Span :: call_site ( ) ) ;
381
+ let variant_struct_name_str = q. refocus ( * select) . full_path_prefix ( ) ;
382
+ let variant_struct_name = Ident :: new ( & variant_struct_name_str, Span :: call_site ( ) ) ;
386
383
387
384
// Render the struct for the selection
388
385
389
- todo ! ( "We have to do more here" ) ;
386
+ let mut fields = Vec :: new ( ) ;
387
+ let mut variants = Vec :: new ( ) ;
390
388
391
389
render_selection (
392
- select. subselection ( ) ,
390
+ q,
391
+ q. refocus ( * select) . subselection_ids ( ) ,
393
392
& mut fields,
394
393
& mut variants,
395
394
response_type_buffer,
396
395
response_derives,
397
396
options,
398
397
) ;
398
+
399
+ let variant = quote ! ( #variant_name { } ) ;
400
+ variants_buffer. push ( variant) ;
401
+
402
+ match q. refocus ( inline) . on ( ) . item {
403
+ TypeId :: Object ( _) | TypeId :: Interface ( _) => {
404
+ let struct_definition = render_object_like_struct (
405
+ response_derives,
406
+ & variant_struct_name_str,
407
+ & fields,
408
+ & variants,
409
+ ) ;
410
+
411
+ response_type_buffer. push ( struct_definition) ;
412
+ }
413
+ TypeId :: Union ( _) => todo ! ( "inline fragment on union" ) ,
414
+ other => unreachable ! ( "Inline fragment on non composite type ({:?})" , other) ,
415
+ }
399
416
}
400
417
Selection :: FragmentSpread ( frag) => {
401
- let frag = select . refocus ( * frag) ;
418
+ let frag = q . refocus ( * frag) ;
402
419
let original_field_name = frag. name ( ) . to_snake_case ( ) ;
403
420
let final_field_name = keyword_replace ( & original_field_name) ;
404
421
let annotation = field_rename_annotation ( & original_field_name, & final_field_name) ;
@@ -494,7 +511,8 @@ fn generate_fragment_definitions(
494
511
let mut variants = Vec :: new ( ) ;
495
512
496
513
render_selection (
497
- fragment. selection ( ) ,
514
+ fragment. refocus ( ( ) ) ,
515
+ fragment. selection_ids ( ) ,
498
516
& mut fields,
499
517
& mut variants,
500
518
& mut response_type_buffer,
0 commit comments