Implementing Rick & Morty GraphQL API in Android — Part 1

Malcolm Maima
4 min readJan 26, 2023

--

In this tutorial, we will be creating a simple Android application that utilizes a GraphQL API to retrieve information about the popular animated series, Rick and Morty. We will be using the official Rick and Morty GraphQL API provided by Axel Fuhrmann, which allows us to easily fetch data about characters, episodes, and more.

Before we begin, it is important to note that you should have some basic knowledge of Android development and GraphQL. Additionally, you should have Android Studio installed on your computer.

To get started maybe it would be a good idea to get some quick intro knowledge on what graphql is.

What is Graphql?

GraphQL is a way for a client (like a mobile or web app) to ask for specific data from a server. It is an alternative to traditional REST APIs, which often return a fixed set of data for a specific endpoint.

With GraphQL, the client can specify exactly what data it needs and the server will return only that data. This allows for more efficient data retrieval and management, as the client only receives the data it needs. It also allows for more flexibility in the client, as the client can ask for different data for different parts of the app without having to create new endpoints on the server.

Think of it like a menu in a restaurant, the client(you) can choose exactly what you want to eat (data) and the server(chef) will give you only that, instead of a pre-set plate (like in a traditional REST API).

In GraphQL, there are two main types of operations: queries and mutations.

A query is used to retrieve data from the server. It is similar to a GET request in REST. For example, a client can use a query to ask for all the characters in the Rick and Morty show, or a specific character by their id.

query {
characters {
name
species
status
}
}

A mutation is used to make changes to the data on the server. It is similar to a POST, PUT or DELETE request in REST. For example, a client can use a mutation to add a new character to the show or update an existing character.

mutation {
addCharacter(name:"Morty Smith", species:"Human", status:"Alive") {
name
species
status
}
}

Another important concept in GraphQL is the schema. The schema is a blueprint of all the types of data that can be queried or modified in the server. It defines the fields and types of the data. The schema is written in a special language called GraphQL Schema Definition Language (SDL). The client should be aware of the schema and make sure it’s asking for the correct fields and types of data.

Additionally, GraphQL also supports subscriptions which allow the client to subscribe to real-time updates from the server. This is useful for scenarios where the client wants to be notified of any updates to the data in near real-time.

Let’s get started shall we

Step 1: Create a new Android project

Open Android Studio and create a new project by clicking on “File” > “New” > “New Project.” Give your project a name and select “Empty Activity” as the template.

Step 2: Add the GraphQL dependency

To use GraphQL in our Android project, we need to add the GraphQL dependency to our app-level build.gradle file. To do this, open the build.gradle file and add the following line under dependencies:

dependencies {
implementation("com.apollographql.apollo3:apollo-runtime:3.7.3")
}

Add the plugin to your build.gradle.kts:

plugins {
id("com.apollographql.apollo3").version("3.7.3")
}

Set the package name to use for the generated models:

apollo {
packageName.set("com.example")
}

Step 3: Create a GraphQL client

In order to interact with the Rick and Morty GraphQL API, we need to create a GraphQL client. This can be done by creating a new class, for example, “GraphQLClient.kt” and instantiating an ApolloClient object.’

val apolloClient = ApolloClient.builder()
.serverUrl("https://rickandmortyapi.com/graphql")
.build()

If you’re using dependency injection for example with hilt you can provide your apollo client and inject it to your viewmodels. A DI snippet would look like so:

@Singleton
@Provides
fun provideApolloClient(): ApolloClient = ApolloClient.builder()
.serverUrl("https://rickandmortyapi.com/graphql")
.build()

Step 4: Create a GraphQL query

Now that we have a client set up, we can create a GraphQL query to retrieve data from the API. We will create a new class, for example, “Queries.kt” and define our query there. For this tutorial, we will retrieve information about all the characters in the series.

val charactersQuery = CharactersQuery.builder().build()

Step 5: Execute the query

In our repository class, we can execute the query and retrieve the data using the client we created earlier:

@Inject
lateinit var apolloClient: ApolloClient

fun getCharacters() {
apolloClient.query(charactersQuery)
.enqueue(object : ApolloCall.Callback<CharactersQuery.Data>() {
override fun onResponse(response: Response<CharactersQuery.Data>) {
//do something with the data
}

override fun onFailure(e: ApolloException) {
//handle error
}
})
}

And that’s it! We have successfully implemented a GraphQL API in our Android application written in Kotlin to retrieve information about the characters in the series Rick and Morty. In the next part of this tutorial, we will look at how to display the data in our app and handle errors.

Links:

--

--

Malcolm Maima

Android Engineer @ Equity Bank — I code stuff…I eat a lot too, coffee makes me sleepy Love cartoons and robots. http://linktr.ee/malcolmmaima