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, you need to edit the schema.graphql file. To find it, first navigate to your project folder and then into amplify/backend/api/amplifyDatasource/schema.graphql.

    Open the file with any text editor and 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 description: String
    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: an optional enumeration type field that indicates the importance of a Todo item; its only available values being LOW, NORMAL, or HIGH
    • description: an optional string field that holds more information about a Todo item
  2. Next, you need to generate the Swift objects for these models by running this command on the terminal:

1amplify codegen models

The generated models will be added to your project under the AmplifyModels group.

  1. Finally, open Xcode and build (Cmd+b) your project to compile the newly generated files.