Enabling Innovative API Data Control with GraphQL

Maxwell Struever
5 min readFeb 20, 2024

--

Maintaining coherent #datagovernance, defining efficient processes, and ensuring final consumers leverage available #data can be challenging for organizations. GraphQL, an open-source data query and manipulation language for APIs, can help combat these problems. In our latest article, Mike Wzorek and I discuss the various advantages and features of #GraphQL, along with some of the tools to make it even easier! Read on to learn more.

GraphQL unleashing the queryability of related data

In the iterative and evolving world of product-based software development, precisely and seamlessly interacting with the data and easily making changes to which data is presented is important. Typically, a REST API-based architecture involves an arduous process of creating and maintaining numerous API endpoints for each data request and stitching those API requests together to create a cohesive and meaningful information model to present to users.

In addition, data comes from multiple sources across the organization, owned and managed by multiple teams, stored in heterogeneous database backends, and located on multiple platforms with complex access policies. Data warehouses, data marts and data lakes expose datasets with their own requirements and set of protocols. Maintaining a coherent governance, defining efficient processes, and ensuring final consumers leverage the data available in the organization is a challenge.

A solution to these growing problems has emerged in the form of GraphQL, an open-source data query and manipulation language for APIs. GraphQL aims at providing self-documenting APIs to end users, using data from any source to build production-ready applications.

A powerful protocol

GraphQL is a standardized query language to query data from multiple sources. Importantly, its capabilities are not limited to simply querying specific types of stored data, such as the kind kept in a PostgreSQL database. The solution works by searching for defined types of data, and then retrieving related data based on the assigned relationships with each subset. This process is highly beneficial to users as it helps dictate how queries are executed when fetching data from actual sources. Data that is utilizing the GraphQL protocol is presented in a JSON-based hierarchical structure and is representative of how information is related in the real world. Think of a customer as the base of the tree and its branches include things that customers relate to, such as orders, shopping cart, addresses, friends, favorites, etc. Within each of these branches you can have sub-branches to represent other entities, such as items in the active shopping cart.

However, the user doesn’t have to be the trunk of the tree; you want to start searching from a particular order number and see who the purchaser was and what items were in it. That’s certainly a possible requirement, and with GraphQL, it’s an easy change to the query.

Interactive example: GraphQL

To put the effectiveness of GraphQL into perspective, typical REST approaches require multiple queries directed to separate endpoints before their related data can be accessed. In the case of GraphQL, specific additional data can be nested within a single query to expedite the entire process. This allows the user or client to have control over the types of data fields that are returned, enhancing the system on two fronts.

Flexibility is one of the most prominent advantages delivered by GraphQL, enabling data to be recognized and made available from the very start. This contrasts the requirement for all routes and endpoints to be pre-defined in the case of typical API approaches. Not only that, but updates made to the GraphQL are immediately available to users and clients via the typed schema. Other key features of GraphQL include its ability to avoid bloat in terms of returned data, and its self-documenting capabilities. It can also help the handling of versioning an API without breaking existing queries, thanks to clear deprecation notices. GraphQL is especially useful for managing multiple data sources by efficiently meshing information together to be available from one API.

Diving into the “Why” of GraphQL

Strong developer experience

  • User-friendly: With GraphQL, developers think in terms of fields and relationships, not endpoints, domains, or complex joins
  • Developer onboarding: Provides often missing and required insights when new team members need to quickly get up to speed with the company API and data landscape. GraphQL’s schema is easy to navigate and learn from. It allows front-end developers to interact with different APIs without requiring familiarity with the implementation details for each of them.
  • Compatible with nearly every popular language available today

Productivity

  • The rate to iterate and to prototype is fast
  • It is no longer necessary to specify data requirements on the server side, such as which fields from which models need to be fetched and exposed. With GraphQL, the product defines a schema, and as a result, the model. The schema is the data model with all the properties it’s carrying. How to use the schema and consume its data is defined on the client side.

Flexibility

  • GraphQL server with connected databases via TCP or Websockets
  • GraphQL layer that integrates with existing systems/infrastructure
  • Hard-to-build innovative products that need access to multiple systems
  • Unifies legacy systems and hides their complexity
  • Also offers a hybrid approach to the points above

Tools to help create and maintain GraphQL APIs

Because GraphQL is a query language itself, developing a server to support it can be achieved using a broad range of tools, with Apollo serving as a prime example. Apollo GraphQL is an industry standard NodeJS server, as well as a client-side GraphQL query library.

To further automate typing support in your code, GraphQL Code-Gen (client side) and TypeGraphQL (server side, NodeJS) can be implemented to automatically add known typing and queries and generate return values.

When it comes to coding, the hassle can be avoided (almost) entirely when using a tool like Hasura, which instantly and directly converts databases, REST endpoints, and other GraphQL APIs into GraphQL hub, and can combine them for a unified GraphQL. When there is no need for custom coding to maintain GraphQL, your API team can quickly and easily scale without a full group of developer resources on hand.

Tools to help create and maintain GraphQL APIs

Co-authored by Mike Wzorek, with BCG X. Cross posted from LinkedIn.

--

--

Maxwell Struever
Maxwell Struever

No responses yet