GraphQL in simple words is query language for APIs and a runtime to fulfill those queries. GraphQL is emerging technology originally developed by Facebook in 2012 and open sourced later in 2015. Today GraphQL is being used by many companies  in verity of applications ranging from small, medium and large-scale.

In this article we are going to talk about how GraphQL can add business value to your company's technology ecosystem by providing performance, productivity and quality. The main theme of this article is to visualize business aspect of adopting GraphQL for your projects.

This article is not technical deep-dive and is intened to hilight GraphQL architecture and business value.

As developers, we like to work on bleeding edge technologies & new frameworks but when it comes to implementing in real-world project within your workplace you need to conveince your manamgnet or boss for using that technology. You will hear quesitons like, What business values it's going to add?

In order to show the business value of the proposed technology you need to get them understand the technology first. No matter how good of a developer your manager was; it woudn't be easy to understand architecture first time, so you need to start from zero and guide him step-by-step. What actually GraphQL is? How it works? And how we can achieve better performance, productivity and quality for our projects within the company.

Let's clear the terminology first

It is easy to get confused by terms used in GraphQL with the databases, so before jumping into puddle, first we need to define terminology first.

  1. Query in GraphQL is NOT database query.
  2. Schema in GraphQL is NOT database schema.
  3. Runtime is term used jsut to callout GraphQL mechanics on server it is NOT like JVM or CLR (.NET). This is simply the code execute on server such as for NodeJS, it is plain javascript code, and for .NET, it is C# code, depending upon the GraphQL implentation you choose for your project.
  4. It is NOT a framwork or library, instead it is specification for Backend for Frontend (BFF) kind of framwork & has different implementations.

What really is the GraphQL?

In typical web or mobile application you need to have number of service end-points for fetching data to support various features in the application. GraphQL allows you to define one endpoint for entire application which you can use to fetch or manipulate data. There are two important terms in GraphQL, query and runtime.

Query is used to fetch data from your client browser or mobile app.

Runtime is combination of schema + revolvers which work in conjunction to get to data from data source such as database, another service, or virtually anything you can read and write on server.

The following figure shows high-level GraphQL architecture.

graphql architecture
GraphQL Architecture

Three basic questions for before developing any application?

Developing application require 3 questions about data needs to be answered first. What data I need for application? Where this data is going to come from? And How you, we can fetch the data? The above figure shows exactly how GraphQL mechanics fits into these questions.

Schema (What?) - What data we need for application, this is defined by schema. This just the definition language for your data, very similar to TypeScript syntax.

Resolves (Where?) - Now the next question is to where to get this data, is defined by revolvers. It is just plain code to fetch data, depending upon the choice of your implementation, it can be JavaScript or C# code for fetching data from database, another service or any data source.

Query (How?) - Now we have data, and we know where to get it, final question is how to fetch the data from client web or mobile app, this is defined by query. GraphQL query is JSON like text query to fetch data.

GraphQL is not framework instead it is RFC specification & has various implementations.

The two popular implementation are:

  1. Apollo (recommended) - Is open-source set of libraries for GraphQL implementation in almost every major language such as NodeJS, React, Angular & .NET etc. Has excellent tools for various framworkds and languages.
  2. Relay - Is also great as it is developed by Facebook, but I find it hard to begin with as starter.

A typical application example

Let's look at example of typical web/mobile application, where you have client, server and database. Your app need to read data from database and display on UI pretty straightforward. You have service host (which is actually server) and client which loads data and store it in somewhere in state using state management mechanism i.e. Redux/Flux. The following diagram depicts the whole mechinary needed for the application.

application without graphql
Application without GraphQL

On the left-side we have server which host your service end-points, and reads data from database, external service or virtually any data source. Once the data is loaded from these data sources you might (definitely) want to format the data as per your app frontend requirements.

Client web or mobile app is on right-side, which reads the data from server and renders on UI. This includes code for fetching data and state management.

If we combine all of these together, what type of boilerplate code you need to write to build full-stack application?

  1. Service end-points for each feature you need for application.
  2. Code to read data from datasource such as databses, or other external services.
  3. Data formatting on server before streaming down to client.
  4. On client utility code/functions to read data from server.
  5. Client side state managment configuration.
  6. Acutal state managment code such (Redux/Flux):
    • Actions.
    • Reducers.
    • Action Creators.
  7. Client-side state managment middleweres.
  8. Nubmer of state updates affects the performace of application in geanal, if performance is the concern then you might need to implement some mechanism to reduce number of updates to state potentially re-redners. You might need some sort of batching mechanism, in case of React/Redux you would got with Batched Actions.
  9. You might need to implemtn caching mechanism to reduce number of round trips to server while making service calls.
  10. If all of above isn't enough to bleed your eys & ears, you need to write unit-tests for all of the boioerplate code to achieve better quality.

How it looks with GraphQL?

If you combined all the boilerplate code in above section for client and server, it is the significant amount of code you need to write by hand to complete end-to-end workflow of application.

The best code is the one you don't have to write!

What if I tell you, all of this can be done without writing most of the code aforementioned? Welcome to GraphQL!

If you look at the following figure and compare it with earlier one, GraphQL with Apollo tools, greatly simplifies architecture and manages majority of the workflow for fetching data and handling state.

application with graphql
Application with GraphQL

