-
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
Conversation
820403b
to
47d00d5
Compare
47d00d5
to
b89bff7
Compare
@@ -87,6 +87,8 @@ a.schema({ | |||
}); | |||
``` | |||
|
|||
<InlineFilter filters={["javascript", "angular", "react-native", "react", "nextjs", "vue", "android", "flutter"]}> |
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.
|
||
</InlineFilter> | ||
|
||
<InlineFilter filters={["javascript", "angular", "react-native", "react", "nextjs", "vue"]}> |
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.
I found this section only relevant for the TS client, by having this inline filter, removes it from swift/android/flutter
<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> |
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.
<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> |
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.
<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> |
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.
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) | ||
} |
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.
<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> |
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.
```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> |
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.
<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> | ||
|
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.
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> |
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.
Description of changes:
Add swift code snippets for the pages under Data/Customize your data model, to
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)?
Which platform(s) are affected by this PR (if applicable)?
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
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.