-
Notifications
You must be signed in to change notification settings - Fork 64
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(appsync-modelgen-plugin): non model type with id field in java #260
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,19 +9,36 @@ const buildSchemaWithDirectives = (schema: String): GraphQLSchema => { | |
return buildSchema([schema, directives, scalars].join('\n')); | ||
}; | ||
|
||
const getVisitor = (schema: string, selectedType?: string, generate: CodeGenGenerateEnum = CodeGenGenerateEnum.code, usePipelinedTransformer: boolean = false) => { | ||
const getVisitor = ( | ||
schema: string, | ||
selectedType?: string, | ||
generate: CodeGenGenerateEnum = CodeGenGenerateEnum.code, | ||
usePipelinedTransformer: boolean = false, | ||
) => { | ||
const ast = parse(schema); | ||
const builtSchema = buildSchemaWithDirectives(schema); | ||
const visitor = new AppSyncModelJavaVisitor( | ||
builtSchema, | ||
{ directives, target: 'android', generate, scalars: JAVA_SCALAR_MAP, isTimestampFieldsAdded: true, handleListNullabilityTransparently: true, usePipelinedTransformer: usePipelinedTransformer }, | ||
{ | ||
directives, | ||
target: 'android', | ||
generate, | ||
scalars: JAVA_SCALAR_MAP, | ||
isTimestampFieldsAdded: true, | ||
handleListNullabilityTransparently: true, | ||
usePipelinedTransformer: usePipelinedTransformer, | ||
}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. lint change |
||
{ selectedType }, | ||
); | ||
visit(ast, { leave: visitor }); | ||
return visitor; | ||
}; | ||
|
||
const getVisitorPipelinedTransformer = (schema: string, selectedType?: string, generate: CodeGenGenerateEnum = CodeGenGenerateEnum.code) => { | ||
const getVisitorPipelinedTransformer = ( | ||
schema: string, | ||
selectedType?: string, | ||
generate: CodeGenGenerateEnum = CodeGenGenerateEnum.code, | ||
) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. lint change |
||
return getVisitor(schema, selectedType, generate, true); | ||
}; | ||
|
||
|
@@ -98,7 +115,7 @@ describe('AppSyncModelVisitor', () => { | |
it('Should generate a class a model with all optional fields except id field', () => { | ||
const schema = /* GraphQL */ ` | ||
type SimpleModel @model { | ||
id: ID!, | ||
id: ID! | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. lint change |
||
name: String | ||
bar: String | ||
} | ||
|
@@ -196,23 +213,23 @@ describe('AppSyncModelVisitor', () => { | |
describe('vNext transformer feature parity tests', () => { | ||
it('should produce the same result for @primaryKey as the primary key variant of @key', async () => { | ||
const schemaV1 = /* GraphQL */ ` | ||
type authorBook @model @key(fields: ["author_id"]) { | ||
id: ID! | ||
author_id: ID! | ||
book_id: ID! | ||
author: String | ||
book: String | ||
} | ||
`; | ||
type authorBook @model @key(fields: ["author_id"]) { | ||
id: ID! | ||
author_id: ID! | ||
book_id: ID! | ||
author: String | ||
book: String | ||
} | ||
`; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. lint change |
||
const schemaV2 = /* GraphQL */ ` | ||
type authorBook @model { | ||
id: ID! | ||
author_id: ID! @primaryKey | ||
book_id: ID! | ||
author: String | ||
book: String | ||
} | ||
`; | ||
type authorBook @model { | ||
id: ID! | ||
author_id: ID! @primaryKey | ||
book_id: ID! | ||
author: String | ||
book: String | ||
} | ||
`; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. lint change |
||
const visitorV1 = getVisitor(schemaV1, 'authorBook'); | ||
const visitorV2 = getVisitorPipelinedTransformer(schemaV2, 'authorBook'); | ||
const version1Code = visitorV1.generate(); | ||
|
@@ -223,23 +240,23 @@ describe('AppSyncModelVisitor', () => { | |
|
||
it('should produce the same result for @index as the secondary index variant of @key', async () => { | ||
const schemaV1 = /* GraphQL */ ` | ||
type authorBook @model @key(fields: ["id"]) @key(name: "authorSecondary", fields: ["author_id", "author"]) { | ||
id: ID! | ||
author_id: ID! | ||
book_id: ID! | ||
author: String | ||
book: String | ||
} | ||
`; | ||
type authorBook @model @key(fields: ["id"]) @key(name: "authorSecondary", fields: ["author_id", "author"]) { | ||
id: ID! | ||
author_id: ID! | ||
book_id: ID! | ||
author: String | ||
book: String | ||
} | ||
`; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. lint change |
||
const schemaV2 = /* GraphQL */ ` | ||
type authorBook @model { | ||
id: ID! @primaryKey | ||
author_id: ID! @index(name: "authorSecondary", sortKeyFields: ["author"]) | ||
book_id: ID! | ||
author: String | ||
book: String | ||
} | ||
`; | ||
type authorBook @model { | ||
id: ID! @primaryKey | ||
author_id: ID! @index(name: "authorSecondary", sortKeyFields: ["author"]) | ||
book_id: ID! | ||
author: String | ||
book: String | ||
} | ||
`; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. lint change |
||
const visitorV1 = getVisitor(schemaV1, 'authorBook'); | ||
const visitorV2 = getVisitorPipelinedTransformer(schemaV2, 'authorBook'); | ||
const version1Code = visitorV1.generate(); | ||
|
@@ -260,9 +277,7 @@ describe('AppSyncModelVisitor', () => { | |
name: String | ||
} | ||
|
||
type ListContainer | ||
@model | ||
{ | ||
type ListContainer @model { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. lint change |
||
id: ID! | ||
name: String | ||
list: [Int] | ||
|
@@ -383,11 +398,11 @@ describe('AppSyncModelVisitor', () => { | |
|
||
it('should generate class with non-default providers', () => { | ||
const schema = /* GraphQL */ ` | ||
type Employee @model @auth(rules: [{ allow: owner }, { allow: private, provider:"iam" } ]) { | ||
type Employee @model @auth(rules: [{ allow: owner }, { allow: private, provider: "iam" }]) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. lint change |
||
id: ID! | ||
name: String! | ||
address: String! | ||
ssn: String @auth(rules: [{ allow: groups, provider:"oidc", groups: ["Admins"] }]) | ||
ssn: String @auth(rules: [{ allow: groups, provider: "oidc", groups: ["Admins"] }]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. lint change |
||
} | ||
`; | ||
const visitor = getVisitor(schema, 'Employee'); | ||
|
@@ -453,6 +468,16 @@ describe('AppSyncModelVisitor', () => { | |
lang: String! | ||
} | ||
`; | ||
const nonModelwithIdSchema = /* GraphQL */ ` | ||
enum ReferenceIdTypeEnum { | ||
ASIN | ||
OBJECT_ID | ||
} | ||
type Reference { | ||
id: String! | ||
idType: ReferenceIdTypeEnum! | ||
} | ||
`; | ||
it('should generate class for non model types', () => { | ||
const visitor = getVisitor(schema, 'Location'); | ||
const generatedCode = visitor.generate(); | ||
|
@@ -465,6 +490,12 @@ describe('AppSyncModelVisitor', () => { | |
expect(() => validateJava(generatedCode)).not.toThrow(); | ||
expect(generatedCode).toMatchSnapshot(); | ||
}); | ||
it('should generate class for non model types with id field', () => { | ||
const visitor = getVisitor(nonModelwithIdSchema, 'Reference'); | ||
const generatedCode = visitor.generate(); | ||
expect(() => validateJava(generatedCode)).not.toThrow(); | ||
expect(generatedCode).toMatchSnapshot(); | ||
}); | ||
}); | ||
|
||
it('should generate Temporal type for AWSDate* scalars', () => { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lint change