If you use graphql as your service's api server, you can try to use the following methods to design your fragment.
If you have a Brand object, you need to show the different info of Brand according to the Views.
fragment BrandSimple on Brand {
id
name
nameJaKana
}
fragment BrandImagePath on Brand {
imagePath
}
fragment BrandItemCount on Brand {
itemConnection {
totalCount
}
}
fragment BrandReviewCount on Brand {
reviewConnection {
totalCount
}
}
fragment BrandListSimpleView on Brand {
...BrandSimple
...BrandItemCount
}
fragment BrandListView on Brand {
...BrandSimple
...BrandImagePath
...BrandReviewCount
}
fragment BrandListView on Brand {
...BrandSimple
...BrandImagePath
...BrandItemCount
...BrandReviewCount
}
extension BrandSimple {
}
extension BrandImagePath {
var imageUrl: URL? {
if let imagePath = imagePath, let imageUrl = URL(imagePath: imagePath) {
return imageUrl
}
return nil
}
}
extension BrandItemCount {
var itemCount: Int {
return itemConnection.totalCount
}
}
extension BrandReviewCount {
var reviewCount: Int {
return reviewConnection?.totalCount ?? 0
}
}
extension BrandListView {
var imageUrl: URL? {
return self.fragments.brandImagePath.imageUrl
}
var itemCount: Int {
return self.fragments.brandItemCount.itemCount
}
var reviewCount: Int {
return self.fragments.brandReviewCount.reviewCount
}
}
If you have a Button object, you need to show or hide by the login status of user.
fragment UserSimple on User {
id
name
age
...
}
fragment UserFollowRelation on User {
followingState: fetchFollowingState @include(if: $login) {
isFollowing
}
}
fragment UserListView on User {
...UserSimple
...UserFollowRelation
}
query UserConnection($login: Boolean = true) {
userConnection {
edges {
node {
...UserListView
}
}
}
}
extension UserFollowRelation {
var isFollowing: Bool {
return self.followingState.isFollowing
}
}
extension UserListView {
var isFollowing: Bool {
return self.fragments.userFollowRelation.isFollowing
}
}