Building Scalable Enterprise Products with GraphQL: Business Aspects and Benefits

Building Scalable Enterprise Products with GraphQL: Business Aspects and Benefits

The API approach you choose for your project influences your app's performance, functionality, and scalability. This piece dwells upon GraphQL - query language, that allows you to build scalable and performative APIs. You will learn about the main benefits of GraphQL, what types of projects it is suitable for, and practical examples of its usage.

Let’s get straight to the point.

What is GraphQL

GraphQL is a query language developed by Facebook in 2012. It was created as an alternative to the REST concept, which requires working with a lot of endpoints for fetching certain pieces of data.

GraphQL, in its turn, allows users to describe what kind of data they want to receive in just one request. It significantly reduces the number of calls to fetch data by retrieving linked resources and aggregating queries. As a result, it prevents fetching extra data and saves both time and efforts. GraphQL gives power to the client, not the server. Applications that use GraphQL are fast and efficient, even on small network connections.

Let’s review the main benefits of GraphQL app development.

Main benefits of using GraphQL for your application

  • Efficient data retrieval

The main goal of GraphQL is to simplify the process of retrieving data. In GraphQL data are collected under a common endpoint. The response from server depends on what user describes inside the query. It’s very useful compared to REST API endpoint. Let’s review an example. Assume that you want to get the list of users from the database, but return only first name and age of the user. With GraphQL you can describe that you need only this type of entity and only 2 fields (name and birth date). That's it. There is no need to use a big amount of resources for each data request.

  • Multi-resource data fetching

GraphQL allows getting the data from multiple resources with one request. In traditional API query languages, the data are delivered with multiple calls to the server, that lead to several endpoints. GraphQL provides better query efficiency, and, as a result, fast and predictable data delivery.

  • Readability

Queries are easy to read and understand even for non-technical users. Example:

GraphQL readability

This is how the query looks like if we want to get only first name, avatar and a company name of some user. GraphQL looks really good for connected data like a database with a lot of collections which have references by a foreign key.

  • Strict typing

GraphQL describes everything in terms of types and fields. This ensures that applications can get only data that are described in GraphQL schemas. In case of invalid requests, GraphQL provides understandable and reliable error system, which clearly describes what went wrong. This also ensures that clients will receive a response in prescribed and predictable format depending on requests they make.

  • Ecosystem

As GraphQL becomes more and more popular among developers, it’s ecosystem grows as well. At the moment there are a couple of reliable clients for GraphQL such as Apollo and Relay. They are responsible for catching data returned by the server, integration with some framework like React, Angular or Vue, managing subscriptions to make applications reactive, which guarantees real-time updates, pagination etc. It also provides a good API explorer called GraphiQL which shows all the capabilities of the current API.

  • Scalability

After describing all types of data, it is really easy to change or add something new, with a minimum impact on existing data architecture. It is especially useful when we have to work with a lot of related collections in the database which are connected to each other.

What type of projects GraphQL is suitable for

As GraphQL allows to aggregate data from different sources, it is very suitable when developing an application which gets data from multiple sources. For example, you get data from MongoDB and some SQL database. It is also useful if your application will be integrated with a lot of third-party services or other applications.

We at Apiko have used GraphQL for a huge management system, which includes a couple of client-side applications with a common backend.

Client's application should have been able to fetch data from different resources and work efficiently with dozens of linked collections (entities). As the result, each application gets the required data, using common GraphQL requests syntax.

Such approach has significantly simplified a development process because GraphQL is very scalable and easy to maintain. Of course, it takes some extra time to set things up in the beginning, but it saves way more time in the future.

Let’s take a look at some examples of GraphQL performance in the application we develop.

On the left side of the following screen you can see the request sent to the server, and on the right - what data were retrieved.

GraphiQL data

Here is another example.

The first screen shows the request itself.

GraphQL request

These are query variables that are added to the request.

GraphQL query

And finally here is the data that were fetched.

GraphQL query

These screenshots show how GraphQL requests with variables look like. In this case, we search for assets with the search variable and get those assests that contain this word in their name.

One more example that suits well here is the business case of NewSpring Church. The company has used different internal and third-party systems all over the web to provide users with the necessary data.

Such approach was a bit confusing for people and difficult to manage and maintain. For this reason, developers have built the application, that connected all the data from different sources in one place.

Thanks to GraphQL, users can access data from different stacks and databases in a single place. GraphQL has allowed them to connect all of those databases and systems into a single endpoint and triple the speed of the app.

As you can see, GraphQL perfectly deals with the applications, that fetch data from different resources and returns it to the client in a structured and comprehensible way.

Read also: Building native mobile applications with GraphQL

Use GraphQL or migrate to it later as your business grows?

If you are planning to build a scalable application, it is better to start using GraphQL from the beginning. Migration from REST to GraphQL takes a tremendous amount of time. However, it still can be a good solution as your application grows and maintaining several endpoints becomes more and more complicated.

When GraphQL may be not the right solution

Like all technologies, GraphQL has it's pros and cons. Despite GraphQL's numerous benefits, a few things can be quite challenging.

Setting GraphQL up requires a lot of time and efforts, as you have to describe all schemas, types, requests, and mutations. It is difficult to implement, but easier to maintain in future. Basically, it is an additional layer between a client and server.

GraphQL might be not the best case if you are about to develop a simple API without integration with third-party services. It might not fit if you use only one source of retrieving data (only one database for example). Another case is if you are sure that the application you develop is not going to extend a lot.

Wrapping Up

Technologies and trends change with a lightning speed, there is no one size fits all when it comes to choosing the API approach. It all depends on your project, your needs, and requirements.

What you really need to do is to think ahead, plan your project’s roadmap and forecast the possibility of scaling. If you plan to make your project more complex in future, integrate it with third-party resources and fetch data from different databases - GraphQL is just the thing.

Need a consulting concerning the choice of API query language or just have any questions? Feel free to leave a comment below:)