amplify-category-api icon indicating copy to clipboard operation
amplify-category-api copied to clipboard

Restrict input shape for mutations (eg. prevent submitting Todo.id in create mutation)

Open naedx opened this issue 1 year ago • 1 comments

Describe the feature you'd like to request

Given the following schema:


const schema = a.schema({
  Todo: a.model({
    todoId: a.id(),
    updatesCount: a.number().default(0),
    content: a.string(),
    completed: a.boolean(),
  })
  .identifier(['todoId'])
  .authorization(allow => [allow.publicApiKey()]),
});

I would like to be able to configure the input shape for a model's mutation.

Specifically, I would like to remove values from the allowed arguments. For instance, since Todo.todoId and Todo.updatesCount are autogenerated I do not want a user to be able to set this client side.

Describe the solution you'd like


const schema = a.schema({
  Todo: a.model({
    todoId: a.id().setServerSideOnly(),
    updatesCount: a.number().default(0).setServerSideOnly(),
    content: a.string(),
    completed: a.boolean(),
  })
  .identifier(['todoId'])
  .authorization(allow => [allow.publicApiKey()]),
});

In the example above .setServerSideOnly() specifies that the value cannot be part of the input CreateTodoInput. The same should apply for mutations except that it must allow key values to be passed in.

Describe alternatives you've considered

In Gen1 I would define my own input type and exclude the values I do not want to be set by the user:

input CreateTodoInput {
  content: String
  completed: bool
}

In Gen2 I would have to avoid using a.schema() and write the entire schema by hand just to achieve this.

Additional context

No response

Is this something that you'd be interested in working on?

  • [ ] 👋 I may be able to implement this feature request

Would this feature include a breaking change?

  • [ ] ⚠️ This feature might incur a breaking change

naedx avatar Jul 27 '24 11:07 naedx