swift-graphql
swift-graphql copied to clipboard
Category Tree Structure upto 2 levels
Describe the bug I am trying to fetch the multiple levels of CategoryTree with this approach
`
struct CategoryResult {
var items: [Category]
var totalCount: Int
static let selection = Selection.CategoryResult<CategoryResult> {
let totalCount: Int = try $0.totalCount() ?? 0
let items: [Category] = try $0.items(selection: Category.categoriesSelection.list)
return CategoryResult(items: items, totalCount: totalCount)
}
static let listQuery = Selection.Query<CategoryResult> {
try $0.categories(filters: ~InputObjects.CategoryFilterInput(parentId: ~InputObjects.FilterEqualTypeInput(eq: "33")), selection: selection)
}
static func listQuery(id: String) -> Selection.Query<CategoryResult> {
Selection.Query<CategoryResult> { try $0.categories(filters: ~InputObjects.CategoryFilterInput(parentId: ~InputObjects.FilterEqualTypeInput(eq: ~id)), selection: selection) }
}
}
extension Category {
static let categoriesSelection1 = Selection.CategoryTree<Category> {
let name: String = try $0.name() ?? ""
let path: String = try $0.path() ?? ""
let urlPath: String = try $0.urlPath() ?? ""
let uid: String = try $0.uid()
let urlKey: String = try $0.urlKey() ?? ""
let childrenCount: String = try $0.childrenCount() ?? "0"
return Category(name: name, path: path, uid: uid, isInMenu: false, childrenCount: childrenCount, children: [])
}
static let categoriesSelection = Selection.CategoryTree<Category> {
let name: String = try $0.name() ?? ""
let path: String = try $0.path() ?? ""
let urlPath: String = try $0.urlPath() ?? ""
let uid: String = try $0.uid()
let urlKey: String = try $0.urlKey() ?? ""
let isInMenu: Bool = try $0.includeInMenu() == 1
let childrenCount: String = try $0.childrenCount() ?? "0"
let children: [Category] = try $0.children(selection: Category.categoriesSelection1.list)
return Category(name: name, path: path, uid: uid, isInMenu: isInMenu, childrenCount: childrenCount, children: children)
}
static func fetchCategories(id: String = "33", completion: @escaping (Result<CategoryResult, Error>) -> Void) {
let request = NetworkClient.http.querying(CategoryResult.listQuery(id: id))
let task = URLSession.shared.dataTask(with: request) { data, response, error in
DispatchQueue.main.async {
guard let result = try? data?.decode(CategoryResult.listQuery(id: id)) else {
completion(.failure(error!))
return
}
completion(.success(result.data))
}
}
task.resume()
}
}
`
To Reproduce I have tried different approaches to solve the issue as seen in the above code but I want to know if it's supported
Expected behavior To fetch the multiple category tree as I am able to successfully fetch it in iGraphQL
Screenshots If applicable, add screenshots to help explain your problem.
Smartphone (please complete the following information):
- Device: iPhone 14
- OS: iOS 17
Additional context Add any other context about the problem here.