Meeting Modern Business Needs with GraphQL
What is GraphQL?
GraphQL is a query language (for APIs) and runtime to satisfy certain queries for your current data. GraphQL offers a comprehensive and comprehensible description of the data in your API, gives users the ability to demand exactly what they need (and nothing more), allows APIs to be easily modified over time, and provides effective development tools [1].
GraphQL is a new, evolving query language developed by Facebook (primarily for the company’s mobile apps). Initially used for web-based applications, it has gained popularity over the past couple of years in the API management domain owing to its unique advantages. The following diagram shows how GraphQL architecture works.
Why Use GraphQL?
One major advantage of having GraphQL in place is that application developers will have the opportunity to reduce back and forth communication, which results in reducing network calls. For example, if we take a look at REST API architecture, a client would need to make several calls to get a particular flow of functionality to work.
On the other hand, GraphQL architecture has a completely different flow, where there will only be a single request object passed to a single endpoint. Then, according to the query object, the functions will be called and the response will be returned.
This also makes the lives of application developers easier as they do not have to worry about multiple endpoints, headers, and status codes. Apart from the above advantages, there are some other advantages such as the ease in which to build the solution and being able to have fine-grained or composite responses.
Architecture Styles or Web Patterns of Communication
When it comes to architecture or web patterns that GraphQL can be incorporated into, there are multiple patterns that we can think of. We will discuss two patterns here.
Composite Pattern
The first one is using GraphQL in a composite pattern. For instance, we can have a GraphQL API that will aggregate data from multiple sources, such as a REST API or database stored procedures (T-SQL, PL-SQL), and build the response. The following diagram illustrates this.
Façade Pattern
The next pattern would be to use GraphQL in a facade pattern, where it would use a single query to return some data that is needed for a single use case. For example, in an analytics dashboard, there is a widget that will have some data. In order to populate this widget, and if a traditional REST API is used directly, it would need to make several calls. If we can use GraphQL in a facade pattern, this can be achieved using a single call to the GraphQL API, where it will composite all the calls to the REST API that is needed to populate the widget.
The composite pattern is also kind of a facade pattern that uses different sources to get the data for a particular use case
In a similar fashion, the GraphQL concept can be added into several architectural and design patterns as per the need.
Conclusion
The right API management solution is critical for business success. When it comes to the selection process, it is vital to take a closer look at the points we have discussed in this article. The solution should be able to expose GraphQL APIs, which adhere to the GraphQL architecture, and also have the same support for traditional REST APIs.
It needs to be highlighted that traditional API management solutions might not be suitable or support the GraphQL style of communication. This is the reason why a modern API management solution such as WSO2 API Manager is required. It treats GraphQL characteristics as first-class features to reap the benefits of API management on top of the advantages that GraphQL provides.
[1] - https://graphql.org/