Decoupling Drupal - What To Consider While Using JSON API?

  • By: Ganesh
  • Date: 12-09-2018

"The most compelling advantage of API-first Drupal is not the way it negates Drupal's presentation features, it's the way it enables the capabilities of the CMS to power multiple applications and devices.”— Jason Enter, Manager Technical Services, Acquia.

The relationship between content and code is not a healthy one anymore. The consumer-facing presentation layer is not as removed from the content editor layer, as it used to be, mainly because of the new interactive experiences being provided to consumers through apps.

Modern frameworks and programming languages allow for flexibility in the formation of your content management stack. Thus, decoupling content from code has become a common practice in the recent years and Drupal was one of the early adopters of the same.

What is “Decoupled”?

Well, decoupled Drupal does sound "cool" doesn't it? Seems like everybody is talking about it, so it must be a better, more easier solution right? Well, maybe. Offering a wide array of benefits, this strategy, also known as "headless drupal" is used to exploit Drupal solely as a Content Management System without a presentation layer. i.e there is only content and the administrative UI, sans the public facing themes and the other templates, thus exposing an API for other applications.

But perhaps the most important question that you have in mind would be, what are the benefits of decoupled architecture?

A headless architecture does a great job in reducing or completely eleminating most of the problems that are a part of the monolithic content management systems.

Simplified upgrades: A decoupled architecture ensures that a CMS upgrade does not affect the presentation layer, thus keeping you from having to set-up again after every CMS upgradation.

Better focus: A huge advantage for developers, with decoupled architecture, they only need to be concerned about setting up their part of the stack. Back-end developers need not worry about compiling CSS and the front-end guys need not scratch their heads about composer installs.

Easy troubleshooting: A headless architecture is structured in such a way that the platform consists of smaller, discrete parts rather than a single giant application. This makes it a lot easier to locate bugs and troubleshoot them.

Improved performance: With both the teams working simultaneously in a decoupled manner, development of new features can happen seamlessly without running into walls. This in turn improves the overall performance of the platform.

Easy troubleshooting: A headless architecture is structured in such a way that the platform consists of smaller, discrete parts rather than a single giant application. This makes it a lot easier to locate bugs and troubleshoot them.

How Important is JSON for a Decoupled Website?

Powerful architectures, responsive and user-friendly web applications are a result of JavaScript frameworks, that has become quite popular since its introduction in early 2010. And with an increasing number of people opting to use mobile devices to access digital content, building native applications for a website makes a lot of sense.

Realizing this potential, Drupal has added the support for building RESTful APIs into the core. However, this comes with its own drawbacks as the RESTful services of Drupal core does not provide robust solutions out of the box.

A traditional REST API implementation usually come face to face with a certain set of problems:

  • An entire data graph needed to construct the view in the consumer will require too many requests.
  • A significant response bloat returning all the fields and relationships of every entity.
  • Finding content based on the data attributes of the entities is a herculean task.
  • Versioning the API so consumers and back-end implementations can iterate separately.

In order to address these problems, there are several specifications out there, JSON API being one of the most prominent ones.

What is JSON?

JSON - JavaScript Object Notation is one of the most common data format for the exchange of data over web services. Primarily replacing XML, the lightweight and easy to read nature of JSON makes it quite awesome.

Why you should consider implementing JSON specifications?

A set of pre-defined standards and conventions that describe how the APIs should be served by the servers and consumed by the clients for exchanging data in JSON format, the JSON API specifications offer key benefits when implemented:

Efficiency: The JSON API specifications are designed in such a way that, they keep the number of requests and the size of the data to bare minimum.

Productivity: It so often happens that, as a devloper you are quite confused about which best practices to follow when building an API. On the other hand, by following a set of standards you are out of dilema.

Consistency: The specifications offer a consistent structure and behaviour while being consumed to build applications.

Support: The specifications are widely accepted and the implementations required for client libraries can be found for almost every framework and programming language.

The JSON API Module

When you install and enable the JSON API module, a REST API is immediately available for every type within your Drupal implementation. By traversing entity types and bundles such that it can generate URL's at which it can access and manipulate entities, the module achieves this feat.

While the JSON API is quite a complex specification to deal with, the JSON API module does a great job in simplifying the process of consuming it, especially for early decoupled Drupal developers.
tweet this

However, by following the approach of "less configurations" and production readiness right of the box, the JSON API module does come with a set of disadvantages of its own.

Support: The JSON API module is entirely entity-based, which means that it is centered around the Drupal's entity types and bundles. However, the module fails to support or handle custom data.

Task Failure: Since the module is entity-based and cannot handle custom data, it also fails to perform other tasks such as login, creating sessions or logging out a user etc.

Pre-defined Structure: The JSON API Module presents a pre-defined payload structure that cannot be modified. Once the module is enabled in Drupal, the consistent payload structure cannot be changed or customized with change in vendor requirements

Learning curve: As JSON API module exposes all the attributes of an entity, it might get complex in nature. With quite a steep learning curve, JSON API may not be everyone's cup of tea.

How to download and install JSON API Module?

Having a dependency on serialization module, it has to be enabled first before downloading and installing JSON API. This can be done by using any of the following methods:

Using Drush

$ drush en serialization -y

$ drush dl jsonapi && drush en jsonapi -y

Using Drupal Console

$ drupal module:install serialization

$ drupal module:download jsonapi && drupal module:install jsonapi

Subscribe For Our Newsletter And Stay Updated

Recent blogs

thunder for drupal
Digital evolution has taken the world of professional publishing by storm. However, this evolution brought along a whole new set of...
higher education
Over the past 2 decades, the advancements in technology have been tremendous and these changes have played a major role in allowing...
DrupalCamp 2018
“Everyone you will ever meet knows something you don’t” - said Bill Nye popularly known as The Science Guy, who (coincidentally)...

Explore Our Drupal Services

Take me there

This website uses cookies to offer you an enhanced website experience. We collect information about how you interact with our website to provide personalized services to you. To find out more, see our Privacy Policy

×