Closed jdanielsyeah closed 2 years ago
Hi @jdanielsyeah 👋 thanks for raising this issue. This is definitely confusing behavior but one thing that stands out to me is that you are using dynamic group field auth rules. DataStore does not support dynamic auth rules, only static.
Here are the supported auth rules for DataStore: https://docs.amplify.aws/lib/datastore/setup-auth-rules/q/platform/js
This probably isn't the cause of the error but is worth pointing out so you can adjust the schema accordingly.
Thanks for the prompt reply @chrisbonifacio . And thanks for pointing me to that documentation. I'd encountered it before but had a suspicion/hope that I might still be able to get some functionality from DataStore with the dynamic auth rules. The reason being that the Amplify Cli documentation: User group-based data access states "Known limitation: Real-time subscriptions are not supported for dynamic group authorization." (ie, suggesting that other features might work?) - but on attempting to use it I am finding that if I test deleting something (in a test intended to fail since I have set my @auth rules to prevent deletions), DataStore will delete items locally (contrary to my @auth rules) but then fail (401 - as per my rules) to delete them remotely. This suggests the rules are not taking effect on DataStore properly.
I would like to follow your suggestion to update the Schema, but I think it's too limited for the use-case we have. We want to retain one, single database table so that it's easier/simpler to retain records and review update history. In order to achieve this, I don't see any other way than having dynamic auth rules in place to prevent certain user groups from editing records. The only options Amplify seems to offer is having multiple tables with different auth rules, or the same table with static rules.
No problem!
A couple things:
Regarding the behavior where DataStore will delete records locally but the same records persist on the server - DataStore cannot enforce auth rules or certain types of validation on the client so it will manipulate the local store as such. Mutation errors can be caught by the errorHandler if that information would be useful. Otherwise, the locally deleted record would be synced back down from the server again.
I agree that DataStore's supported auth rules do not fit your use case, but you could also leverage the API.graphql library directly which does support dynamic auth. Unless you also require offline capabilities, I would explore that route.
Lastly, I'm curious what authorization mode you might've been using when attempting the mutation in the AppSync console? Was it IAM or Cognito User Pools?
I was using Cognito User Pools within the AppSync console. It seems to work well with the Dynamic Group Authorization rules.
I'm grateful to get your clarification. I thought I might ask for one more: I've been trying to find how to remove DataStore from my front-end (NextJS) and have only found the following instructions on this github issue:
_1. Type in terminal 'amplify update api'
I just wanted to check this was the correct method before I ran it, because I couldn't find these commands detailed in any documentation?
That seems to be correct.
amplify update api -> GraphQL -> Disable conflict detection -> amplify push
Going to close this issue as resolved, but for anyone that might run into this error in the future - please note that nullable fields in the schema should be set to null
instead of undefined
from the client side, whether using DataStore or API.graphql.
Before opening, please confirm:
JavaScript Framework
Next.js
Amplify APIs
Authentication, DataStore
Amplify Categories
auth, api
Environment information
Describe the bug
AppSync is throwing 401 errors at confusing (and I suspect inappropriate times). I have a graphql schema:
When I try to update in the AppSync console with this:
The above works successfully, as expected.
When I replace
0
withundefined
I get the following error:"Validation error of type BadValueForDefaultArg: Bad default value EnumValue{name='undefined'} for type AWSTimestamp"
. This makes sense to me.When I try the above and replace
0
withnull
I get the following error:"Unauthorized on [approvedTime]"
. This is confusing.What is extra confusing, is if I update the Card object in the front-end using DataStore as follows:
This throws a 401 in my console. The network response reads:
Whereas if I set
updated.approvedTime = null
the update works fine. This is the opposite behaviour to what's happening in the AppSync console - which is confusing on it's own. What is additionally confusing is that this isn't an issue that can be fixed by updating authorization - this is an error with bad data inputs. I've just spent hours testing different @auth rules in my schema when the issue was with my js code all along.Expected behavior
AWS suggests that a 401 should occur because:: "The request is denied by either AWS AppSync or the authorization mode because the credentials are missing or invalid. (...)"
It would be more useful if I had received the same error when I entered
undefined
in the AppSync console instead of any of the 401s that I received.Reproduction steps
Code Snippet
Log output
aws-exports.js
No response
Manual configuration
No response
Additional configuration
No response
Mobile Device
No response
Mobile Operating System
No response
Mobile Browser
No response
Mobile Browser Version
No response
Additional information and screenshots
No response