@@ -9,19 +9,36 @@ const buildSchemaWithDirectives = (schema: String): GraphQLSchema => {
99 return buildSchema ( [ schema , directives , scalars ] . join ( '\n' ) ) ;
1010} ;
1111
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+ ) => {
1318 const ast = parse ( schema ) ;
1419 const builtSchema = buildSchemaWithDirectives ( schema ) ;
1520 const visitor = new AppSyncModelJavaVisitor (
1621 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+ } ,
1831 { selectedType } ,
1932 ) ;
2033 visit ( ast , { leave : visitor } ) ;
2134 return visitor ;
2235} ;
2336
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+ ) => {
2542 return getVisitor ( schema , selectedType , generate , true ) ;
2643} ;
2744
@@ -98,7 +115,7 @@ describe('AppSyncModelVisitor', () => {
98115 it ( 'Should generate a class a model with all optional fields except id field' , ( ) => {
99116 const schema = /* GraphQL */ `
100117 type SimpleModel @model {
101- id: ID!,
118+ id: ID!
102119 name: String
103120 bar: String
104121 }
@@ -196,23 +213,23 @@ describe('AppSyncModelVisitor', () => {
196213 describe ( 'vNext transformer feature parity tests' , ( ) => {
197214 it ( 'should produce the same result for @primaryKey as the primary key variant of @key' , async ( ) => {
198215 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+ ` ;
207224 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+ ` ;
216233 const visitorV1 = getVisitor ( schemaV1 , 'authorBook' ) ;
217234 const visitorV2 = getVisitorPipelinedTransformer ( schemaV2 , 'authorBook' ) ;
218235 const version1Code = visitorV1 . generate ( ) ;
@@ -223,23 +240,23 @@ describe('AppSyncModelVisitor', () => {
223240
224241 it ( 'should produce the same result for @index as the secondary index variant of @key' , async ( ) => {
225242 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+ ` ;
234251 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+ ` ;
243260 const visitorV1 = getVisitor ( schemaV1 , 'authorBook' ) ;
244261 const visitorV2 = getVisitorPipelinedTransformer ( schemaV2 , 'authorBook' ) ;
245262 const version1Code = visitorV1 . generate ( ) ;
@@ -260,9 +277,7 @@ describe('AppSyncModelVisitor', () => {
260277 name: String
261278 }
262279
263- type ListContainer
264- @model
265- {
280+ type ListContainer @model {
266281 id: ID!
267282 name: String
268283 list: [Int]
@@ -383,11 +398,11 @@ describe('AppSyncModelVisitor', () => {
383398
384399 it ( 'should generate class with non-default providers' , ( ) => {
385400 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" }]) {
387402 id: ID!
388403 name: String!
389404 address: String!
390- ssn: String @auth(rules: [{ allow: groups, provider:"oidc", groups: ["Admins"] }])
405+ ssn: String @auth(rules: [{ allow: groups, provider: "oidc", groups: ["Admins"] }])
391406 }
392407 ` ;
393408 const visitor = getVisitor ( schema , 'Employee' ) ;
@@ -453,6 +468,16 @@ describe('AppSyncModelVisitor', () => {
453468 lang: String!
454469 }
455470 ` ;
471+ const nonModelwithIdSchema = /* GraphQL */ `
472+ enum ReferenceIdTypeEnum {
473+ ASIN
474+ OBJECT_ID
475+ }
476+ type Reference {
477+ id: String!
478+ idType: ReferenceIdTypeEnum!
479+ }
480+ ` ;
456481 it ( 'should generate class for non model types' , ( ) => {
457482 const visitor = getVisitor ( schema , 'Location' ) ;
458483 const generatedCode = visitor . generate ( ) ;
@@ -465,6 +490,12 @@ describe('AppSyncModelVisitor', () => {
465490 expect ( ( ) => validateJava ( generatedCode ) ) . not . toThrow ( ) ;
466491 expect ( generatedCode ) . toMatchSnapshot ( ) ;
467492 } ) ;
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+ } ) ;
468499 } ) ;
469500
470501 it ( 'should generate Temporal type for AWSDate* scalars' , ( ) => {
0 commit comments