GraphQL Apollo gives you all the feature you write it by hand earlier plus much more such as:

  1. GraphQL simplifies the logic to load and process data for the application by clearly defining Schema, Resolver and Queries.
  2. You don't have to define dozens of service end-points to fetch data.
  3. Apollo Client handles data fetching from GraphQL server to client app.
  4. Apollo Cache handles data cache based on query parameters without needing you to write single line of code for caching; to me this is the blockbuster feature. Parameter based Caching on client-side, is notoriously difficult to achieve if you have to do it manually.
  5. GraphQL lets to fetech only the data you need, which isn't possible with traditional services where you get everything returned by service even if you only need less then half of the data. That is where you need seprate endpoints for moible and web, GraphQL elemitate this ditctinction alltogether.
  6. You can be more productive as developer, with tool such as Chrome extensions & VS Code extentions for GraphQL, which makes develpmen and debugging flow easier than ever.
  7. GraphQL significantly reduces the amount of code you need to write to achieve simple features and separes you more time to spend on building application.

What is the business value of adopting GraphQL?

There are number of obvious advantages of GraphQL if you follow the points mentioned above but lets divided them into three categories.

Performance

  1. Off-loads the work from client browser or app by reducing the boilerplate code, which you have to write otherwise, to achieve state management on client.
  2. GraphQL lets you request the amount of data you actually need it, which is not possible with REST based services. This reduces the amount of data travel over the wire & improve the response times.
  3. Apollo GraphQL lets you cache the query response based on parameters, which reduces the number o round trips to the server, and greatly improve performance and user experience.
  4. Subscriptions allows streaming data from server to client which imporve user experice while load long lists of data.
  5. Lazy-loading quries, allows executing queries whith priorities, which lets to load  high priorty data fetched first without waiting on low priotiy data. You can achieve this without costing you single line of code.
  6. In production mode GraphQL quries don't get transmitted over the network, instead you can configure your applicaoitn to use persisted queries, which sends query hash instead of actual query while fetching the data from server.

Productivity

  1. Reduce boilerplate code, which otherwise you have to write in form of using framwork such as Redux/Flux, Actions, Action Creators & Reduces etc.
  2. GraphQL Playground: A playground allows you to run your quries, view documentation & and schma in develpment environment.
  3. GraphQL Chrome extension helps to see traces of queries and contents of cache in the browser (similar to Redux extension for Chrome).
  4. VS Code extension to hlep navigation through the GraphQL schema and quries.
  5. No need to have custome framwork to handle data loading, instead use industry standard framwork tools.
  6. Apollo GraphQL is very well documented and maintained.

Quality

  1. Less amount of code to achieve more features. Less code means less bugs.
  2. You can mock the entire GraphQL API which makes testing easier.
  3. You can write automation/e2e test with Jest.
  4. Easy bench-marking & easy to baseline performance score.

What is the cost of GraphQL adoption?

The most common concern about GraphQL adoption is; Do we need to re-write everything for GraphQL? Not really, GraphQL require absolute zero change to anything exiting. It can work perfectly fine in conjunction with other frameworks and technologies within your existing system.

Learning Curve - There are two parts of GraphQL adoption:

  1. GraphQL configuration within your application, which is setting up service endpoint, and other stuff for GraphQL to be up and running. This is one-time job at the beginning.
  2. Usage, develop your application features using

GraphQL is significant shift in thinking about data retrieval as compared to REST base web services. In a team environment there is  learning curve for developers, the learning curve is not so steep as everybody in the team don't need to know about the configuration instead they just need to lean how to use GraphQL.

For example everybody in the team need to know about how to write schema, revolvers and queries to fetch the data, but not so much about GraphQL's internal mechanics.

Some other questions you might expect?

You can expect any type of questions, depending upon the  knowledge of audience.

As technical junky I love to explore new technologies and propose new ideas within my workplace, sometime I get questions which are absolutely nuts, I feel like being surrounded by bunch of idiots; if you experience somthing similar, its pretty normal for pasionate developers!
  1. Is it secure? - GraphQL is no different from any other service end-point, if you put it behind auth handler etc.
  2. What about randomly poking the schema from Client? - There are tow things, first this schema is  not database schema; second you define what you exactly need in the app, and don't just expose whole database.
  3. What if I use Fiddler to inspect traffic and change query? - Apollo support Automated Persisted Queries, in which you won't even see query going in HTTP request except for only very first time (which can easily be pre-populated after deployment); so there is no question of tempering with GraphQL query.
  4. For NodeJS/React/Angular projects, What is TypeScript support? - Apollo tools for GraphQL has very good support for TypeScript.
  5. What about error handling? Very good Error Handling and tracing mechanism.
  6. What about larger input forms for collecting data from client? The size of HTTP request depends upon the max allowed size in your app, its nothing specific to GraphQL.

Conclusion

GraphQL is great technology to fulfill data needs for your applications, and significantly simplifies application development process by reducing boilerplate code throughout the application. REST base services has been primary choice of service oriented applications in the past; there are tones of applications and tools written to support that, GraphQL is going to replace REST but that is significant shift from traditional development, and it is going to take some time to devlop awarness and understand of GraphQL in the tech industry.

The hard part of GraphQL adoption is the convincing people who are not so much technical and happend to be your managers; once you cross that barrier and people know how its going to improve overall technical workflow, everything else should not be a piece of cake.

Hope that article would help you in GraphQL adoption!

Tell me how did you convernce your boss? Share your feedback, thanks.