@@ -9,19 +9,36 @@ const buildSchemaWithDirectives = (schema: String): GraphQLSchema => {
9
9
return buildSchema ( [ schema , directives , scalars ] . join ( '\n' ) ) ;
10
10
} ;
11
11
12
- const getVisitor = ( schema : string , selectedType ?: string , generate : CodeGenGenerateEnum = CodeGenGenerateEnum . code , usePipelinedTransformer : boolean = false ) => {
12
+ const getVisitor = (
13
+ schema : string ,
14
+ selectedType ?: string ,
15
+ generate : CodeGenGenerateEnum = CodeGenGenerateEnum . code ,
16
+ usePipelinedTransformer : boolean = false ,
17
+ ) => {
13
18
const ast = parse ( schema ) ;
14
19
const builtSchema = buildSchemaWithDirectives ( schema ) ;
15
20
const visitor = new AppSyncModelJavaVisitor (
16
21
builtSchema ,
17
- { directives, target : 'android' , generate, scalars : JAVA_SCALAR_MAP , isTimestampFieldsAdded : true , handleListNullabilityTransparently : true , usePipelinedTransformer : usePipelinedTransformer } ,
22
+ {
23
+ directives,
24
+ target : 'android' ,
25
+ generate,
26
+ scalars : JAVA_SCALAR_MAP ,
27
+ isTimestampFieldsAdded : true ,
28
+ handleListNullabilityTransparently : true ,
29
+ usePipelinedTransformer : usePipelinedTransformer ,
30
+ } ,
18
31
{ selectedType } ,
19
32
) ;
20
33
visit ( ast , { leave : visitor } ) ;
21
34
return visitor ;
22
35
} ;
23
36
24
- const getVisitorPipelinedTransformer = ( schema : string , selectedType ?: string , generate : CodeGenGenerateEnum = CodeGenGenerateEnum . code ) => {
37
+ const getVisitorPipelinedTransformer = (
38
+ schema : string ,
39
+ selectedType ?: string ,
40
+ generate : CodeGenGenerateEnum = CodeGenGenerateEnum . code ,
41
+ ) => {
25
42
return getVisitor ( schema , selectedType , generate , true ) ;
26
43
} ;
27
44
@@ -98,7 +115,7 @@ describe('AppSyncModelVisitor', () => {
98
115
it ( 'Should generate a class a model with all optional fields except id field' , ( ) => {
99
116
const schema = /* GraphQL */ `
100
117
type SimpleModel @model {
101
- id: ID!,
118
+ id: ID!
102
119
name: String
103
120
bar: String
104
121
}
@@ -196,23 +213,23 @@ describe('AppSyncModelVisitor', () => {
196
213
describe ( 'vNext transformer feature parity tests' , ( ) => {
197
214
it ( 'should produce the same result for @primaryKey as the primary key variant of @key' , async ( ) => {
198
215
const schemaV1 = /* GraphQL */ `
199
- type authorBook @model @key(fields: ["author_id"]) {
200
- id: ID!
201
- author_id: ID!
202
- book_id: ID!
203
- author: String
204
- book: String
205
- }
206
- ` ;
216
+ type authorBook @model @key(fields: ["author_id"]) {
217
+ id: ID!
218
+ author_id: ID!
219
+ book_id: ID!
220
+ author: String
221
+ book: String
222
+ }
223
+ ` ;
207
224
const schemaV2 = /* GraphQL */ `
208
- type authorBook @model {
209
- id: ID!
210
- author_id: ID! @primaryKey
211
- book_id: ID!
212
- author: String
213
- book: String
214
- }
215
- ` ;
225
+ type authorBook @model {
226
+ id: ID!
227
+ author_id: ID! @primaryKey
228
+ book_id: ID!
229
+ author: String
230
+ book: String
231
+ }
232
+ ` ;
216
233
const visitorV1 = getVisitor ( schemaV1 , 'authorBook' ) ;
217
234
const visitorV2 = getVisitorPipelinedTransformer ( schemaV2 , 'authorBook' ) ;
218
235
const version1Code = visitorV1 . generate ( ) ;
@@ -223,23 +240,23 @@ describe('AppSyncModelVisitor', () => {
223
240
224
241
it ( 'should produce the same result for @index as the secondary index variant of @key' , async ( ) => {
225
242
const schemaV1 = /* GraphQL */ `
226
- type authorBook @model @key(fields: ["id"]) @key(name: "authorSecondary", fields: ["author_id", "author"]) {
227
- id: ID!
228
- author_id: ID!
229
- book_id: ID!
230
- author: String
231
- book: String
232
- }
233
- ` ;
243
+ type authorBook @model @key(fields: ["id"]) @key(name: "authorSecondary", fields: ["author_id", "author"]) {
244
+ id: ID!
245
+ author_id: ID!
246
+ book_id: ID!
247
+ author: String
248
+ book: String
249
+ }
250
+ ` ;
234
251
const schemaV2 = /* GraphQL */ `
235
- type authorBook @model {
236
- id: ID! @primaryKey
237
- author_id: ID! @index(name: "authorSecondary", sortKeyFields: ["author"])
238
- book_id: ID!
239
- author: String
240
- book: String
241
- }
242
- ` ;
252
+ type authorBook @model {
253
+ id: ID! @primaryKey
254
+ author_id: ID! @index(name: "authorSecondary", sortKeyFields: ["author"])
255
+ book_id: ID!
256
+ author: String
257
+ book: String
258
+ }
259
+ ` ;
243
260
const visitorV1 = getVisitor ( schemaV1 , 'authorBook' ) ;
244
261
const visitorV2 = getVisitorPipelinedTransformer ( schemaV2 , 'authorBook' ) ;
245
262
const version1Code = visitorV1 . generate ( ) ;
@@ -260,9 +277,7 @@ describe('AppSyncModelVisitor', () => {
260
277
name: String
261
278
}
262
279
263
- type ListContainer
264
- @model
265
- {
280
+ type ListContainer @model {
266
281
id: ID!
267
282
name: String
268
283
list: [Int]
@@ -383,11 +398,11 @@ describe('AppSyncModelVisitor', () => {
383
398
384
399
it ( 'should generate class with non-default providers' , ( ) => {
385
400
const schema = /* GraphQL */ `
386
- type Employee @model @auth(rules: [{ allow: owner }, { allow: private, provider:"iam" } ]) {
401
+ type Employee @model @auth(rules: [{ allow: owner }, { allow: private, provider: "iam" }]) {
387
402
id: ID!
388
403
name: String!
389
404
address: String!
390
- ssn: String @auth(rules: [{ allow: groups, provider:"oidc", groups: ["Admins"] }])
405
+ ssn: String @auth(rules: [{ allow: groups, provider: "oidc", groups: ["Admins"] }])
391
406
}
392
407
` ;
393
408
const visitor = getVisitor ( schema , 'Employee' ) ;
@@ -453,6 +468,16 @@ describe('AppSyncModelVisitor', () => {
453
468
lang: String!
454
469
}
455
470
` ;
471
+ const nonModelwithIdSchema = /* GraphQL */ `
472
+ enum ReferenceIdTypeEnum {
473
+ ASIN
474
+ OBJECT_ID
475
+ }
476
+ type Reference {
477
+ id: String!
478
+ idType: ReferenceIdTypeEnum!
479
+ }
480
+ ` ;
456
481
it ( 'should generate class for non model types' , ( ) => {
457
482
const visitor = getVisitor ( schema , 'Location' ) ;
458
483
const generatedCode = visitor . generate ( ) ;
@@ -465,6 +490,12 @@ describe('AppSyncModelVisitor', () => {
465
490
expect ( ( ) => validateJava ( generatedCode ) ) . not . toThrow ( ) ;
466
491
expect ( generatedCode ) . toMatchSnapshot ( ) ;
467
492
} ) ;
493
+ it ( 'should generate class for non model types with id field' , ( ) => {
494
+ const visitor = getVisitor ( nonModelwithIdSchema , 'Reference' ) ;
495
+ const generatedCode = visitor . generate ( ) ;
496
+ expect ( ( ) => validateJava ( generatedCode ) ) . not . toThrow ( ) ;
497
+ expect ( generatedCode ) . toMatchSnapshot ( ) ;
498
+ } ) ;
468
499
} ) ;
469
500
470
501
it ( 'should generate Temporal type for AWSDate* scalars' , ( ) => {
0 commit comments