Skip to content

Commit 58851c2

Browse files
authored
Merge pull request #136 from aws-amplify/master
Release: Fix modelgen failure for existing models without ID fields
2 parents 7ae2959 + 84285ae commit 58851c2

File tree

2 files changed

+7
-25
lines changed

2 files changed

+7
-25
lines changed

packages/appsync-modelgen-plugin/src/__tests__/visitors/appsync-visitor.test.ts

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,30 +17,6 @@ const createAndGenerateVisitor = (schema: string) => {
1717
};
1818

1919
describe('AppSyncModelVisitor', () => {
20-
it('should throw error when model has no id field', () => {
21-
const schema = /* GraphQL */ `
22-
enum Status {
23-
draft
24-
inReview
25-
published
26-
}
27-
type Post @model {
28-
title: String!
29-
content: String
30-
comments: [Comment] @connection
31-
status: Status!
32-
}
33-
34-
type Comment @model {
35-
comment: String!
36-
post: Post @connection
37-
}
38-
`;
39-
const ast = parse(schema);
40-
const builtSchema = buildSchemaWithDirectives(schema);
41-
const visitor = new AppSyncModelVisitor(builtSchema, { directives, target: 'android', generate: CodeGenGenerateEnum.code }, {});
42-
expect(() => visit(ast, { leave: visitor })).toThrowErrorMatchingInlineSnapshot('"Post model does not have the required id field"');
43-
});
4420

4521
it('should support schema with id', () => {
4622
const schema = /* GraphQL */ `

packages/appsync-modelgen-plugin/src/visitors/appsync-visitor.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,13 @@ export class AppSyncModelVisitor<
436436
// Make id field required
437437
idField.isNullable = false;
438438
} else {
439-
throw new Error(`${model.name} model does not have the required id field`);
439+
model.fields.splice(0, 0, {
440+
name: 'id',
441+
type: 'ID',
442+
isNullable: false,
443+
isList: false,
444+
directives: [],
445+
});
440446
}
441447
}
442448

0 commit comments

Comments
 (0)