In this guide, you will learn how to create, update, and delete your data using Amplify Libraries' GraphQL client.
Before you begin, you will need:
Run mutations to create, update, and delete application dataIn GraphQL, mutations are APIs that are used to create, update, or delete data. This is different than queries, which are used to read the data but not change it. The following examples demonstrate how you can create, update, and delete items using the Amplify GraphQL client.
Create an itemYou can create an item by first importing the API and mutations. Then you can add an item:
You should see the item created: Learn AWS AppSync
.
Note: You do not need to specify the createdAt
field. Amplify will automatically populate this field for you.
To update the item, use the GraphQL update mutation:
Notes:
updatedAt
field. Amplify will automatically populate this field for you.error
field returned by the query. In GraphQL, errors are not thrown like exceptions in other languages. Instead, any errors are captured and returned as part of the query result in the error
field.You can then delete the Todo by using the delete mutation. To specify which item to delete, you only need to provide the id
of that item:
Note: Join table records must be deleted before deleting the associated records. For example, for a many-to-many relationship between Posts and Tags, delete the PostTags join record before deleting a Post or Tag.
Learn more
Custom authorization mode to mutate data
Each AWS AppSync API uses a default authorization mode when you configure your app. If you get unauthorized errors, you may need to update your authorization mode. To override this default, pass an authMode
property. The following examples show how you can mutate data with a custom authorization mode:
You can cancel any GraphQL API request by calling .cancel
on the GraphQL request promise that's returned by client.graphql(...)
.
You need to ensure that the promise returned from client.graphql()
has not been modified. Typically, async functions wrap the promise being returned into another promise. For example, the following will not work:
Congratulations! You have finished the Create, update, and delete application data guide. In this guide, you created, updated, and deleted your app data through the GraphQL API.
Next stepsOur recommended next steps include using the API to query data and subscribe to real-time events to look for mutations in your data. Some resources that will help with this work include:
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4