- Newest
- Most votes
- Most comments
In AWS Amplify Gen 2, the distinction between queries and mutations is based on their purpose and effect on backend data:
- Queries are used when you only need to read data and will not modify any backend data.
- Mutations are used when your operation will modify backend data.
In your specific case, the function addUserToGroup
suggests that you're modifying data by adding a user to a group. Based on this naming convention, it would typically be more appropriate to use a mutation rather than a query.
However, the fact that changing from .mutation()
to .query()
fixed your issue suggests there might be something in the implementation of the addUserToGroup
function that aligns better with query behavior. Without seeing the implementation details of that function, it's difficult to pinpoint exactly why the query worked while the mutation failed.
When building schemas for mutations and queries in Amplify Gen 2, keep these considerations in mind:
- Use queries for read-only operations
- Use mutations for operations that create, update, or delete data
- Both queries and mutations require a return type and can optionally have arguments
- Both can be configured with custom business logic handlers
If your operation is truly modifying data by adding a user to a group, you might want to investigate why the mutation isn't working rather than using a query as a workaround, as this could lead to confusion for other developers working with your code.
Sources
Add custom queries and mutations - AWS Amplify Gen 2 Documentation
Add custom queries and mutations - AWS Amplify Gen 2 Documentation
Add custom queries and mutations - AWS Amplify Gen 2 Documentation
Add custom queries and mutations - AWS Amplify Gen 2 Documentation
Relevant content
- asked a year ago
- asked a year ago
- AWS OFFICIALUpdated 4 years ago
This is not useful, as I am simply following the tutorial from AWS Amplify documents, the handler implementation is exactly the same: