PokeAPI / pokeapi

The Pokémon API
https://pokeapi.co
BSD 3-Clause "New" or "Revised" License
4.13k stars 931 forks source link

Pokedex list with all the names + types in it #629

Closed Squallrx closed 3 years ago

Squallrx commented 3 years ago

Hello, I'm build my first project using an Api, and i'm decided to go with the pokeapi Everything is working really great, but I wanted to list the pokemon in the home page using their name and type. I found that the "pokedex" endpoint has all the names, but not the type, so I would have to enter each specific pokemon url to get their type, which will be over 800 requests, and it doesn't look right. Is there a endpoint that has the type with the name in a unique list? like the pokedex endpoint, with the type? is possible to add that if it doesn't exist?

I wanted to do all the request on the next getStaticProps, but they don't allow me to do over 800 request without nodeJs dying, so only solution that I have found at the moment would be to keep the request on the front, and lazy loading them (it does work, but I really wanted to use the getStaticProps)

Sorry if the format of the post is not right, i'm really new at this world

Thanks

phalt commented 3 years ago

You should try out the graphql API, it might help: https://beta.pokeapi.co/graphql/console/

Naramsim commented 3 years ago
query samplePokeAPIquery {
  pokemon: pokemon_v2_pokemon {
    name
    id
    types: pokemon_v2_pokemontypes {
      type: pokemon_v2_type {
        name
      }
    }
  }
}

The above query does what you want @Squallrx.

If you wanna use the API feel free to query us as many times as you need. Also, you don't need to fire 800 concurrent HTTP requests. You can implement a lazy loading strategy.

Naramsim commented 3 years ago
query samplePokeAPIquery {
  species: pokemon_v2_pokemonspecies(order_by: {id: asc}) {
    name
    id
    pokemon: pokemon_v2_pokemons {
      name
      types: pokemon_v2_pokemontypes {
        type: pokemon_v2_type {
          name
        }
      }
    }
  }
}

The query above groups pokemons by their species.

Squallrx commented 3 years ago

Thanks guys @Naramsim @phalt ! I didn't know about graphql or the fact that the api supports it. I saw some videos about graphql to understand it better and I think the query that Naramsim send will do the work just fine!

Naramsim commented 3 years ago

Check out the Node example if you need: https://github.com/PokeAPI/pokeapi/tree/master/graphql/examples/node