The Amplify API category provides an interface for retrieving and persisting your model data. The API category comes with default built-in support for AWS AppSync. The Amplify CLI allows you to define your API and provision a GraphQL service with CRUD operations and real-time functionality.
GoalTo setup and configure your application with Amplify API to save items in the backend.
PrerequisitesAn application with Amplify libraries integrated and a minimum target of any of the following:
For a full example, please follow the project setup walkthrough.
visionOS support is currently in preview and can be used by using the latest Amplify Release. As new Xcode and visionOS versions are released, the support will be updated with any necessary fixes on a best effort basis.
Configure APITo start provisioning API resources in the backend, go to your project directory and execute the command:
Enter the following when prompted:
? Please select from one of the below mentioned services:
# The part below will show you some options you can change, if you wish to change any of them you can navigate with
# your arrow keys and update any field, otherwise you can click on `Continue` to move on
? Here is the GraphQL API that we will create. Select a setting to edit or continue
? Choose a schema template:
`Single object with fields (e.g., âTodoâ with ID, name, description)`
? Do you want to edit the schema now?
Troubleshooting: The AWS API plugins do not support conflict detection. If AppSync returns errors about missing _version
and _lastChangedAt
fields, or unhandled conflicts, disable conflict detection. Run amplify update api
, and choose Disable conflict detection. If you started with the Getting Started guide, which introduces DataStore, this step is required.
The guided schema creation will output amplify/backend/api/{api_name}/schema.graphql
containing the following:
To push your changes to the cloud, execute the command:
Upon completion, amplifyconfiguration.json
will be updated to reference provisioned backend resources. Note that this file should already be a part of your project if you followed the Project setup walkthrough.
To generate the Todo
model, change directories to your project folder and execute the command:
The generated files will be under the amplify/generated/models
directory. It is strongly advised not to put any hand written code in amplify/generated
directory as it gets re-generated each time codegen is run.
Drag and place the entire models
directory into Xcode to add them to your project.
To install Amplify Libraries in your application, open your project in Xcode and select File > Add Packages....
Enter the Amplify Library for Swift GitHub repo URL (https://github.com/aws-amplify/amplify-swift
) into the search bar and click Add Package.
Note: Up to Next Major Version should be selected from the Dependency Rule dropdown.
To initialize the Amplify API category, you are required to use the Amplify.add()
method for the plugin followed by calling Amplify.configure()
.
Create a custom AppDelegate
with the following imports and application:didFinishLaunchingWithOptions
method:
Then in your App
scene, use the UIApplicationDelegateAdaptor
property wrapper to use your custom AppDelegate
:
Add the following imports to the top of your AppDelegate.swift
file:
Add the following code to the AppDelegate's application:didFinishLaunchingWithOptions
method:
Upon building and running this application you should see the following in your console window:
Amplify configured with API plugin
Create a Todo
Make sure you have the additional import at the top of your file
Upon successfully executing this code, you should see an instance of todo
persisted in your dynamoDB table.
To navigate to your backend, run amplify console api
and choose GraphQL
. This will open the AppSync console to your GraphQL service. Select Data Sources
and select the Resource link in your TodoTable
to bring you to the DynamoDB Console. Select the items
tab to see the Todo
object that has been persisted in your database.
Congratulations! You've created a Todo
object in your database. Check out the following links to see other Amplify API use cases:
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