@@ -20,14 +20,11 @@ const createAndGenerateVisitor = (schema: string, usePipelinedTransformer: boole
20
20
return visitor ;
21
21
} ;
22
22
23
- const createAndGeneratePipelinedTransformerVisitor = (
24
- schema : string
25
- ) => {
23
+ const createAndGeneratePipelinedTransformerVisitor = ( schema : string ) => {
26
24
return createAndGenerateVisitor ( schema , true ) ;
27
25
} ;
28
26
29
27
describe ( 'AppSyncModelVisitor' , ( ) => {
30
-
31
28
it ( 'should support schema with id' , ( ) => {
32
29
const schema = /* GraphQL */ `
33
30
type Post @model {
@@ -177,14 +174,14 @@ describe('AppSyncModelVisitor', () => {
177
174
describe ( 'with connection name' , ( ) => {
178
175
const schema = /* GraphQL */ `
179
176
type Post @model {
180
- id: ID!,
177
+ id: ID!
181
178
title: String!
182
179
content: String
183
180
comments: [Comment] @connection(name: "PostComment")
184
181
}
185
182
186
183
type Comment @model {
187
- id: ID!,
184
+ id: ID!
188
185
comment: String!
189
186
post: Post @connection(name: "PostComment")
190
187
}
@@ -223,14 +220,14 @@ describe('AppSyncModelVisitor', () => {
223
220
describe ( 'connection with fields argument' , ( ) => {
224
221
const schema = /* GraphQL */ `
225
222
type Post @model {
226
- id: ID!,
223
+ id: ID!
227
224
title: String!
228
225
content: String
229
226
comments: [Comment] @connection(fields: ["id"])
230
227
}
231
228
232
229
type Comment @model {
233
- id: ID!,
230
+ id: ID!
234
231
comment: String!
235
232
postId: ID!
236
233
post: Post @connection(fields: ["postId"])
@@ -275,14 +272,14 @@ describe('AppSyncModelVisitor', () => {
275
272
it ( 'should not include a comments in Post when comments field does not have connection directive' , ( ) => {
276
273
const schema = /* GraphQL */ `
277
274
type Post @model {
278
- id: ID!,
275
+ id: ID!
279
276
title: String!
280
277
content: String
281
278
comments: [Comment]
282
279
}
283
280
284
281
type Comment @model {
285
- id: ID!,
282
+ id: ID!
286
283
comment: String!
287
284
post: Post @connection
288
285
}
@@ -299,14 +296,14 @@ describe('AppSyncModelVisitor', () => {
299
296
it ( 'should not include a post when post field in Comment when post does not have connection directive' , ( ) => {
300
297
const schema = /* GraphQL */ `
301
298
type Post @model {
302
- id: ID!,
299
+ id: ID!
303
300
title: String!
304
301
content: String
305
302
comments: [Comment] @connection
306
303
}
307
304
308
305
type Comment @model {
309
- id: ID!,
306
+ id: ID!
310
307
comment: String!
311
308
post: Post
312
309
}
@@ -320,12 +317,39 @@ describe('AppSyncModelVisitor', () => {
320
317
expect ( commentsField ) . not . toContain ( 'post' ) ;
321
318
expect ( commentsField ) . toContain ( 'postCommentsId' ) ; // because of connection from Post.comments
322
319
} ) ;
320
+
321
+ it ( 'should not generate projectTeamId connection field for hasOne directive' , ( ) => {
322
+ const schema = /* GraphQL */ `
323
+ type Project @model {
324
+ id: ID!
325
+ name: String
326
+ team: Team @hasOne
327
+ }
328
+
329
+ type Team @model {
330
+ id: ID!
331
+ name: String!
332
+ }
333
+ ` ;
334
+ const ast = parse ( schema ) ;
335
+ const builtSchema = buildSchemaWithDirectives ( schema ) ;
336
+ const visitor = new AppSyncModelVisitor (
337
+ builtSchema ,
338
+ { directives, target : 'typescript' , generate : CodeGenGenerateEnum . code , usePipelinedTransformer : true } ,
339
+ { } ,
340
+ ) ;
341
+ visit ( ast , { leave : visitor } ) ;
342
+ visitor . generate ( ) ;
343
+ const teamFields = visitor . models . Team . fields . map ( field => field . name ) ;
344
+ expect ( teamFields ) . not . toContain ( 'projectTeamId' ) ;
345
+ } ) ;
323
346
} ) ;
347
+
324
348
describe ( 'auth directive' , ( ) => {
325
349
it ( 'should process auth with owner authorization' , ( ) => {
326
350
const schema = /* GraphQL */ `
327
351
type Post @searchable @model @auth(rules: [{ allow: owner }]) {
328
- id: ID!,
352
+ id: ID!
329
353
title: String!
330
354
content: String
331
355
}
@@ -353,7 +377,7 @@ describe('AppSyncModelVisitor', () => {
353
377
it ( 'should process group with owner authorization' , ( ) => {
354
378
const schema = /* GraphQL */ `
355
379
type Post @model @searchable @auth(rules: [{ allow: groups, groups: ["admin", "moderator"] }]) {
356
- id: ID!,
380
+ id: ID!
357
381
title: String!
358
382
content: String
359
383
}
@@ -577,16 +601,16 @@ describe('AppSyncModelVisitor', () => {
577
601
578
602
beforeEach ( ( ) => {
579
603
simpleManyToManySchema = /* GraphQL */ `
580
- type Human @model {
581
- governmentID: ID! @primaryKey
582
- pets: [Animal] @manyToMany(relationName: "PetFriend")
583
- }
584
-
585
- type Animal @model {
586
- animalTag: ID!
587
- humanFriend: [Human] @manyToMany(relationName: "PetFriend")
588
- }
589
- ` ;
604
+ type Human @model {
605
+ governmentID: ID! @primaryKey
606
+ pets: [Animal] @manyToMany(relationName: "PetFriend")
607
+ }
608
+
609
+ type Animal @model {
610
+ animalTag: ID!
611
+ humanFriend: [Human] @manyToMany(relationName: "PetFriend")
612
+ }
613
+ ` ;
590
614
591
615
simpleManyModelMap = {
592
616
Human : {
@@ -599,16 +623,16 @@ describe('AppSyncModelVisitor', () => {
599
623
isNullable : false ,
600
624
isList : false ,
601
625
name : 'governmentID' ,
602
- directives : [ { name : 'primaryKey' , arguments : { } } ]
626
+ directives : [ { name : 'primaryKey' , arguments : { } } ] ,
603
627
} ,
604
628
{
605
629
type : 'Animal' ,
606
630
isNullable : true ,
607
631
isList : true ,
608
632
name : 'pets' ,
609
- directives : [ { name : 'manyToMany' , arguments : { relationName : 'PetFriend' } } ]
610
- }
611
- ]
633
+ directives : [ { name : 'manyToMany' , arguments : { relationName : 'PetFriend' } } ] ,
634
+ } ,
635
+ ] ,
612
636
} ,
613
637
Animal : {
614
638
name : 'Animal' ,
@@ -627,39 +651,39 @@ describe('AppSyncModelVisitor', () => {
627
651
isNullable : true ,
628
652
isList : true ,
629
653
name : 'postID' ,
630
- directives : [ { name : 'manyToMany' , arguments : { relationName : 'PetFriend' } } ]
631
- }
654
+ directives : [ { name : 'manyToMany' , arguments : { relationName : 'PetFriend' } } ] ,
655
+ } ,
632
656
] ,
633
- }
657
+ } ,
634
658
} ;
635
659
} ) ;
636
660
637
661
transformedSimpleManyModelMap = {
638
662
Human : {
639
663
name : 'Human' ,
640
664
type : 'model' ,
641
- directives : [ { name : 'model' , arguments : { } } ] ,
665
+ directives : [ { name : 'model' , arguments : { } } ] ,
642
666
fields : [
643
667
{
644
668
type : 'ID' ,
645
669
isNullable : false ,
646
670
isList : false ,
647
671
name : 'governmentID' ,
648
- directives : [ { name : 'primaryKey' , arguments : { } } ]
672
+ directives : [ { name : 'primaryKey' , arguments : { } } ] ,
649
673
} ,
650
674
{
651
675
type : 'PetFriend' ,
652
676
isNullable : true ,
653
677
isList : true ,
654
678
name : 'pets' ,
655
- directives : [ { name : 'hasMany' , arguments : { fields : [ 'governmentID' ] } } ]
656
- }
657
- ]
679
+ directives : [ { name : 'hasMany' , arguments : { fields : [ 'governmentID' ] } } ] ,
680
+ } ,
681
+ ] ,
658
682
} ,
659
683
Animal : {
660
684
name : 'Animal' ,
661
685
type : 'model' ,
662
- directives : [ { name : 'model' , arguments : { } } ] ,
686
+ directives : [ { name : 'model' , arguments : { } } ] ,
663
687
fields : [
664
688
{
665
689
type : 'ID' ,
@@ -673,74 +697,74 @@ describe('AppSyncModelVisitor', () => {
673
697
isNullable : true ,
674
698
isList : true ,
675
699
name : 'postID' ,
676
- directives : [ { name : 'hasMany' , arguments : { fields : [ 'id' ] } } ]
677
- }
700
+ directives : [ { name : 'hasMany' , arguments : { fields : [ 'id' ] } } ] ,
701
+ } ,
678
702
] ,
679
703
} ,
680
704
PetFriend : {
681
705
name : 'PetFriend' ,
682
706
type : 'model' ,
683
- directives : [ { name : 'model' , arguments : { } } ] ,
707
+ directives : [ { name : 'model' , arguments : { } } ] ,
684
708
fields : [
685
709
{
686
710
type : 'ID' ,
687
711
isNullable : false ,
688
712
isList : false ,
689
713
name : 'id' ,
690
- directives : [ ]
714
+ directives : [ ] ,
691
715
} ,
692
716
{
693
717
type : 'ID' ,
694
718
isNullable : false ,
695
719
isList : false ,
696
720
name : 'humanID' ,
697
- directives : [ { name : 'index' , arguments : { name : 'byHuman' , sortKeyFields : [ 'animalID' ] } } ]
721
+ directives : [ { name : 'index' , arguments : { name : 'byHuman' , sortKeyFields : [ 'animalID' ] } } ] ,
698
722
} ,
699
723
{
700
724
type : 'ID' ,
701
725
isNullable : false ,
702
726
isList : false ,
703
727
name : 'animalID' ,
704
- directives : [ { name : 'index' , arguments : { name : 'byAnimal' , sortKeyFields : [ 'humanID' ] } } ]
728
+ directives : [ { name : 'index' , arguments : { name : 'byAnimal' , sortKeyFields : [ 'humanID' ] } } ] ,
705
729
} ,
706
730
{
707
731
type : 'Human' ,
708
732
isNullable : false ,
709
733
isList : false ,
710
734
name : 'human' ,
711
- directives : [ { name : 'belongsTo' , arguments : { fields : [ 'humanID' ] } } ]
735
+ directives : [ { name : 'belongsTo' , arguments : { fields : [ 'humanID' ] } } ] ,
712
736
} ,
713
737
{
714
738
type : 'Animal' ,
715
739
isNullable : false ,
716
740
isList : false ,
717
741
name : 'animal' ,
718
- directives : [ { name : 'belongsTo' , arguments : { fields : [ 'humanID' ] } } ]
719
- }
720
- ]
721
- }
742
+ directives : [ { name : 'belongsTo' , arguments : { fields : [ 'humanID' ] } } ] ,
743
+ } ,
744
+ ] ,
745
+ } ,
722
746
} ;
723
747
724
748
it ( 'Should correctly convert the model map of a simple manyToMany' , ( ) => {
725
749
const visitor = createAndGeneratePipelinedTransformerVisitor ( simpleManyToManySchema ) ;
726
750
727
751
expect ( visitor . models . Human . fields . length ) . toEqual ( 5 ) ;
728
- expect ( visitor . models . Human . fields [ 2 ] . directives [ 0 ] . name ) . toEqual ( 'hasMany' )
729
- expect ( visitor . models . Human . fields [ 2 ] . directives [ 0 ] . arguments . fields . length ) . toEqual ( 1 )
730
- expect ( visitor . models . Human . fields [ 2 ] . directives [ 0 ] . arguments . fields [ 0 ] ) . toEqual ( 'governmentID' )
731
- expect ( visitor . models . Human . fields [ 2 ] . directives [ 0 ] . arguments . indexName ) . toEqual ( 'byHuman' )
752
+ expect ( visitor . models . Human . fields [ 2 ] . directives [ 0 ] . name ) . toEqual ( 'hasMany' ) ;
753
+ expect ( visitor . models . Human . fields [ 2 ] . directives [ 0 ] . arguments . fields . length ) . toEqual ( 1 ) ;
754
+ expect ( visitor . models . Human . fields [ 2 ] . directives [ 0 ] . arguments . fields [ 0 ] ) . toEqual ( 'governmentID' ) ;
755
+ expect ( visitor . models . Human . fields [ 2 ] . directives [ 0 ] . arguments . indexName ) . toEqual ( 'byHuman' ) ;
732
756
expect ( visitor . models . PetFriend ) . toBeDefined ( ) ;
733
757
expect ( visitor . models . PetFriend . fields . length ) . toEqual ( 5 ) ;
734
- expect ( visitor . models . PetFriend . fields [ 2 ] . directives [ 0 ] . name ) . toEqual ( 'belongsTo' )
735
- expect ( visitor . models . PetFriend . fields [ 2 ] . directives [ 0 ] . arguments . fields . length ) . toEqual ( 1 )
736
- expect ( visitor . models . PetFriend . fields [ 2 ] . directives [ 0 ] . arguments . fields [ 0 ] ) . toEqual ( 'animalID' )
758
+ expect ( visitor . models . PetFriend . fields [ 2 ] . directives [ 0 ] . name ) . toEqual ( 'belongsTo' ) ;
759
+ expect ( visitor . models . PetFriend . fields [ 2 ] . directives [ 0 ] . arguments . fields . length ) . toEqual ( 1 ) ;
760
+ expect ( visitor . models . PetFriend . fields [ 2 ] . directives [ 0 ] . arguments . fields [ 0 ] ) . toEqual ( 'animalID' ) ;
737
761
expect ( visitor . models . Animal . fields . length ) . toEqual ( 5 ) ;
738
762
expect ( visitor . models . Animal . fields [ 2 ] . type ) . toEqual ( 'PetFriend' ) ;
739
763
expect ( visitor . models . Animal . fields [ 2 ] . directives . length ) . toEqual ( 1 ) ;
740
764
expect ( visitor . models . Animal . fields [ 2 ] . directives [ 0 ] . name ) . toEqual ( 'hasMany' ) ;
741
- expect ( visitor . models . Animal . fields [ 2 ] . directives [ 0 ] . arguments . fields . length ) . toEqual ( 1 )
742
- expect ( visitor . models . Animal . fields [ 2 ] . directives [ 0 ] . arguments . fields [ 0 ] ) . toEqual ( 'id' )
743
- expect ( visitor . models . Animal . fields [ 2 ] . directives [ 0 ] . arguments . indexName ) . toEqual ( 'byAnimal' )
765
+ expect ( visitor . models . Animal . fields [ 2 ] . directives [ 0 ] . arguments . fields . length ) . toEqual ( 1 ) ;
766
+ expect ( visitor . models . Animal . fields [ 2 ] . directives [ 0 ] . arguments . fields [ 0 ] ) . toEqual ( 'id' ) ;
767
+ expect ( visitor . models . Animal . fields [ 2 ] . directives [ 0 ] . arguments . indexName ) . toEqual ( 'byAnimal' ) ;
744
768
} ) ;
745
769
} ) ;
746
770
} ) ;
0 commit comments