Read application data
Query item
Now that you were able to make a mutation, take the Id
that was printed out and use it in your query to retrieve data.
1private void getTodo(String id) {2 Amplify.API.query(3 ModelQuery.get(Todo.class, id),4 response -> Log.i("MyAmplifyApp", ((Todo) response.getData()).getName()),5 error -> Log.e("MyAmplifyApp", error.toString(), error)6 );7}
List items
You can get the list of items that match a condition that you specify in Amplify.API.query
:
1Amplify.API.query(2 ModelQuery.list(Todo.class, Todo.NAME.contains("first")),3 response -> {4 for (Todo todo : response.getData()) {5 Log.i("MyAmplifyApp", todo.getName());6 }7 },8 error -> Log.e("MyAmplifyApp", "Query failure", error)9);
Note: This approach will only return up to the first 1,000 items. To change this limit or make requests for additional results beyond this limit, use pagination as discussed below.
List subsequent pages of items
A list query only returns the first 1,000 items by default, so for large data sets, you'll need to paginate through the results. After receiving a page of results, you can obtain a GraphQLRequest
for requesting the next page, if there are more results available. The page size is configurable as well, as in the example below.
1public void queryFirstPage() {2 query(ModelQuery.list(Todo.class, ModelPagination.limit(1_000)));3}4
5private static void query(GraphQLRequest<PaginatedResult<Todo>> request) {6 Amplify.API.query(7 request,8 response -> {9 if (response.hasData()) {10 for (Todo todo : response.getData()) {11 Log.d("MyAmplifyApp", todo.getName());12 }13 if (response.getData().hasNextResult()) {14 query(response.getData().getRequestForNextResult());15 }16 }17 },18 failure -> Log.e("MyAmplifyApp", "Query failed.", failure)19 );20}