Skip to content
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

Cannot update optional fields to Null to remove them. #2562

Closed
lawmicha opened this issue May 16, 2024 · 6 comments
Closed

Cannot update optional fields to Null to remove them. #2562

lawmicha opened this issue May 16, 2024 · 6 comments
Labels
api-graphql question Further information is requested

Comments

@lawmicha
Copy link

Amplify CLI version 12.10.3

Schema

type User2 @model @auth(rules: [{allow: public, operations: [read]}, {allow: private, operations: [read]}, {allow: owner, operations: [create, update]}]) {
    id: ID!
    requiredName: String!
    optionalName: String
} 

Semd createUser2 request, with the user pool user signed in ("michael" user)

mutation CreateUser {
  createUser2(input: {requiredName: "1", optionalName: "2"}) {
    createdAt
    optionalName
    id
    owner
    requiredName
    updatedAt
  }
}

Response

{
  "data": {
    "createUser2": {
      "createdAt": "2024-05-16T16:01:13.753Z",
      "optionalName": "2",
      "id": "a74354c6-afa4-4f8f-8861-79b2f25cd9d7",
      "owner": "michael",
      "requiredName": "1",
      "updatedAt": "2024-05-16T16:01:13.753Z"
    }
  }
}

Let's say now I want to remove the value set for optionalName. Perform an update on the user with optionalName set to null.

mutation UpdateOptionalFieldToNull {
  updateUser2(input: {id: "a74354c6-afa4-4f8f-8861-79b2f25cd9d7", optionalName: null}) {
    createdAt
    id
    optionalName
    owner
    requiredName
    updatedAt
  }
}

Response

{
  "data": {
    "updateUser2": null
  },
  "errors": [
    {
      "path": [
        "updateUser2"
      ],
      "data": null,
      "errorType": "Unauthorized",
      "errorInfo": null,
      "locations": [
        {
          "line": 29,
          "column": 3,
          "sourceName": null
        }
      ],
      "message": "Unauthorized on [optionalName]"
    }
  ]
}

The issue is that I cannot update the user to remove an optional string value, whether it was originally set to something or not.

Originally posted by @lawmicha in aws-amplify/amplify-swift#3677 (comment)

@lawmicha
Copy link
Author

lawmicha commented May 16, 2024

is this because operations: [create, update]} does not include delete so the customer cannot remove fields that were set? or is the delete operation allows access to deleteUser operation?

@palpatim
Copy link
Member

tl;dr: You need to give the owner delete permissions on the field while continuing to restrict delete on the model.

See #2487 (comment) for longer discussion.

@palpatim palpatim added question Further information is requested pending-response and removed bug Something isn't working labels May 17, 2024
@lawmicha
Copy link
Author

Thanks @palpatim, this was a customer from aws-amplify/amplify-swift#3677 @dan-codes1 let us know if you have anymore questions

@dan-codes1
Copy link

I understand the issue now. Thanks @lawmicha

@AnilMaktala
Copy link
Member

Thank you for confirming @dan-codes1. We are closing the issue.

Copy link

This issue is now closed. Comments on closed issues are hard for our team to see.
If you need more assistance, please open a new issue that references this one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api-graphql question Further information is requested
Projects
None yet
Development

No branches or pull requests

4 participants