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

Conversation

lawmicha
Copy link
Contributor

@lawmicha lawmicha commented May 15, 2024

Description of changes:

Add swift code snippets for the pages under Data/Customize your data model, to

  • add fields to data model page
  • modeling relationships
  • customize data model identifiers
  • customize secondary indexes

These changes are tested via the integration test over in the PR: aws-amplify/amplify-swift#3699

Related GitHub issue #, if available:

Instructions

If this PR should not be merged upon approval for any reason, please submit as a DRAFT

Which product(s) are affected by this PR (if applicable)?

  • amplify-cli
  • amplify-ui
  • amplify-studio
  • amplify-hosting
  • amplify-libraries

Which platform(s) are affected by this PR (if applicable)?

  • JS
  • Swift
  • Android
  • Flutter
  • React Native

Please add the product(s)/platform(s) affected to the PR title

Checks

  • Does this PR conform to the styleguide?

  • Does this PR include filetypes other than markdown or images? Please add or update unit tests accordingly.

  • Are any files being deleted with this PR? If so, have the needed redirects been created?

  • Are all links in MDX files using the MDX link syntax rather than HTML link syntax?

    ref: MDX: [link](https://docs.amplify.aws/)
    HTML: <a href="https://docs.amplify.aws/">link</a>

When this PR is ready to merge, please check the box below

  • Ready to merge

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@lawmicha lawmicha requested review from renebrandel and a team as code owners May 15, 2024 15:08
@lawmicha lawmicha force-pushed the data.customize-data-model-pages branch 4 times, most recently from 820403b to 47d00d5 Compare May 15, 2024 15:23
@lawmicha lawmicha force-pushed the data.customize-data-model-pages branch from 47d00d5 to b89bff7 Compare May 15, 2024 15:32
@@ -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.


</InlineFilter>

<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

Comment on lines +107 to +120
<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>
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

Comment on lines +174 to +185
<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>
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

Comment on lines +55 to +65
<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>
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

Comment on lines +677 to 689
do {
guard let queriedCart = try await Amplify.API.query(
request: .get(
Cart.self,
byIdentifier: existingCart.identifier)).get() else {
print("Missing cart")
return
}

let customer = try await queriedCart.customer
} catch {
print("Failed to fetch cart or customer", error)
}
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

Comment on lines +872 to +890
<InlineFilter filters={["swift"]}>

```swift
do {
guard let queriedPost = try await Amplify.API.query(
request: .get(
Post.self,
byIdentifier: post.identifier)).get() else {
print("Missing post")
return
}

let loadedAuthor = try await queriedPost.author
let loadedEditor = try await queriedPost.editor
} catch {
print("Failed to fetch post, author, or editor", error)
}
```
</InlineFilter>
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

Comment on lines +74 to +105
```swift
struct PaginatedList<ModelType: Model>: Decodable {
let items: [ModelType]
let nextToken: String?
}
let operationName = "listCustomer8ByAccountRepresentativeId"
let document = """
query ListCustomer8ByAccountRepresentativeId {
\(operationName)(accountRepresentativeId: "\(accountRepresentativeId)") {
items {
createdAt
accountRepresentativeId
id
name
phoneNumber
updatedAt
}
nextToken
}
}
"""

let request = GraphQLRequest<PaginatedList<Customer>>(
document: document,
responseType: PaginatedList<Customer>.self,
decodePath: operationName)

let queriedCustomers = try await Amplify.API.query(
request: request).get()
```

</InlineFilter>
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
  • A custom document is needed here since the Swift client does not contain any knowledge of the new list query operation.
  • PaginatedList shows how to decode from the graphQL response shape containing items and nextToken.

Comment on lines +152 to +186
<InlineFilter filters={["swift"]}>

On the client side, you can create a custom GraphQL request based on the new `listBy...` query that's named after hash key and sort keys. You can supply the filter as part of this new list query:

```swift
struct PaginatedList<ModelType: Model>: Decodable {
let items: [ModelType]
let nextToken: String?
}
let operationName = "listCustomer9ByAccountRepresentativeIdAndName"
let document = """
query ListCustomer8ByAccountRepresentativeId {
\(operationName)(accountRepresentativeId: "\(accountRepresentativeId)", name: {beginsWith: "\(name)"}) {
items {
accountRepresentativeId
createdAt
id
name
phoneNumber
updatedAt
}
nextToken
}
}
"""
let request = GraphQLRequest<PaginatedList<Customer>>(
document: document,
responseType: PaginatedList<Customer>.self,
decodePath: operationName)

let queriedCustomers = try await Amplify.API.query(
request: request).get()
```
</InlineFilter>

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

Comment on lines +229 to +258
struct PaginatedList<ModelType: Model>: Decodable {
let items: [ModelType]
let nextToken: String?
}
let operationName = "listByRep"
let document = """
query ListByRep {
\(operationName)(accountRepresentativeId: "\(accountRepresentativeId)") {
items {
accountRepresentativeId
createdAt
id
name
phoneNumber
updatedAt
}
nextToken
}
}
"""

let request = GraphQLRequest<PaginatedList<Customer>>(
document: document,
responseType: PaginatedList<Customer>.self,
decodePath: operationName)

let queriedCustomers = try await Amplify.API.query(
request: request).get()
```
</InlineFilter>
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

@lawmicha lawmicha merged commit 5aebffd into main May 15, 2024
10 checks passed
@lawmicha lawmicha deleted the data.customize-data-model-pages branch May 15, 2024 17:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants