Description
Before opening, please confirm:
- I have installed the latest version of the Amplify CLI (see above), and confirmed that the issue still persists.
- I have searched for duplicate or closed issues.
- I have read the guide for submitting bug reports.
- 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
amplify configure codegen
- Follow prompts to select TypeScript- Add the following definition to schema.graphql
type Todo
@model
{
id: ID!
description: String
}
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:
export declare class Todo {
readonly id: string;
readonly description: string;
readonly createdAt?: string;
readonly updatedAt?: string;
constructor(init: ModelInit<Todo, TodoMetaData>);
static copyOf(source: Todo, mutator: (draft: MutableModel<Todo, TodoMetaData>) => MutableModel<Todo, TodoMetaData> | void): Todo;
}
In src/API.ts:
export type TodoInput = {
id: string,
description: string,
_version?: number | null,
};
When I try to access the _version, I get a TypeScript compile warning:
class GraphqlTodoDAO implements TodoDAO {
public async getTodoById(
todoId: string
): Promise<Todo> {
const result: GraphQLResult<GetTodoQuery> = await API.graphql({
query: getTodo,
variables: {
id: todoId
}
}) as GraphQLResult<GetTodoQuery>;
return result.data?.getTodo as Todo;
}
public async updateTodoDescription(
todoId: string,
newDescription: string
): Promise<Todo> {
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)
# Put schemas below this line
type Todo
@model
{
id: ID!
description: String
}
Log output
# Put your logs below this line
Additional information
No response