-
Notifications
You must be signed in to change notification settings - Fork 1.1k
fix(data): swift code snippets for customize your data model #7593
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -87,6 +87,8 @@ a.schema({ | |
}); | ||
``` | ||
|
||
<InlineFilter filters={["javascript", "angular", "react-native", "react", "nextjs", "vue", "android", "flutter"]}> | ||
|
||
To set or read the location field on the client side, you can expand a nested object and the type system will auto-infer the allowed values. | ||
|
||
```ts | ||
|
@@ -100,8 +102,28 @@ const { data: newPost, errors } = await client.models.Post.create({ | |
console.log(newPost?.location?.lat, newPost?.location?.long); | ||
``` | ||
|
||
</InlineFilter> | ||
|
||
<InlineFilter filters={["swift"]}> | ||
|
||
To set or read the location field on the client side, you can create a Post object with the location values. | ||
|
||
```swift | ||
let post = Post( | ||
location: .init( | ||
lat: 48.837006, | ||
long: 8.28245)) | ||
let createdPost = try await Amplify.API.mutate(request: .create(post)).get() | ||
print("\(createdPost)") | ||
``` | ||
|
||
</InlineFilter> | ||
Comment on lines
+107
to
+120
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
||
## Specify an enum field type | ||
Enum has a similar developer experience as custom types: short-hand and long-form approaches. To make building a dropdown UI easier, you can also retrieve all available enum options from your client code. | ||
|
||
Enum has a similar developer experience as custom types: short-hand and long-form approaches. | ||
|
||
Short-hand approach | ||
|
||
```ts | ||
a.schema({ | ||
|
@@ -112,6 +134,8 @@ a.schema({ | |
}); | ||
``` | ||
|
||
Long-form approach | ||
|
||
```ts | ||
a.schema({ | ||
PrivacySetting: a.enum([ | ||
|
@@ -131,6 +155,8 @@ a.schema({ | |
}); | ||
``` | ||
|
||
<InlineFilter filters={["javascript", "angular", "react-native", "react", "nextjs", "vue", "android", "flutter"]}> | ||
|
||
When creating a new item client-side, the enums are also type-enforced: | ||
```ts | ||
client.models.Post.create({ | ||
|
@@ -143,6 +169,23 @@ client.models.Post.create({ | |
}); | ||
``` | ||
|
||
</InlineFilter> | ||
|
||
<InlineFilter filters={["swift"]}> | ||
|
||
Creating a new item client-side with the enum value: | ||
|
||
```swift | ||
let post = Post( | ||
content: "hello", | ||
privacySetting: .private) | ||
let createdPost = try await Amplify.API.mutate(request: .create(post)).get() | ||
``` | ||
|
||
</InlineFilter> | ||
Comment on lines
+174
to
+185
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
||
<InlineFilter filters={["javascript", "angular", "react-native", "react", "nextjs", "vue"]}> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I found this section only relevant for the TS client, by having this inline filter, removes it from swift/android/flutter |
||
|
||
### List enum values client-side | ||
|
||
You can list available enum values client-side using the `client.enums.<ENUM_NAME>.values()` API. For example, this allows you to display the available enum values within a dropdown UI. | ||
|
@@ -152,6 +195,8 @@ const availableSettings = client.enums.PrivacySetting.values() | |
// availableSettings returns ["PRIVATE", "FRIENDS_ONLY", "PUBLIC"] | ||
``` | ||
|
||
</InlineFilter> | ||
|
||
## Mark fields as required | ||
|
||
By default, fields are optional. To mark a field as required, use the `.required()` modifier. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,13 +41,29 @@ const schema = a.schema({ | |
}); | ||
``` | ||
|
||
<InlineFilter filters={["javascript", "angular", "react-native", "react", "nextjs", "vue", "android", "flutter"]}> | ||
|
||
```ts | ||
const client = generateClient<Schema>(); | ||
|
||
const todo = await client.models.Todo.create({ content: 'Buy Milk', completed: false }); | ||
console.log(`New Todo created: ${todo.id}`); // New Todo created: 5DB6B4CC-CD41-49F5-9844-57C0AB506B69 | ||
``` | ||
|
||
</InlineFilter> | ||
|
||
<InlineFilter filters={["swift"]}> | ||
|
||
```swift | ||
let todo = Todo( | ||
content: "Buy Milk", | ||
completed: false) | ||
let createdTodo = try await Amplify.API.mutate(request: .create(todo)).get() | ||
print("New Todo created: \(createdTodo)") | ||
``` | ||
|
||
</InlineFilter> | ||
Comment on lines
+55
to
+65
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
||
If you want, you can use Amplify Data to define single-field and composite identifiers: | ||
- Single-field identifier with a consumer-provided value (type: `id` or `string`, and must be marked `required`) | ||
- Composite identifier with a set of consumer-provided values (type: `id` or `string`, and must be marked `required`) | ||
|
@@ -66,12 +82,29 @@ const schema = a.schema({ | |
.identifier(['todoId']) | ||
.authorization(allow => [allow.publicApiKey()]), | ||
}); | ||
``` | ||
|
||
<InlineFilter filters={["javascript", "angular", "react-native", "react", "nextjs", "vue", "android", "flutter"]}> | ||
```ts | ||
const client = generateClient<Schema>(); | ||
|
||
const { data: todo, errors } = await client.models.Todo.create({ todoId: 'MyUniqueTodoId', content: 'Buy Milk', completed: false }); | ||
const { data: todo, errors } = await client.models.Todo.create({ todoId: 'MyUniqueTodoId', content: 'Buy Milk', completed: false }); | ||
console.log(`New Todo created: ${todo.todoId}`); // New Todo created: MyUniqueTodoId | ||
``` | ||
</InlineFilter> | ||
|
||
<InlineFilter filters={["swift"]}> | ||
|
||
```swift | ||
let todo = Todo( | ||
todoId: "MyUniqueTodoId", | ||
content: "Buy Milk", | ||
completed: false) | ||
let createdTodo = try await Amplify.API.mutate(request: .create(todo)).get() | ||
print("New Todo created: \(createdTodo)") | ||
``` | ||
|
||
</InlineFilter> | ||
Comment on lines
+96
to
+107
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
||
## Composite identifier | ||
|
||
|
@@ -90,8 +123,23 @@ const schema = a.schema({ | |
}).identifier(['tenantId', 'name']) | ||
.authorization(allow => [allow.publicApiKey()]), | ||
}); | ||
``` | ||
<InlineFilter filters={["javascript", "angular", "react-native", "react", "nextjs", "vue", "android", "flutter"]}> | ||
|
||
```ts | ||
const client = generateClient<Schema>(); | ||
|
||
const branch = await client.models.StoreBranch.get({ tenantId: '123', name: 'Downtown' }); // All identifier fields are required when retrieving an item | ||
``` | ||
</InlineFilter> | ||
|
||
<InlineFilter filters={["swift"]}> | ||
```swift | ||
let queriedStoreBranch = try await Amplify.API.query( | ||
request: .get( | ||
StoreBranch.self, | ||
byIdentifier: .identifier( | ||
tenantId: "123", | ||
name: "Downtown"))) | ||
``` | ||
</InlineFilter> | ||
Comment on lines
+136
to
+145
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This PR continues to include android and flutter code snippets under JS snippets, as this is only scoped to address the Swift ones for now.