Data fetch swift ios

Hi guys,

I need some help completing this code. I’m coding now for 4 weeks, so big novice here :see_no_evil:
I manage to setup Apollo and is working with no problem. I can fetch the data, but now i want to get in this case the category names in a array or only one.

My code

Network.shared.apollo.fetch(query: GetCategoriesQuery(sortby: sortBy, last: Last)) { result in
	guard let data = try? result.get().data else { return }
	print(data) // <-- what to do here? 
	// I assumed: print(data.edges.node.name) but nope!
	}

WPGraphQL

query getCategories ($sortby: String, $last: Int) {
  categories(where: {descriptionLike: $sortby, orderby: NAME}, last: $last) {
    edges {
      node {
        name
      }
    }
  }
}

Hi :wave: - from your operation it looks like there should be a categories property on data. If that callback is exiting early it’s probably because the guard statement failed and returned. It might help to add print(result) above the guard statement to make sure you’re actually receiving data and not an error.

Hi, thxs for your reply. The guard statement is working and i’m getting the data but I want to just print the names of the categories.

this is the data/result:

success(Apollo.GraphQLResult<AppName.GetCategoriesQuery.Data>(data: 
Optional(AppName.GetCategoriesQuery.Data(resultMap: ["categories": Optional(["edges":
 Optional([Optional(["__typename": Optional("RootQueryToCategoryConnectionEdge"), 
"node": Optional(["name": Optional("Foobar")... etc...

print(data.categories.name) gives me the following error:
Value of type ‘Result<GraphQLResult<GetCategoriesQuery.Data>, Error>’ has no member ‘categories’

When the closure is called and you’ve established that you’ve got a .success result the data should be in the shape of the GetCategoriesQuery class - what does that class look like? You should also have access to data.jsonObject but accessing it through the generated class properties is the safest.

Sorry but can you simplify this for me? Isn’t an easer way for me to get the data the way I need it. Because i thought that the Apollo framework should make this simpel. :see_no_evil:

Do you mean the class generated by Apollo or? Because i’m still working with the fetch code i copied form Apollo’s website just trying to understand how it works… with no succes

Never mind, after countless try and error i manage to solve it. Not easy tho

result.data?.categories?.edges?.compactMap({ $0?.node?.name })

apollo-ios provides a lot of value when using it with code generation and the type-safe operation-based models.

Do you mean the class generated by Apollo or? Because i’m still working with the fetch code i copied form Apollo’s website just trying to understand how it works… with no succes

Yes, the classes from code generation. If you haven’t yet gone through the tutorial I’d recommend it as a good starting point.

Never mind, after countless try and error i manage to solve it. Not easy tho

Glad you managed to get it working. :+1: