Page updated Nov 10, 2023

Generate model files

With the basic setup complete, next you will model the data that your application will store. Amplify DataStore will use this model to persist data to your local device that will be synchronized to a backend API without writing any additional code.

These models are specified as GraphQL schemas. If you'd like, first learn more about GraphQL schemas and data modeling.

  1. To begin, run the command:
1amplify add api

Enter the following when prompted:

1? Select from one of the below mentioned services:
2 `GraphQL`
3? Here is the GraphQL API that we will create. Select a setting to edit or continue
4 `Conflict detection (required for DataStore): Disabled`
5? Enable conflict detection?
6 `Yes`
7? Select the default resolution strategy
8 `Auto Merge`
9? Here is the GraphQL API that we will create. Select a setting to edit or continue
10 `Continue`
11? Choose a schema template:
12 `Single object with fields (e.g., “Todo” with ID, name, description)`
13? Do you want to edit the schema now?
14 `Yes`

This will open a new window. Replace its contents with the following schema:

1enum Priority {
2 LOW
3 NORMAL
4 HIGH
5}
6
7type Todo @model @auth(rules: [{ allow: public }]) {
8 id: ID!
9 name: String!
10 priority: Priority!
11 completedAt: AWSDateTime
12}

This schema creates a model called Todo with four properties:

  • id: an auto-generated identifier field for a Todo item
  • name: a non-optional string field that is the title of the Todo item
  • priority: a non-optional enumeration type field that indicates the importance of a Todo item; its only available values being LOW, NORMAL, or HIGH
  • completedAt: an optional AWSDateTime field that holds the time of completion of a Todo item
  1. Next, you need to generate the Java objects for these models by running this command in the terminal:
1amplify codegen models

The generated models will be added to your project at app/src/main/java/com/amplifyframework/datastore/generated/model