aws-amplify / amplify-category-api

The AWS Amplify CLI is a toolchain for simplifying serverless web and mobile development. This plugin provides functionality for the API category, allowing for the creation and management of GraphQL and REST based backends for your amplify project.
https://docs.amplify.aws/
Apache License 2.0
88 stars 76 forks source link

@default directive to work even when field level resolver do have create permission #1988

Open sushpatg opened 10 months ago

sushpatg commented 10 months ago

Describe the feature you'd like to request

Sample Schema

  type Todo @model @auth(rules: [{allow: owner, identityClaim: "sub", operations: [read,create]}]) {
  id: ID! @primaryKey @auth(rules: [{allow: owner, identityClaim: "sub", operations: [read, create, update]}])
owner: String! @auth(rules: [{allow: owner, identityClaim: "sub", operations: [read, create]}])
  status: String @default(value: "test") @auth(rules: [{allow: owner, identityClaim: "sub", operations: [read]}])
}

Use-Case: Users should be able to create records and the status filed should populate with default value. The restriction is that users, should not be able to pass or update the status value.

The above auth rules are failing with "Unauthorized on [status]" error.

Describe the solution you'd like

To be able to populate the default value to status filed without giving create permission

Describe alternatives you've considered

Current work around: Override Amplify-generated resolvers to update the Mutation.createTodo.init.2.req.vtl resolver to return unauthorized if id value is passed

## [Start] Setting "status" to default value of "test". **
#if( $util.isNull($context.args.input.status))
$util.qr($context.args.input.put("status", $util.defaultIfNull($ctx.args.input.status, "test")))
#else
$util.unauthorized()
#end
## [End] Setting "status" to default value of "test". **
{}

And also adding "status" to ownerAllowedFields0 in Mutation.createClaim.auth.1.req.vtl

Additional context

Similar Github Issue: 9846

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

Would this feature include a breaking change?

AnilMaktala commented 10 months ago

Hi @sushpatg, Thank you for raising this. We will categorize this as a feature request for our team to investigate and assess more thoroughly.