Name:
interface
Value:
Amplify has re-imagined the way frontend developers build fullstack applications. Develop and deploy without the hassle.
Gen1 DocsLegacy

Page updated Mar 26, 2026

アプリケーションデータの読み取り

IDでクエリする

変更を実行できたので、出力されたIDを使用してクエリを実行してデータを取得します。

func getTodo() async {
do {
let result = try await Amplify.API.query(
request: .get(Todo.self, byId: "9FCF5DD5-1D65-4A82-BE76-42CB438607A0")
)
switch result {
case .success(let todo):
guard let todo = todo else {
print("Could not find todo")
return
}
print("Successfully retrieved todo: \(todo)")
case .failure(let error):
print("Got failed result with \(error.errorDescription)")
}
} catch let error as APIError {
print("Failed to query todo: ", error)
} catch {
print("Unexpected error: \(error)")
}
}
func getTodo() -> AnyCancellable {
let sink = Amplify.Publisher.create {
try await Amplify.API.query(
request: .get(Todo.self, byId: "9FCF5DD5-1D65-4A82-BE76-42CB438607A0")
)
}
.sink {
if case let .failure(error) = $0 {
print("Got failed event with error \(error)")
}
}
receiveValue: { result in
switch result {
case .success(let todo):
guard let todo = todo else {
print("Could not find todo")
return
}
print("Successfully retrieved todo: \(todo)")
case .failure(let error):
print("Got failed result with \(error.errorDescription)")
}
}
return sink
}

リストクエリ

オプションのパラメータlimitwhereを使用して.listでアイテムのリストを取得し、ページサイズと条件を指定できます。デフォルトのページサイズは1000です。

func listTodos() async {
let todo = Todo.keys
let predicate = todo.name == "my first todo" && todo.description == "todo description"
let request = GraphQLRequest<Todo>.list(Todo.self, where: predicate, limit: 1000)
do {
let result = try await Amplify.API.query(request: request)
switch result {
case .success(let todos):
print("Successfully retrieved list of todos: \(todos)")
case .failure(let error):
print("Got failed result with \(error.errorDescription)")
}
} catch let error as APIError {
print("Failed to query list of todos: ", error)
} catch {
print("Unexpected error: \(error)")
}
}
func listTodos() -> AnyCancellable {
let todo = Todo.keys
let predicate = todo.name == "my first todo" && todo.description == "todo description"
let request = GraphQLRequest<Todo>.list(Todo.self, where: predicate, limit: 1000)
let sink = Amplify.Publisher.create {
try await Amplify.API.query(request: request)
}
.sink {
if case let .failure(error) = $0 {
print("Got failed event with error \(error)")
}
}
receiveValue: { result in
switch result {
case .success(let todos):
print("Successfully retrieved list of todos: \(todos)")
case .failure(let error):
print("Got failed result with \(error.errorDescription)")
}
}
return sink
}

後続ページのアイテムをリストする

SwiftUIを使用していてSwiftUIが同じコードファイルにインポートされている場合、SwiftUI.Listとの名前の衝突を解決するために、クラスAmplify.Listをインポートする必要があります:

import SwiftUI
import Amplify
import class Amplify.List

大規模なデータセットの場合、結果をページネーションする必要があります。最初のページの結果を受け取った後、後続のページがあるかどうかを確認し、次のページを取得できます。

var todos: [Todo] = []
var currentPage: List<Todo>?
func listFirstPage() async {
let todo = Todo.keys
let predicate = todo.name == "my first todo" && todo.description == "todo description"
let request = GraphQLRequest<Todo>.list(Todo.self, where: predicate, limit: 1000)
do {
let result = try await Amplify.API.query(request: request)
switch result {
case .success(let todos):
print("Successfully retrieved list of todos: \(todos)")
self.currentPage = todos
self.todos.append(contentsOf: todos)
case .failure(let error):
print("Got failed result with \(error.errorDescription)")
}
} catch let error as APIError {
print("Failed to query list of todos: ", error)
} catch {
print("Unexpected error: \(error)")
}
}
func listNextPage() async {
if let current = self.currentPage, current.hasNextPage() {
do {
let todos = try await current.getNextPage()
self.todos.append(contentsOf: todos)
self.currentPage = todos
} catch {
print("Failed to get next page \(error)")
}
}
}

すべてのページをリストする

すべてのページを取得したい場合は、最初または次のページを正常に取得したときに後続のページを取得します。

  1. 上記のメソッドlistFirstPage()listAllPages()に更新します
  2. listAllPages()のクエリの成功ブロックでlistNextPageRecursively()を呼び出します
  3. listNextPage()listNextPageRecursively()に更新します
  4. listNextPageRecursively()のクエリの成功ブロックでlistNextPageRecursively()を呼び出します

完成した変更は次のようになります:

var todos: [Todo] = []
var currentPage: List<Todo>?
func listAllPages() async { // 1. `listFirstPage()`から更新
let todo = Todo.keys
let predicate = todo.name == "my first todo" && todo.description == "todo description"
let request = GraphQLRequest<Todo>.list(Todo.self, where: predicate, limit: 1000)
do {
let result = try await Amplify.API.query(request: request)
switch result {
case .success(let todos):
print("Successfully retrieved list of todos: \(todos)")
self.currentPage = todos
self.todos.append(contentsOf: todos)
await self.listNextPageRecursively() // 2. 追加
case .failure(let error):
print("Got failed result with \(error.errorDescription)")
}
} catch let error as APIError {
print("Failed to query list of todos: ", error)
} catch {
print("Unexpected error: \(error)")
}
}
func listNextPageRecursively() async { // 3. `listNextPage()`から更新
if let current = currentPage, current.hasNextPage() {
do {
let todos = try await current.getNextPage()
self.todos.append(contentsOf: todos)
self.currentPage = todos
await self.listNextPageRecursively() // 4. 追加
} catch {
print("Failed to get next page \(error)")
}
}
}