@@ -24,6 +24,18 @@ const createAndGeneratePipelinedTransformerVisitor = (schema: string) => {
2424 return createAndGenerateVisitor ( schema , true ) ;
2525} ;
2626
27+ const createGraphQLV2TransformerVisitor = ( schema : string ) => {
28+ const ast = parse ( schema ) ;
29+ const builtSchema = buildSchemaWithDirectives ( schema ) ;
30+ const visitor = new AppSyncModelVisitor (
31+ builtSchema ,
32+ { directives, target : 'general' , isTimestampFieldsAdded : true , usePipelinedTransformer : true } ,
33+ { generate : CodeGenGenerateEnum . code } ,
34+ ) ;
35+ visit ( ast , { leave : visitor } ) ;
36+ return visitor ;
37+ }
38+
2739describe ( 'AppSyncModelVisitor' , ( ) => {
2840 it ( 'should support schema with id' , ( ) => {
2941 const schema = /* GraphQL */ `
@@ -897,4 +909,34 @@ describe('AppSyncModelVisitor', () => {
897909 expect ( visitor . models . ModelB . fields [ 1 ] . directives [ 0 ] . arguments . indexName ) . toEqual ( 'byModelB' ) ;
898910 } ) ;
899911 } ) ;
912+
913+ describe . only ( 'Graphql V2 fix tests for multiple has many relations of only one model type' , ( ) => {
914+ const schema = /* GraphQL*/ `
915+ type Meeting @model {
916+ id: ID! @primaryKey
917+ title: String!
918+ attendees: [Registration] @hasMany(indexName: "byMeeting", fields: ["id"])
919+ }
920+
921+ type Attendee @model {
922+ id: ID! @primaryKey
923+ meetings: [Registration] @hasMany(indexName: "byAttendee", fields: ["id"])
924+ }
925+
926+ type Registration @model {
927+ id: ID! @primaryKey
928+ meetingId: ID @index(name: "byMeeting", sortKeyFields: ["attendeeId"])
929+ meeting: Meeting! @belongsTo(fields: ["meetingId"])
930+ attendeeId: ID @index(name: "byAttendee", sortKeyFields: ["meetingId"])
931+ attendee: Attendee! @belongsTo(fields: ["attendeeId"])
932+ }
933+ ` ;
934+ const outputModels : string [ ] = [ 'Meeting' , 'Attendee' , 'Registration' ] ;
935+ outputModels . forEach ( model => {
936+ it ( `should not throw error when processing ${ model } ` , ( ) => {
937+ const visitor = createGraphQLV2TransformerVisitor ( schema ) ;
938+ expect ( ( ) => visitor . generate ( ) ) . not . toThrow ( ) ;
939+ } ) ;
940+ } )
941+ } )
900942} ) ;
0 commit comments