Closed SanjoSolutions closed 3 weeks ago
Hi @SanjoSolutions 👋 the relation redaction behavior is not a bug, it is intentional in Gen 2. The way to get around this with graphql queries and mutations created with codegen is to generate them with a max statement depth of 1 so that the selection set only includes top level fields and not nested relational data fields in the mutations. The default is 2.
ampx generate graphql-client-code --statement-max-depth=1
you can also manually remove the user fields from the selection set in this file to test.
Thanks for the answer.
With a different authentication it seems to return the user. See branch https://github.com/SanjoSolutions/amplify-create-with-relation-bug-reproduce/tree/here-it-works, commit https://github.com/SanjoSolutions/amplify-create-with-relation-bug-reproduce/commit/332dd9066e5c2efe559f23b1df0438dbe09d6659#diff-590c4139982e36911299b1417410e032b5a841ef4b4042513473bcfce4239ce4.
So it's really just intentionally not returning related data when the authentication is set to allow.custom()
?
I'm not sure if the only issue is the authorization rule. The error wasn't that you're unauthorized to access the data, just that the mutation was returning non-nullable fields in the selection set.
The redaction behavior depends on the auth rules and whether it can be determined that it won't change between requests. A custom auth rule means that we can't make that determination, so redaction is enabled.
but that error you got specifically happens when you don't get back fields that are non-nullable, which is just a possible symptom of the redaction behavior but can happen in other instances, especially if you create your own GraphQL queries.
Here's the page where this behavior is explained in a warning:
https://docs.amplify.aws/react/build-a-backend/data/data-modeling/relationships/
You can test if the redaction behavior is present in the resolver by checking the AppSync console for the mutation's resolver logic. Change the auth rule, redeploy, and check to see if the resolver logic changed.
Ok, thanks for the explanation.
In the project, where I have experienced this behavior first, I have written a custom GraphQL query which just queries for the fields that were required.
Before opening, please confirm:
JavaScript Framework
Next.js
Amplify APIs
Authentication, GraphQL API, DataStore
Amplify Version
v6
Amplify Categories
auth, function, api
Backend
Amplify Gen 2
Environment information
Describe the bug
In the repro project: when creating a todo from a lambda function, in the returned data the user field is null and the GraphQL client seems to throw an error that required fields of the user are missing.
Expected behavior
The user field is populated with data by the GraphQL API.
Reproduction steps
Install the repro project:
Reproduce by opening http://localhost:3000
Check the CloudWatch logs of the createTodo2 function. There should be an error like this:
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