visit
Even better, you can use a hosted GraphQL backend — (by ). This service is brand new and was . With Slash GraphQL, I can create a new backend endpoint, specify the schema I want for my , and — voila! — be up and running in just a few steps.
The beauty of a hosted backend is that you don't need to manage your own backend infrastructure, create and manage your own database, or create API endpoints. All of that is taken care of for you.You can .Update: On April 16th, 2021, Slash GraphQL was officially renamed Dgraph Cloud. The information below still applies, and you can still build the app as described.
The Pokémon Pokédex app I builtWhat 90s child (or adult, for that matter) didn't dream of catching all 150 original Pokémon? Our demo app will help us keep track of our progress in becoming Pokémon masters.As we build out our app, we'll cover all the CRUD operations for working with an API: create, read, update, and delete.We'll start by adding all our Pokémon to the database online in Slash GraphQL's API Explorer. Then, in the Pokédex app UI, we'll display all 151 Pokémon queried from the database. (Hey, I couldn't leave out Mew, could I?) At the top of the screen, we'll show two dropdown menus that will allow us to filter the shown results by Pokémon type and by whether or not the Pokémon has been captured. Each Pokémon will also have a toggle switch next to it that will allow us to mark the Pokémon as captured or not. We won't be deleting any Pokémon from our database via the app's UI, but I'll walk you through how that could be done in the event that you need to clean up some data.Ready to begin our journey?
Now that we have our backend up and running, we need to create the schema for the type of data we'll have in our database. For the Pokédex app, we'll have a
Pokémon
type and a PokémonType
enum.There's a lot to unpack in that small amount of code! The
PokémonType
enum is straightforward enough—it's a set of all the Pokémon types, including Fire, Water, Grass, and Electric. The Pokémon
type describes the shape of our data that we'll have for each Pokémon. Each Pokémon will have an ID, a name, an image URL for displaying the Pokémon's picture, the types of Pokémon it is, and a status indicating whether or not the Pokémon is captured.You can see that each field has a data type associated with it. For example,
id
is an Int
(integer), name
and imgUrl
are String
types, and captured
is a Boolean
. The presence of an exclamation point !
means the field is required. Finally, adding the @search
keyword makes the field searchable in your queries and mutations.To test out working with our database and newly created schema, we can use the API Explorer, which is a neat feature that allows us to run queries and mutations against our database right from within the Slash GraphQL web console.Adding all the Pokémon via the API Explorer
Now, for a quick sanity check, we can query our database to make sure that all our Pokémon have been added correctly. We'll request the data for all our Pokémon like so:Here's what it looks like in the API Explorer:Querying for all Pokémon in the API Explorer
We could also write a similar query that only returns the Pokémon names if that's all the data we need. Behold, the beauty of GraphQL!Querying for all Pokémon names in the API Explorer
When using Slash GraphQL in our frontend code, we essentially just make a POST request to our single API endpoint that we were provided when creating the backend. In the body of the request, we provide our GraphQL code as the
query
, we write a descriptive name for the query or mutation as the operationName
, and then we optionally provide an object of any variables
we reference in our GraphQL code.Here's a simplified version of how we follow this pattern to fetch our Pokémon in the app:We then take that data and loop over it using the Array
map
helper function to display each Pokémon in the UI.The filters at the top of the page are hooked up to our API as well. When the filter values change, a new API request kicks off, but this time with a narrower set of search results. For example, here are all the Fire type Pokémon that we've captured:Captured Fire type PokémonThe JavaScript for making an API request for Pokémon filtered by type and captured status looks a little like this:
We then call the
updatePokemonCapturedStatus
function when the toggle value changes. This kicks off the API request to update the value in the database. Then, we can either optimistically update the UI without waiting for a response from the backend, or we can wait for a response and merge the result for the single Pokémon into our frontend's larger dataset of all Pokémon. We could also simply request all the Pokémon again and replace our frontend's stored Pokémon info with the new result, which is what I chose to do.Deleting all Bulbasaur Pokémon via the API Explorer
Then, we could add one Bulbasaur back:Hopefully we didn't... hurt ourselves in confusion... [cue audible groans from the readers].
We haven't yet covered to secure our app or how to use the when making our GraphQL requests, but those are important topics for another article!As an experienced frontend developer but without much experience using GraphQL, working with Slash GraphQL was refreshingly easy. Getting set up was a breeze, and the API Explorer along with the documentation played a crucial role in helping me explore the various queries and mutations I could make with my data.Slash GraphQL, I choose you! [more audible groans from the readers]