Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DataStore codegen changes for upcoming iOS release #3506

Closed
drochetti opened this issue Feb 24, 2020 · 3 comments
Closed

DataStore codegen changes for upcoming iOS release #3506

drochetti opened this issue Feb 24, 2020 · 3 comments
Assignees
Labels
feature-request Request a new feature

Comments

@drochetti
Copy link
Contributor

drochetti commented Feb 24, 2020

Description

When generating code for GraphQL schemas on iOS, some features are missing, such as enum and non-model types. This ticket highlights the required changes for the upcoming iOS release of Amplify DataStore.

Enums:

Given the GraphQL schema:

enum PostStatus {
    active
    inactive
    draft
}

The expected iOS code is:

import Amplify

public enum PostStatus: String, EnumPersistable {

    case active
    case inactive
    case draft

}

Notes:

  • Enum values in Swift should be camelCase
  • The protocol being added (EnumPersistable)
  • Some enum values might clash with reserved keywords in Swift, in that case it needs to be escaped. To escape in Swift use back-ticks: `public`
  • it's not necessary to register Enums in the ModelRegistry

Non-model types (aka Nested or Embedded types)

For non-model types, the codegen should generate Swift structs that follow the same semantic of the model types minus the Model-specific rules such as : Model and using List<M> for associations (since associations are not supported in non-model types).

type Location {
    latitude: Float!
    longitude: Float!
    tags: [String]
}

The expected iOS code is:

public struct Location: Codable {

    var latitude: Double
    var longitude: Double
    var tags: [String]

}

Notes:

  • reuse all the types from models but:
    • doesn't conform to Model
    • conforms to Codable
  • never use List<M>
  • models cannot be used as property types of non-model types (I believe this is enforced at the transformer level already, is that correct?)

Schema definition

On the schema definition (e.g. Post+Schema.swift) when dealing with non-model types and arrays of non-model/scalar types the definition should be like:

.customType(Location.self)
.customType([String].self)

// complete definition of a field:

.field(example.nonModelField, is: .required, ofType: .customType(ExampleNonModelType.self))

// .collection(of) is only used for Model types
.field(example.arrayOfStringsField, is: .required, ofType: .customType([String].self))

Related issues:

Edit: added information about how to define the schema for the non-model types

Edit 2:

  • final name for the enum protocol: EnumPersistable
  • final name for non-model types and arrays of scalars/types: .customType (code samples above updated accordingly)
@drochetti
Copy link
Contributor Author

The iOS PR aws-amplify/amplify-swift#334 landed in master. The names were updated in the original issue and reflect the final names.

@yuth
Copy link
Contributor

yuth commented Mar 26, 2020

Change have been released in Amplify CLI v 4.17.2

@github-actions
Copy link

This issue has been automatically locked since there hasn't been any recent activity after it was closed. Please open a new issue for related bugs.

Looking for a help forum? We recommend joining the Amplify Community Discord server *-help channels for those types of questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 26, 2021
@josefaidt josefaidt added feature-request Request a new feature and removed enhancement labels Sep 3, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feature-request Request a new feature
Projects
None yet
Development

No branches or pull requests

4 participants