@@ -296,7 +296,7 @@ export class AppSyncModelVisitor<
296
296
}
297
297
processDirectives (
298
298
// TODO: Remove us when we have a fix to roll-forward.
299
- shouldUseModelNameFieldInHasManyAndBelongsTo : boolean
299
+ shouldUseModelNameFieldInHasManyAndBelongsTo : boolean ,
300
300
) {
301
301
if ( this . config . usePipelinedTransformer || this . config . transformerVersion === 2 ) {
302
302
this . processV2KeyDirectives ( ) ;
@@ -551,15 +551,12 @@ export class AppSyncModelVisitor<
551
551
} ) ;
552
552
}
553
553
554
- protected generateIntermediateModel (
555
- firstModel : CodeGenModel ,
556
- secondModel : CodeGenModel ,
557
- firstField : CodeGenField ,
558
- secondField : CodeGenField ,
559
- relationName : string ,
560
- ) {
554
+ protected generateIntermediateModel ( firstModel : CodeGenModel , secondModel : CodeGenModel , relationName : string ) {
561
555
const firstModelKeyFieldName = `${ camelCase ( firstModel . name ) } ID` ;
556
+ const firstModelSortKeyFields : CodeGenField [ ] = this . getSortKeyFields ( firstModel ) ;
562
557
const secondModelKeyFieldName = `${ camelCase ( secondModel . name ) } ID` ;
558
+ const secondModelSortKeyFields : CodeGenField [ ] = this . getSortKeyFields ( secondModel ) ;
559
+
563
560
let intermediateModel : CodeGenModel = {
564
561
name : relationName ,
565
562
type : 'model' ,
@@ -577,15 +574,49 @@ export class AppSyncModelVisitor<
577
574
isNullable : false ,
578
575
isList : false ,
579
576
name : firstModelKeyFieldName ,
580
- directives : [ { name : 'index' , arguments : { name : 'by' + firstModel . name , sortKeyFields : [ secondModelKeyFieldName ] } } ] ,
577
+ directives : [
578
+ {
579
+ name : 'index' ,
580
+ arguments : {
581
+ name : 'by' + firstModel . name ,
582
+ sortKeyFields : firstModelSortKeyFields . map ( f => this . generateIntermediateModelSortKeyFieldName ( firstModel , f ) ) ,
583
+ } ,
584
+ } ,
585
+ ] ,
581
586
} ,
587
+ ...firstModelSortKeyFields . map ( field => {
588
+ return {
589
+ type : field . type ,
590
+ isNullable : false ,
591
+ isList : field . isList ,
592
+ name : this . generateIntermediateModelSortKeyFieldName ( firstModel , field ) ,
593
+ directives : [ ] ,
594
+ } ;
595
+ } ) ,
582
596
{
583
597
type : 'ID' ,
584
598
isNullable : false ,
585
599
isList : false ,
586
600
name : secondModelKeyFieldName ,
587
- directives : [ { name : 'index' , arguments : { name : 'by' + secondModel . name , sortKeyFields : [ firstModelKeyFieldName ] } } ] ,
601
+ directives : [
602
+ {
603
+ name : 'index' ,
604
+ arguments : {
605
+ name : 'by' + secondModel . name ,
606
+ sortKeyFields : secondModelSortKeyFields . map ( f => this . generateIntermediateModelSortKeyFieldName ( secondModel , f ) ) ,
607
+ } ,
608
+ } ,
609
+ ] ,
588
610
} ,
611
+ ...secondModelSortKeyFields . map ( field => {
612
+ return {
613
+ type : field . type ,
614
+ isNullable : false ,
615
+ isList : field . isList ,
616
+ name : this . generateIntermediateModelSortKeyFieldName ( secondModel , field ) ,
617
+ directives : [ ] ,
618
+ } ;
619
+ } ) ,
589
620
{
590
621
type : firstModel . name ,
591
622
isNullable : false ,
@@ -606,6 +637,18 @@ export class AppSyncModelVisitor<
606
637
return intermediateModel ;
607
638
}
608
639
640
+ protected generateIntermediateModelSortKeyFieldName ( model : CodeGenModel , sortKeyField : CodeGenField ) : string {
641
+ const modelName = model . name . charAt ( 0 ) . toLocaleLowerCase ( ) + model . name . slice ( 1 ) ;
642
+ return `${ modelName } ${ sortKeyField . name } ` ;
643
+ }
644
+
645
+ protected getSortKeyFields ( model : CodeGenModel ) : CodeGenField [ ] {
646
+ const keyDirective = model . directives . find ( d => d . name === 'key' && ! d . arguments . name ) ;
647
+ return keyDirective
648
+ ? ( keyDirective . arguments . fields as string [ ] ) . slice ( 1 ) . map ( fieldName => model . fields . find ( f => f . name === fieldName ) ! )
649
+ : [ ] ;
650
+ }
651
+
609
652
protected determinePrimaryKeyFieldname ( model : CodeGenModel ) : string {
610
653
let primaryKeyFieldName = 'id' ;
611
654
model . fields . forEach ( field => {
@@ -664,12 +707,13 @@ export class AppSyncModelVisitor<
664
707
let intermediateModel = this . generateIntermediateModel (
665
708
value [ 0 ] . model ,
666
709
value [ 1 ] . model ,
667
- value [ 0 ] . field ,
668
- value [ 1 ] . field ,
669
710
graphqlName ( toUpper ( value [ 0 ] . directive . arguments . relationName ) ) ,
670
711
) ;
671
712
const modelDirective = intermediateModel . directives . find ( directive => directive . name === 'model' ) ;
672
713
if ( modelDirective ) {
714
+ // Maps @primaryKey and @index of intermediate model to old @key
715
+ processPrimaryKey ( intermediateModel ) ;
716
+ processIndex ( intermediateModel ) ;
673
717
this . ensureIdField ( intermediateModel ) ;
674
718
this . addTimestampFields ( intermediateModel , modelDirective ) ;
675
719
this . sortFields ( intermediateModel ) ;
@@ -682,7 +726,7 @@ export class AppSyncModelVisitor<
682
726
683
727
protected processConnectionDirectivesV2 (
684
728
// TODO: Remove us when we have a fix to roll-forward.
685
- shouldUseModelNameFieldInHasManyAndBelongsTo : boolean
729
+ shouldUseModelNameFieldInHasManyAndBelongsTo : boolean ,
686
730
) : void {
687
731
this . processManyToManyDirectives ( ) ;
688
732
@@ -726,15 +770,17 @@ export class AppSyncModelVisitor<
726
770
Object . values ( this . modelMap ) . forEach ( model => {
727
771
model . fields . forEach ( field => {
728
772
const connectionInfo = field . connectionInfo ;
729
- if ( connectionInfo
730
- && connectionInfo . kind !== CodeGenConnectionType . HAS_MANY
731
- && connectionInfo . kind !== CodeGenConnectionType . HAS_ONE
732
- && connectionInfo . targetName !== 'id' ) {
773
+ if (
774
+ connectionInfo &&
775
+ connectionInfo . kind !== CodeGenConnectionType . HAS_MANY &&
776
+ connectionInfo . kind !== CodeGenConnectionType . HAS_ONE &&
777
+ connectionInfo . targetName !== 'id'
778
+ ) {
733
779
// Need to remove the field that is targetName
734
780
removeFieldFromModel ( model , connectionInfo . targetName ) ;
735
781
}
736
782
} ) ;
737
- } )
783
+ } ) ;
738
784
}
739
785
740
786
protected processV2KeyDirectives ( ) : void {
0 commit comments