Skip to content

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

Merged
merged 1 commit into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ a.schema({
});
```

<InlineFilter filters={["javascript", "angular", "react-native", "react", "nextjs", "vue", "android", "flutter"]}>
Copy link
Contributor Author

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.


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
Expand All @@ -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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image


## 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({
Expand All @@ -112,6 +134,8 @@ a.schema({
});
```

Long-form approach

```ts
a.schema({
PrivacySetting: a.enum([
Expand All @@ -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({
Expand All @@ -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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image


<InlineFilter filters={["javascript", "angular", "react-native", "react", "nextjs", "vue"]}>
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.
Expand All @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image


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`)
Expand All @@ -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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image


## Composite identifier

Expand All @@ -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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

Loading
Loading