Getting Started with AWS AppSync for iOS
Learn how to consume GraphQL APIs in your SwiftUI iOS apps in a simple and type-safe way using AWS AppSync framework. By Alex Brown.
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Contents
Getting Started with AWS AppSync for iOS
30 mins
- Getting Started
- About GraphQL and AppSync
- What is GraphQL?
- GraphQL with AWS AppSync
- Installing the Amplify Framework
- Installing npm
- Installing Amplify CLI
- Installing CocoaPods
- Adding Amplify to the Project
- Adding AppSync Script
- Initializing Amplify
- Creating Models Using GraphQL
- Using Amplify in the App
- Building the To Do List UI
- Adding Rows to the To Do List
- Setting up Your Data
- Adding Sections
- Adding a To Do
- Creating and Editing To Dos
- Adding To Dos
- Completing To Dos
- Deleting To Dos
- Where to Go From Here?
Deleting To Dos
The final thing you need add is a way to delete rows. Swipe-to-delete already exists in the UI, so you just need to wire it up.
Open TodoListViewModel.swift, and you’ll notice three delete methods at the top. These will act as helpers to delete to do items from their respective list.
Add the following method:
func delete(todo: Todo) {
Amplify.DataStore.delete(todo) { result in
switch result {
case .success:
print("Deleted item: \(todo.name)")
case .failure(let error):
print("Could not update data with error: \(error)")
}
}
}
This method deletes the model from the data store by calling delete(_:)
from the Amplify framework.
Next, replace the three delete methods above with the following:
// 1
func deleteItems(at offsets: IndexSet, from todoList: inout [Todo]) {
for index in offsets {
let todo = todoList[index]
delete(todo: todo)
}
todoList.remove(atOffsets: offsets)
}
// 2
func deleteTodos(at offsets: IndexSet) {
deleteItems(at: offsets, from: &todos)
}
// 3
func deleteCompletedTodos(at offsets: IndexSet) {
deleteItems(at: offsets, from: &completedTodos)
}
Here’s what you’ve done:
- The first delete method calls
delete(at:from:)
, which you just added. - This method routes the call to delete with the
todos
array. - This method routes the call to delete with the
completedTodos
array.
Build and run the project. You can now swipe to delete todos!
You now have a to do list that allows you to add, edit and delete to dos. It works offline and stays synchronized to an AWS back end.
Where to Go From Here?
You can download the completed version of the project using the Download Materials button at the top or bottom of this tutorial.
You now know the basics of integrating and using AppSync with Amplify in your iOS apps — but there’s a lot more to learn! GraphQL can do much more than what you’ve covered here.
When you’re ready for your next steps with AWS and Amplify, check out Using AWS as a Back End: Authentication & API and Using AWS as a Back End: The Data Store API.
Check out our tutorial GraphQL Using the Apollo Framework: Getting Started to see more practical examples. You can also learn a lot more about GraphQL on the official GraphQL website.
You should also look at the official AWS AppSync tutorials Amazon has produced for further learning.
If you have any questions, please join the discussion in the forum below!