The majority of Android developers are strong supporters of RESTful APIs. And this is not surprising as REST is a really great architecture. Since technology is constantly changing, developers always need to keep abreast of developments.
In this GraphQL tutorial, our main points of discussion are the Apollo GraphQL itself, its main advantages and how to start using this technology in your Android application.
Usually, GraphQL is considered an almost radical, new way to think about APIs. And let’s be honest, it has some really cool advantages from the technical viewpoint:
- It allows the client to specify the data it needs;
- It simplifies data aggregation from several sources;
- It uses a type system for data description.
If you want to learn more about GraphQL, you can use the official guide by this link.
So far, there is only one GraphQL client for Android — Apollo. It’s a really sophisticated GraphQL client that makes consuming GraphQL API as easy as possible.
But why actually GraphQL? What makes it special?
Basically, there are three main reasons:
- It allows you to avoid a number of issues that you would normally face when using REST. For example, GraphQL relies on a more elegant way of making queries, which results in a simpler architecture.Using GraphQL also positively affects the performance side of things since it allows you to access only the data you need (i.e. solves the issue of overfetching and underfetching). Finally, GraphQL is just easier to maintain thanks to better documentation.
- GraphQL is born for top-tier and high-load applications. The fact that it’s used by Facebook says a lot. So if you need a tool to manage a gargantuan number of users, GraphQL is a perfect fit.
- GraphQL allows you to change the design of your application without having to mess with its backend part. It’s cool, isn’t it? This feature alone translates into massive amounts of time and efforts saved.
Of course there are many other advantages of GraphQL such as the ability to use it across different platforms without having to create workarounds, growing and supportive community, etc.
However, the main point is that GraphQL is much more efficient in terms of development when comparing to REST. Plus, it has a very short learning curve.
In other words, GraphQL is all about saving your time and optimizing development processes.
Now you know the advantages of GraphQL. Do you want to start using it already but you have no idea where to start?
Keep calm, we know the way!
Step-by-step GraphQL tutorial
- First, we add a dependency in build.gradle of our project’s file (current version Apollo 0.4.4).
- Next, we add a dependency in build.gradle file of our application’s module after the Android Plugin.
- If you use Kotlin in your project, add an apollo plugin before Kotlin plugins.
There are three types of GraphQL requests: query(analogue of GET), mutation(analogue of POST, PUT), and subscription.
We need to create a file with the .graphql extension for request building. In this file are gathered all your queries, mutations, subscriptions, and fragments (re-used blocks with the set of fields that can be used in the requests).
You can think of any name, such as examplequeries.graphql. However, it’s necessary to place the file into the folder src/main/graphq on the same level with the java folder.
Here’s how this file looks:
Hereafter, we need to add a file schema.json. In order to get it, let’s run the following command:
apollo-codegen introspect-schema http://yourserver/graphql –output /home/admi/AndroidStudio/StudioProjects/GraphQLExample/schema.json
In this command, we have to specify the path to your server as well as a place where we need to save a schema.json file. Having made the file, we add it into our project and place it with our file examplequeries.graphql.
Now we can rebuild the project for Apollo to generate all the necessary classes.
For sending GraphQL classes, we need to use ApolloClient class. However, before we start using it, we must set it up and create its instance using builder.
Now we can use ApolloClient to send requests to a backend.
Additionally, Apollo supports RxJava. As it works with Retrofit, you can use Observable as well as other types from Rx for request processing.
Here’s an example of a request without RxJava:
To use RxJava with Apollo, you add a dependency in build.gradle files of your application’s module.
Here’s an example of a request using RxJava:
If you followed our instructions carefully, you can proudly tell your friends you enjoy using GraphQL Apollo in your Android app. Our heartfelt congratulations to you!
GraphQL Tutorial – The Bottom Line
GraphQL makes the development process more simple and efficient. As opposed to REST API, GraphQL requires much less time for its implementation, which results in money and time saved.
In addition, the way GraphQL makes queries also makes the backend of your application more reliable and stable, which is another reason to consider this tool.
However, it’s only the very beginning for this technology, and it has much potential for the future. If you don’t want to miss anything and, don’t forget to follow our blog. We do our bests to keep you up with the trends!