9
9
import { buildSchema , parse , visit } from 'graphql' ;
10
10
import { directives , scalars } from '../../scalars/supported-directives' ;
11
11
import { AppSyncModelVisitor , CodeGenGenerateEnum } from '../../visitors/appsync-visitor' ;
12
+
12
13
describe ( 'GraphQL V2 process connections tests' , ( ) => {
13
14
describe ( 'GraphQL vNext getConnectedField tests with @primaryKey and @index' , ( ) => {
14
15
let hasOneWithFieldsModelMap : CodeGenModelMap ;
@@ -625,6 +626,7 @@ describe('GraphQL V2 process connections tests', () => {
625
626
} ) ;
626
627
} ) ;
627
628
} ) ;
629
+
628
630
describe ( 'Connection process with custom Primary Key support tests' , ( ) => {
629
631
const createBaseVisitorWithCustomPrimaryKeyEnabled = ( schema : string ) => {
630
632
const visitorConfig = {
@@ -901,6 +903,7 @@ describe('Connection process with custom Primary Key support tests', () => {
901
903
} ) ;
902
904
} ) ;
903
905
} ) ;
906
+
904
907
describe ( 'belongsTo special case tests' , ( ) => {
905
908
it ( 'should return correct connection info in multiple hasOne defined in parent' , ( ) => {
906
909
const schema = /* GraphQL */ `
@@ -951,5 +954,83 @@ describe('Connection process with custom Primary Key support tests', () => {
951
954
isConnectingFieldAutoCreated : false ,
952
955
} ) ;
953
956
} ) ;
957
+
958
+ it ( 'With shouldUseFieldsInAssociatedWithInHasOne and CPK enabled, should return correct connection info for bi-directional has-one with CPK' , ( ) => {
959
+ const schema = /* GraphQL */ `
960
+ type CompositeOwner @model {
961
+ lastName: ID! @primaryKey(sortKeyFields: ["firstName"])
962
+ firstName: String!
963
+ compositeDog: CompositeDog @hasOne
964
+ }
965
+ type CompositeDog @model {
966
+ name: ID! @primaryKey(sortKeyFields: ["description"])
967
+ description: String!
968
+ compositeOwner: CompositeOwner @belongsTo
969
+ }
970
+ ` ;
971
+ const modelMap : CodeGenModelMap = createBaseVisitorWithCustomPrimaryKeyEnabled ( schema ) . models ;
972
+ const compositeOwner : CodeGenModel = modelMap . CompositeOwner ;
973
+ const compositeDog : CodeGenModel = modelMap . CompositeDog ;
974
+ const hasOneField = compositeOwner . fields . find ( f => f . name === 'compositeDog' ) ! ;
975
+ const belongsToField = compositeDog . fields . find ( f => f . name === 'compositeOwner' ) ! ;
976
+ const hasOneAssociatedWithFields = compositeDog . fields . filter ( f => f . name === 'name' || f . name === 'description' ) ! ;
977
+ const hasOneRelationInfo = processConnectionsV2 ( hasOneField , compositeOwner , modelMap , false , true , true ) ;
978
+ const belongsToRelationInfo = processConnectionsV2 ( belongsToField , compositeDog , modelMap , false , true , true ) ;
979
+ expect ( hasOneRelationInfo ) . toEqual ( {
980
+ kind : CodeGenConnectionType . HAS_ONE ,
981
+ associatedWith : hasOneAssociatedWithFields [ 0 ] ,
982
+ associatedWithFields : hasOneAssociatedWithFields ,
983
+ targetName : 'compositeOwnerCompositeDogName' ,
984
+ targetNames : [ 'compositeOwnerCompositeDogName' , 'compositeOwnerCompositeDogDescription' ] ,
985
+ connectedModel : compositeDog ,
986
+ isConnectingFieldAutoCreated : true ,
987
+ } ) ;
988
+ expect ( belongsToRelationInfo ) . toEqual ( {
989
+ kind : CodeGenConnectionType . BELONGS_TO ,
990
+ targetName : 'compositeDogCompositeOwnerLastName' ,
991
+ targetNames : [ 'compositeDogCompositeOwnerLastName' , 'compositeDogCompositeOwnerFirstName' ] ,
992
+ connectedModel : compositeOwner ,
993
+ isConnectingFieldAutoCreated : false ,
994
+ } ) ;
995
+ } ) ;
996
+
997
+ it ( 'With shouldUseFieldsInAssociatedWithInHasOne and CPK enabled, should return correct connection info for bi-directional has-one without CPK' , ( ) => {
998
+ const schema = /* GraphQL */ `
999
+ type BoringOwner @model {
1000
+ id: ID!
1001
+ name: String
1002
+ boringDog: BoringDog @hasOne
1003
+ }
1004
+ type BoringDog @model {
1005
+ id: ID!
1006
+ name: String
1007
+ boringOwner: BoringOwner @belongsTo
1008
+ }
1009
+ ` ;
1010
+ const modelMap : CodeGenModelMap = createBaseVisitorWithCustomPrimaryKeyEnabled ( schema ) . models ;
1011
+ const boringOwner : CodeGenModel = modelMap . BoringOwner ;
1012
+ const boringDog : CodeGenModel = modelMap . BoringDog ;
1013
+ const hasOneField = boringOwner . fields . find ( f => f . name === 'boringDog' ) ! ;
1014
+ const belongsToField = boringDog . fields . find ( f => f . name === 'boringOwner' ) ! ;
1015
+ const hasOneAssociatedWithFields = boringDog . fields . filter ( f => f . name === 'id' ) ! ;
1016
+ const hasOneRelationInfo = processConnectionsV2 ( hasOneField , boringOwner , modelMap , false , true , true ) ;
1017
+ const belongsToRelationInfo = processConnectionsV2 ( belongsToField , boringDog , modelMap , false , true , true ) ;
1018
+ expect ( hasOneRelationInfo ) . toEqual ( {
1019
+ kind : CodeGenConnectionType . HAS_ONE ,
1020
+ associatedWith : hasOneAssociatedWithFields [ 0 ] ,
1021
+ associatedWithFields : hasOneAssociatedWithFields ,
1022
+ targetName : 'boringOwnerBoringDogId' ,
1023
+ targetNames : [ 'boringOwnerBoringDogId' ] ,
1024
+ connectedModel : boringDog ,
1025
+ isConnectingFieldAutoCreated : true ,
1026
+ } ) ;
1027
+ expect ( belongsToRelationInfo ) . toEqual ( {
1028
+ kind : CodeGenConnectionType . BELONGS_TO ,
1029
+ targetName : 'boringDogBoringOwnerId' ,
1030
+ targetNames : [ 'boringDogBoringOwnerId' ] ,
1031
+ connectedModel : boringOwner ,
1032
+ isConnectingFieldAutoCreated : false ,
1033
+ } ) ;
1034
+ } ) ;
954
1035
} )
955
1036
} ) ;
0 commit comments