I finished watching some videos on how to use GraphQL in iOS. I’m trying to practice writing queries and I thought maybe I would use PokeAPI GraphQL. Now I wrote a query that fetches a single Pokemon with the following: id, name, order, types & sprites. Now the query works if I don’t add the sprites. Also the sprites type is jsonb! don’t really know what this is but was able to use it to get the info I wanted from the sprites. Here is how the query looks like.
query GetAllPokemon {
List: pokemon_v2_pokemon(
order_by: [{ pokemon_species_id: asc, id: asc }],
limit: 1
) {
id
name
order
types: pokemon_v2_pokemontypes {
slot
type: pokemon_v2_type {
name
}
}
sprites: pokemon_v2_pokemonsprites {
home: sprites(path: "other.home")
showdown: sprites(path: "other.showdown")
official: sprites(path: "other.official-artwork")
}
}
}
Now I added this to my project and using this method to call the query just like the tutorials I’ve watched. The tutorials didn’t have this jsonb!. Now this is how I’m calling it in Xcode.
Network.shared.apollo.fetch(query: GetAllPokemonQuery()) { result in
switch result {
case .success(let response):
print(response)
case .failure(let error):
print(error)
}
}
Now this is always falling I don’t know why but I have a feeling it’s because of the sprites:jsonb!. The sprites have nested objects that can be nil. This is the error I’m getting.
GraphQLExecutionError(path: List.0.sprites.0.home, underlying: ApolloAPI.JSONDecodingError.couldNotConvert(value: AnyHashable([AnyHashable(“front_default”): AnyHashable(“https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/other/home/1.png”), AnyHashable(“front_female”): AnyHashable(<null>), AnyHashable(“front_shiny”): AnyHashable(“https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/other/home/shiny/1.png”), AnyHashable(“front_shiny_female”): AnyHashable(<null>)]), to: Swift.String))
Now I’m completely lost and been stuck here for a couple of hours. Can someone point me in the right direction on how I would be able to get this to work. Also is it possible to be able to pass in an empty string instead of null?
Hope that I would to be able to fetch at least one Pokemon using GraphQL