@@ -126,7 +126,7 @@ describe('process connection', () => {
126
126
expect ( connectionInfo . kind ) . toEqual ( CodeGenConnectionType . HAS_ONE ) ;
127
127
expect ( connectionInfo . associatedWith ) . toEqual ( modelMap . License . fields [ 0 ] ) ;
128
128
expect ( connectionInfo . isConnectingFieldAutoCreated ) . toEqual ( true ) ;
129
- expect ( connectionInfo . targetName ) . toEqual ( " personLicenseId" ) ;
129
+ expect ( connectionInfo . targetName ) . toEqual ( ' personLicenseId' ) ;
130
130
} ) ;
131
131
132
132
it ( 'should return BELONGS_TO License.person field' , ( ) => {
@@ -500,11 +500,11 @@ describe('process connection', () => {
500
500
type Post @model {
501
501
comments: [Comment] @hasMany(fields: ["id"])
502
502
}
503
-
503
+
504
504
type Comment @model {
505
505
postID: ID! @primaryKey(sortKeyFields: ["content"])
506
506
content: String!
507
- post: Post @belongsTo(fields:["postID"])
507
+ post: Post @belongsTo(fields: ["postID"])
508
508
}
509
509
` ;
510
510
@@ -533,7 +533,7 @@ describe('process connection', () => {
533
533
isNullable : false ,
534
534
isList : false ,
535
535
name : 'chargerID' ,
536
- directives : [ ]
536
+ directives : [ ] ,
537
537
} ,
538
538
{
539
539
type : 'PowerSource' ,
@@ -599,7 +599,7 @@ describe('process connection', () => {
599
599
isNullable : false ,
600
600
isList : false ,
601
601
name : 'id' ,
602
- directives : [ ]
602
+ directives : [ ] ,
603
603
} ,
604
604
{
605
605
type : 'Float' ,
@@ -644,7 +644,7 @@ describe('process connection', () => {
644
644
isNullable : false ,
645
645
isList : false ,
646
646
name : 'postID' ,
647
- directives : [ { name : 'primaryKey' , arguments : { sortKeyFields : [ 'content' ] } } ] ,
647
+ directives : [ { name : 'primaryKey' , arguments : { sortKeyFields : [ 'content' ] } } ] ,
648
648
} ,
649
649
{
650
650
type : 'String' ,
@@ -710,7 +710,7 @@ describe('process connection', () => {
710
710
isNullable : false ,
711
711
isList : false ,
712
712
name : 'postID' ,
713
- directives : [ { name : 'index' , arguments : { name : 'byPost' , sortKeyFields : [ 'content' ] } } ] ,
713
+ directives : [ { name : 'index' , arguments : { name : 'byPost' , sortKeyFields : [ 'content' ] } } ] ,
714
714
} ,
715
715
{
716
716
type : 'String' ,
@@ -727,7 +727,7 @@ describe('process connection', () => {
727
727
describe ( 'Has many comparison' , ( ) => {
728
728
it ( 'should support connection with @primaryKey on BELONGS_TO side' , ( ) => {
729
729
const postField = v2ModelMap . Comment . fields [ 2 ] ;
730
- const connectionInfo = ( processConnectionsV2 ( postField , v2ModelMap . Post , v2ModelMap ) as any ) as CodeGenFieldConnectionBelongsTo ;
730
+ const connectionInfo = ( processConnectionsV2 ( postField , v2ModelMap . Comment , v2ModelMap ) as any ) as CodeGenFieldConnectionBelongsTo ;
731
731
expect ( connectionInfo ) . toBeDefined ( ) ;
732
732
expect ( connectionInfo . kind ) . toEqual ( CodeGenConnectionType . BELONGS_TO ) ;
733
733
expect ( connectionInfo . targetName ) . toEqual ( v2ModelMap . Comment . fields [ 0 ] . name ) ;
@@ -736,7 +736,7 @@ describe('process connection', () => {
736
736
737
737
it ( 'should support connection with @primaryKey on HAS_MANY side' , ( ) => {
738
738
const commentsField = v2ModelMap . Post . fields [ 0 ] ;
739
- const connectionInfo = ( processConnectionsV2 ( commentsField , v2ModelMap . Comment , v2ModelMap ) as any ) as CodeGenFieldConnectionHasMany ;
739
+ const connectionInfo = ( processConnectionsV2 ( commentsField , v2ModelMap . Post , v2ModelMap ) as any ) as CodeGenFieldConnectionHasMany ;
740
740
expect ( connectionInfo ) . toBeDefined ( ) ;
741
741
expect ( connectionInfo . kind ) . toEqual ( CodeGenConnectionType . HAS_MANY ) ;
742
742
expect ( connectionInfo . connectedModel ) . toEqual ( v2ModelMap . Comment ) ;
@@ -745,7 +745,11 @@ describe('process connection', () => {
745
745
746
746
it ( 'Should support connection with @index on BELONGS_TO side' , ( ) => {
747
747
const commentsField = v2IndexModelMap . Post . fields [ 2 ] ;
748
- const connectionInfo = ( processConnectionsV2 ( commentsField , v2IndexModelMap . Comment , v2IndexModelMap ) as any ) as CodeGenFieldConnectionHasMany ;
748
+ const connectionInfo = ( processConnectionsV2 (
749
+ commentsField ,
750
+ v2IndexModelMap . Post ,
751
+ v2IndexModelMap ,
752
+ ) as any ) as CodeGenFieldConnectionHasMany ;
749
753
expect ( connectionInfo ) . toBeDefined ( ) ;
750
754
expect ( connectionInfo . kind ) . toEqual ( CodeGenConnectionType . HAS_MANY ) ;
751
755
expect ( connectionInfo . connectedModel ) . toEqual ( v2IndexModelMap . Comment ) ;
@@ -756,21 +760,85 @@ describe('process connection', () => {
756
760
describe ( 'Has one testing' , ( ) => {
757
761
it ( 'Should support @hasOne with no explicit primary key' , ( ) => {
758
762
const powerSourceField = hasOneNoFieldsModelMap . BatteryCharger . fields [ 0 ] ;
759
- const connectionInfo = ( processConnectionsV2 ( powerSourceField , hasOneNoFieldsModelMap . PowerSource , hasOneNoFieldsModelMap ) ) as CodeGenFieldConnectionHasOne ;
763
+ const connectionInfo = processConnectionsV2 (
764
+ powerSourceField ,
765
+ hasOneNoFieldsModelMap . BatteryCharger ,
766
+ hasOneNoFieldsModelMap ,
767
+ ) as CodeGenFieldConnectionHasOne ;
760
768
expect ( connectionInfo ) . toBeDefined ( ) ;
761
769
expect ( connectionInfo . kind ) . toEqual ( CodeGenConnectionType . HAS_ONE ) ;
762
770
expect ( connectionInfo . connectedModel ) . toEqual ( hasOneNoFieldsModelMap . PowerSource ) ;
763
771
expect ( connectionInfo . isConnectingFieldAutoCreated ) . toEqual ( true ) ;
764
772
} ) ;
765
773
it ( 'Should support @hasOne with an explicit primary key' , ( ) => {
766
774
const powerSourceField = hasOneWithFieldsModelMap . BatteryCharger . fields [ 1 ] ;
767
- const connectionInfo = ( processConnectionsV2 ( powerSourceField , hasOneWithFieldsModelMap . PowerSource , hasOneWithFieldsModelMap ) ) as CodeGenFieldConnectionHasOne ;
775
+ const connectionInfo = processConnectionsV2 (
776
+ powerSourceField ,
777
+ hasOneWithFieldsModelMap . BatteryCharger ,
778
+ hasOneWithFieldsModelMap ,
779
+ ) as CodeGenFieldConnectionHasOne ;
768
780
expect ( connectionInfo ) . toBeDefined ( ) ;
769
781
expect ( connectionInfo . kind ) . toEqual ( CodeGenConnectionType . HAS_ONE ) ;
770
782
expect ( connectionInfo . connectedModel ) . toEqual ( hasOneWithFieldsModelMap . PowerSource ) ;
771
783
expect ( connectionInfo . isConnectingFieldAutoCreated ) . toEqual ( false ) ;
772
784
} ) ;
785
+ it ( 'disambiguates multiple connection directives in related type based on field type' , ( ) => {
786
+ const modelMap : CodeGenModelMap = {
787
+ Post : {
788
+ name : 'Post' ,
789
+ type : 'model' ,
790
+ directives : [ ] ,
791
+ fields : [
792
+ {
793
+ type : 'Comment' ,
794
+ isNullable : true ,
795
+ isList : false ,
796
+ name : 'comment' ,
797
+ directives : [ { name : 'hasOne' , arguments : { } } ] ,
798
+ } ,
799
+ ] ,
800
+ } ,
801
+ Comment : {
802
+ name : 'Comment' ,
803
+ type : 'model' ,
804
+ directives : [ ] ,
805
+ fields : [
806
+ {
807
+ type : 'id' ,
808
+ isNullable : false ,
809
+ isList : false ,
810
+ name : 'id' ,
811
+ directives : [ ] ,
812
+ } ,
813
+ {
814
+ type : 'Like' ,
815
+ isNullable : true ,
816
+ isList : true ,
817
+ name : 'likes' ,
818
+ directives : [ { name : 'hasMany' , arguments : { indexName : 'byComment' , fields : [ 'id' ] } } ] ,
819
+ } ,
820
+ ] ,
821
+ } ,
822
+ Like : {
823
+ name : 'Like' ,
824
+ type : 'model' ,
825
+ directives : [ ] ,
826
+ fields : [
827
+ {
828
+ type : 'string' ,
829
+ isNullable : true ,
830
+ isList : false ,
831
+ name : 'likeString' ,
832
+ directives : [ ] ,
833
+ } ,
834
+ ] ,
835
+ } ,
836
+ } ;
837
+ const connectionInfo = processConnectionsV2 ( modelMap . Post . fields [ 0 ] , modelMap . Post , modelMap ) ;
838
+ expect ( connectionInfo . kind ) . toEqual ( CodeGenConnectionType . HAS_ONE ) ;
839
+ console . log ( connectionInfo ) ;
840
+ expect ( ( connectionInfo as CodeGenFieldConnectionHasOne ) . associatedWith . name ) . toEqual ( 'id' ) ;
841
+ } ) ;
773
842
} ) ;
774
-
775
843
} ) ;
776
844
} ) ;
0 commit comments