aws-amplify / aws-amplify/amplify-codegen
Unable to perform update and delete mutations as Codegen doesn't generate _version attribute on model types
- Dominant language
- TypeScript
- Stars
- 59
- Forks
- 64
- PR merge metrics
- No merged PRs in 30d
Description
### Before opening, please confirm:
- [X] I have installed the latest version of the Amplify CLI (see above), and confirmed that the issue still persists.
- [X] I have [searched for duplicate or closed issues](https://github.com/aws-amplify/amplify-codegen/issues?q=is%3Aissue+).
- [X] I have read the guide for [submitting bug reports](https://github.com/aws-amplify/amplify-codegen/blob/master/CONTRIBUTING.md#bugs).
- [X] I have done my best to include a minimal, self-contained set of instructions for consistently reproducing the issue.
### How did you install the Amplify CLI?
npm
### If applicable, what version of Node.js are you using?
8.1.4
### Amplify CLI Version
7.6.5
### What operating system are you using?
Mac
### Amplify Codegen Command
codegen, codegen models
### Describe the bug
_version attribute is added by AppSync resolvers when a new record is created. This enables conflict resolution (optimistic locking).
However, when Amplify generates the model types from schema.graphql, _version attribute is not declared on the model type. ModelInput object expects _version when performing updates as one would expect, but the fact that the model object returned by the query doesn't formally declare the attribute means that you cannot provide all of the required inputs to the update mutation.
### Expected behavior
_version should be declared on the auto-generated model type, or there should be another way to retrieve the record _version.
### Reproduction steps
1. `amplify configure codegen` - Follow prompts to select TypeScript
2. Add the following definition to schema.graphql
```graphql
type Todo
@model
{
id: ID!
description: String
}
```
3. `amplify codegen`
At this point you should have a Todo type that doesn't have the `_version` attribute, and a TodoInput type that does have `_version` attribute.
In src/models/index.d.ts:
```typescript
export declare class Todo {
readonly id: string;
readonly description: string;
readonly createdAt?: string;
readonly updatedAt?: string;
constructor(init: ModelInit);
static copyOf(source: Todo, mutator: (draft: MutableModel) => MutableModel | void): Todo;
}
```
In src/API.ts:
```typescript
export type TodoInput = {
id: string,
description: string,
_version?: number | null,
};
```
When I try to access the _version, I get a TypeScript compile warning:
```typescript
class GraphqlTodoDAO implements TodoDAO {
public async getTodoById(
todoId: string
): Promise {
const result: GraphQLResult = await API.graphql({
query: getTodo,
variables: {
id: todoId
}
}) as GraphQLResult;
return result.data?.getTodo as Todo;
}
public async updateTodoDescription(
todoId: string,
newDescription: string
): Promise {
const currentTodo = await this.getTodoById(todoId);
const updateTodoInput: UpdateTodoInput = {
id: todoId,
description: newDescription,
_version: currentTodo._version // Property '_version' does not exist on type 'Todo'
};
}
}
```
### GraphQL schema(s)
```graphql
# Put schemas below this line
type Todo
@model
{
id: ID!
description: String
}
```
### Log output
```
# Put your logs below this line
```
### Additional information
_No response_
Contributor guide
Assessment
This issue has not been assessed yet.