GraphQL and WordPress — Why They’re Better Together

by Renato Alves

WordPress is going through some fantastic changes right now. Over the past few years, we are seeing a move to a better editing experience with the block editor, aka, Gutenberg. An improvement in development experience in the PHP side, and after the REST API landed in the core of the most-used CMS, it opened several doors for the growing headless space, where several sites and companies are using WordPress as a CMS but delivering it via a fast and interactive front-end.

Much of this success can be ascribed to the latter: ongoing use of Javascript — React specifically — within WordPress. But in the world of site or app development, those are not the newest kids on the block. The newest is GraphQL, a technology built by Facebook ( read the announcement here) a few years ago but, just recently, WordPress developers have been getting up to speed with it.

Much of the praise and success GraphQL represents is because of its new ideology when it comes to consuming APIs. Hitherto, using the REST API, anyone that needed to fetch information would usually get more than they needed. Instead, with GraphQL, clients and consumers are able to declaratively fetch only the information they need for the page or request, which has had big ramifications in how developers code and approach projects.

So for this brief article on the matter, I’d like to make a case why GraphQL and WordPress fit well together, how you can use it today with WordPress, and how this technology will increase velocity and improve productivity.

So first, let’s see how you can add it to your WordPress stack right now.

Using GraphQL with WordPress

One of the leading WordPress plugins in the ecosystem is WPGraphQL. It provides a GraphQL API schema with WordPress data types. Meaning that you can query posts (post types), pages, comments, authors, categories (taxonomies), settings, etc, via its API.

The plugin has many other extensions for WooCommerce, BuddyPress (which I am the creator), and other known plugins like Yoast SEO. And it is an easy to plug-and-play plugin that quickly allows your team to have access to your WordPress information via GraphQL in their front-end or apps.

Now, let’s see where GraphQL stands in the APLM process/workflow.

GraphQL and APLM

Design

Meaning that, with GraphQL, if you need a new field, there is no need to go to a fresh code iteration in the back-end. You only need to establish the scope of data from the schema and they would be exposed and be readily available to you.

Development

Another advantage of using GraphQL is its ease of integration with multiple data sources. Meaning that your API can return information from your, possibly, several databases and APIs (including legacy REST APIs).

There are also other technical benefits such as API/Schema discovery, IDE support, and tools that the GraphQL ecosystem provides.

Retiring / Deprecating

Also, tracking API usage is hard with REST. We can’t know for sure when attributes and fields are being used and how much. That’s different with GraphQL. Since it has its typed schema, tracking usage is possible and much easier, including deprecated fields and attributes. You don’t need to change endpoints to accomplish that and even deprecated information can keep working as long as needed while the front-end is being migrated to the new fields/attributes.

Cons

A few of those are:

  • There is a steep learning curve in the early days, especially if your developers are not well versed with it. So migrating completely to GraphQL without understanding its risks can be challenging.
  • There are some rough-edges like caching.
  • Access-control: especially field control, is possible but not as easy as one might think.

Just bear in mind that you might find some hiccups in the process, especially if you are not well versed in it.

Increasing Velocity and Improving Productivity

What questions do you have about GraphQL? What projects are you thinking it might help you create? Maybe we can help! Reach out to us on Twitter.

Originally published at https://alley.co on April 16, 2021.

We are strategists, researchers, designers, and developers who craft digital experiences for publishers, nonprofit institutions, museums, and brands. alley.co

We are strategists, researchers, designers, and developers who craft digital experiences for publishers, nonprofit institutions, museums, and brands. alley.co