Skip to content

Unable to perform update and delete mutations as Codegen doesn't generate _version attribute on model types #338

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

Open
4 tasks done
dsharygin opened this issue Dec 22, 2021 · 0 comments
Labels
feature-request New feature or request model-gen Issues on datastore model generation p4

Comments

@dsharygin
Copy link

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

  1. amplify configure codegen - Follow prompts to select TypeScript
  2. Add the following definition to schema.graphql
type Todo
  @model
{
  id: ID!
  description: String
}
  1. 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

@frimfram frimfram added enhancement pending-triage Issues that need further discussion to determine labels Dec 28, 2021
@phani-srikar phani-srikar added bug Something isn't working model-gen Issues on datastore model generation and removed pending-triage Issues that need further discussion to determine labels Mar 29, 2022
@alharris-at alharris-at added enhancement p4 and removed bug Something isn't working labels Apr 5, 2022
@dpilch dpilch added feature-request New feature or request and removed enhancement labels Apr 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature-request New feature or request model-gen Issues on datastore model generation p4
Projects
None yet
Development

No branches or pull requests

5 participants