Exploring the WordPress Graph with Next.js & WPGraphQL

Rate this content
Bookmark

Headless Wordpress using its built-in REST API is a powerful solution to scale WordPress to the web, but complex relationships can easily turn into chains of requests, making maintainability difficult along with the potential cost of performance.


With WPGraphQL, we can harness the benefits of GraphQL, leading to a better developer experience and optimized request logic, making sure we’re only delivering what we need to our users.

We’ll explore these advantages and how these pieces fit together with modern tools like Next.js to build great experiences for the web.

Slides & more

Colby Fayock
Colby Fayock
23 min
09 Dec, 2021

Comments

Sign in or register to post your comment.

Video Summary and Transcription

WordPress is widely used, and it now supports a REST API for headless usage. Serving static HTML files allows for infinite scaling and surviving viral traffic. GraphQL can be used to interface with WordPress data, reducing complexity. WordPress can be coupled with plugins like Yoast and ACF, and WPGraphQL works seamlessly with these plugins. GraphQL allows for selecting only necessary data and has performance advantages over REST APIs.

1. Introduction to WordPress and Headless CMS

Short description:

WordPress is still widely used, with 37% of the top one million sites using it. It now supports a REST API, allowing for headless usage. Static site generators and modern web frameworks can do the heavy lifting before the page loads, avoiding the need for client-side requests to WordPress.

B-R-E-A-C-K-E-R-Y-S-T-Y-F-R-O-K-K-Y-S-T-Y-F-R-O-K-K-Y-S-T-Y-F-R-O-K-K-Y-S-T-Y-F-R-O-K-K-Y-S-T-Y-F-R-O-K-K-Y-S-T-Y-F-R-O-K-K-Y-S-T-Y-F-R-O-K-K-Y-S-T-Y-F-R-O-K-K-Y-S-T-Y-F-R-O-K-K-Y-S-T-Y-F-R-O-K-K-Y-S-T-Y-F-R-O-K-K-Y-S-T-Y-F

You can pretty much find me anywhere on the web by just Googling my name, as I'm the only one in the world. So let's start off by addressing the CMS in the room. It's 2021, and some developers would still cringe at the thought of using WordPress. But frankly, we're still living in a WordPress world. According to Build With Trends a little bit ago, if we look at the CMS distribution of the top one million sites, 37% of websites are using WordPress. That's a huge percentage. And I'm sure it's gonna have gone up by now, and I'm not quite sure how accurate it is, but if you look at the number of detections on the Build With site, it's over 960 million installs of WordPress. That's almost one billion. That's a staggering number.

While we might not all want to use WordPress, it's realistically around to stay for the foreseeable future. But hold up, why am I even talking about WordPress? Well, jumping back a bit, WordPress, as we traditionally know it, is an all-in-one CMS and website solution. It works side to grab all the data from the database, render it to HTML, and then send it over to the browser. But since 4.7, WordPress now supports a REST API. This means right out of the box we actually can use WordPress headlessly. If you haven't heard of the term headless before, what does that actually mean? Well, with our traditional stack like WordPress, somebody will visit a page in the browser. The browser reaches out to the server, the server will do the work like make those requests to the database, render the HTML for the page, and then send that response. If we're lucky, it'll return it cached. Finally, the browser will display that response to the person who's visiting that site. With a headless approach, that request to the server might be asynchronous on the client. In this particular example, the person would visit a page in their browser and immediately get a response straight from storage. Once that page loads inside of the browser, the browser will kick off another request to a server which can load all of that dynamic content. But I would imagine you probably wondering why would we want to make a client-side request to WordPress for a CMS? That's not necessarily the recommended approach. That's where static site generators and modern web frameworks come in. They do all the hard lifting before the page actually hits the browser. We can use front-end frameworks to get all that data at compile time or with server-side rendering, avoiding that cost inside of the browser. Now, if this all sounds new to you, it might sound like a lot of work. Why not even, why even bother with an API? Why not use WordPress out of the box like we always do? So let's focus on this example of using an API with a static site, where the only time we reach out to that API is at compile time. And we store the HTML files directly in storage. We get a lot of benefits from actually avoiding hitting the server directly on each of those requests. With most of our base solutions, server-based solutions like WordPress, there's a lot of options to help speed things up.

2. WordPress and Headless CMS

Short description:

WordPress plugins and custom work can help with caching, but each page is still a server hit. Static HTML files, served from storage or a CDN, are faster. Load balancing and auto scaling are not perfect solutions. Serving static HTML files allows for infinite scaling and surviving viral traffic. Storage is cheap, and managing servers can be costly. The REST API in WordPress allows for fetching all data, including blog posts and author information.

For WordPress specifically, that includes some plugins to cache or some custom work under the hood, but each page is still a server hit, which is still prone to its ups and downs. On the other hand, with our statically compiled site, a static HTML file is just going to be fast. Instead of spending time rendering on a server, you serve a static file straight from storage or a CDN. While you can do this by default with WordPress, it's often much more complicated. And some of the plugins that cache might serve an HTML file, but they still serve it from a regular server, not static storage.

With any server, we're typically paying for how much we expect our traffic to be. While most of the time that is predictable, we all hope to one day have a viral post, and if that happens, the people visiting our site will be the ones paying for it with slow speeds or timeouts. There are solutions like load balancing and auto scaling, but those aren't perfect solutions, and we might not always be able to handle that certain traffic.

Back to the fact that we're serving static HTML files, because we're serving files straight from storage, or better yet, statically from the CDN, buzzword alert, that means our user-facing website is going to infinitely scale. That static site will survive the Reddit hug of death when your post goes viral. But managing servers isn't always cheap. While a low traffic personal blog could maybe manage a few to be a few dollars a month, the more that traffic grows, the more that cost is going to grow. While you do still have those options like load balancing and auto scaling, the services add up really quickly. Without it, you, again, risk your site slowing down, or worse, downtime. But storage is cheap. It's really cheap. We can maintain huge static projects in AWS using S3 for a really low cost. But even if we still manage a server, the usage is going to be much, much lower because we're only having to deal with content managers or requests at compile time.

Now, I hopefully convinced you as to why headless is a good thing. Or at least set up some context about what's actually coming next. So, how do I apply this to WordPress? Well, back to the REST API. If I want to start building out my new static blog, I'll ultimately need to fetch all my data. I'll start off by going to WP.json, which is going to be some basic info about my website, along with a list of routes showing what endpoints are actually available to hit. Next, I want to add my blog posts. So, I can hit the post endpoint and easily get them all. I can then simply load them up inside of my application and iterate through all of them inside of the UI. But I want to add more details to this. I want to know, for instance, who the author is. So, I find the author ID in the post, and it probably doesn't make sense to hit each, every individual author one by one, because that could be a lot of authors. So, I hit the author's endpoint, and I try to match up the data.

QnA

Check out more articles and videos

We constantly think of articles and videos that might spark Git people interest / skill us up or help building a stellar career

From GraphQL Zero to GraphQL Hero with RedwoodJS
GraphQL Galaxy 2021GraphQL Galaxy 2021
32 min
From GraphQL Zero to GraphQL Hero with RedwoodJS
Top Content
We all love GraphQL, but it can be daunting to get a server up and running and keep your code organized, maintainable, and testable over the long term. No more! Come watch as I go from an empty directory to a fully fledged GraphQL API in minutes flat. Plus, see how easy it is to use and create directives to clean up your code even more. You're gonna love GraphQL even more once you make things Redwood Easy!
Local State and Server Cache: Finding a Balance
Vue.js London Live 2021Vue.js London Live 2021
24 min
Local State and Server Cache: Finding a Balance
Top Content
How many times did you implement the same flow in your application: check, if data is already fetched from the server, if yes - render the data, if not - fetch this data and then render it? I think I've done it more than ten times myself and I've seen the question about this flow more than fifty times. Unfortunately, our go-to state management library, Vuex, doesn't provide any solution for this.For GraphQL-based application, there was an alternative to use Apollo client that provided tools for working with the cache. But what if you use REST? Luckily, now we have a Vue alternative to a react-query library that provides a nice solution for working with server cache. In this talk, I will explain the distinction between local application state and local server cache and do some live coding to show how to work with the latter.
Batteries Included Reimagined - The Revival of GraphQL Yoga
GraphQL Galaxy 2021GraphQL Galaxy 2021
33 min
Batteries Included Reimagined - The Revival of GraphQL Yoga
The Guild has recently released Envelop - a new, modern GraphQL Server Framework and plugin system. In this talk I’ll share a brief overview of Envelop and why you should probably upgrade your existing GraphQL server to it.
Rock Solid React and GraphQL Apps for People in a Hurry
GraphQL Galaxy 2022GraphQL Galaxy 2022
29 min
Rock Solid React and GraphQL Apps for People in a Hurry
In this talk, we'll look at some of the modern options for building a full-stack React and GraphQL app with strong conventions and how this can be of enormous benefit to you and your team. We'll focus specifically on RedwoodJS, a full stack React framework that is often called 'Ruby on Rails for React'.
Step aside resolvers: a new approach to GraphQL execution
GraphQL Galaxy 2022GraphQL Galaxy 2022
16 min
Step aside resolvers: a new approach to GraphQL execution
Though GraphQL is declarative, resolvers operate field-by-field, layer-by-layer, often resulting in unnecessary work for your business logic even when using techniques such as DataLoader. In this talk, Benjie will introduce his vision for a new general-purpose GraphQL execution strategy whose holistic approach could lead to significant efficiency and scalability gains for all GraphQL APIs.

Workshops on related topic

Build with SvelteKit and GraphQL
GraphQL Galaxy 2021GraphQL Galaxy 2021
140 min
Build with SvelteKit and GraphQL
Top Content
Featured WorkshopFree
Scott Spence
Scott Spence
Have you ever thought about building something that doesn't require a lot of boilerplate with a tiny bundle size? In this workshop, Scott Spence will go from hello world to covering routing and using endpoints in SvelteKit. You'll set up a backend GraphQL API then use GraphQL queries with SvelteKit to display the GraphQL API data. You'll build a fast secure project that uses SvelteKit's features, then deploy it as a fully static site. This course is for the Svelte curious who haven't had extensive experience with SvelteKit and want a deeper understanding of how to use it in practical applications.

Table of contents:
- Kick-off and Svelte introduction
- Initialise frontend project
- Tour of the SvelteKit skeleton project
- Configure backend project
- Query Data with GraphQL
- Fetching data to the frontend with GraphQL
- Styling
- Svelte directives
- Routing in SvelteKit
- Endpoints in SvelteKit
- Deploying to Netlify
- Navigation
- Mutations in GraphCMS
- Sending GraphQL Mutations via SvelteKit
- Q&A
End-To-End Type Safety with React, GraphQL & Prisma
React Advanced Conference 2022React Advanced Conference 2022
95 min
End-To-End Type Safety with React, GraphQL & Prisma
Featured WorkshopFree
Sabin Adams
Sabin Adams
In this workshop, you will get a first-hand look at what end-to-end type safety is and why it is important. To accomplish this, you’ll be building a GraphQL API using modern, relevant tools which will be consumed by a React client.
Prerequisites: - Node.js installed on your machine (12.2.X / 14.X)- It is recommended (but not required) to use VS Code for the practical tasks- An IDE installed (VSCode recommended)- (Good to have)*A basic understanding of Node.js, React, and TypeScript
GraphQL for React Developers
GraphQL Galaxy 2022GraphQL Galaxy 2022
112 min
GraphQL for React Developers
Featured Workshop
Roy Derks
Roy Derks
There are many advantages to using GraphQL as a datasource for frontend development, compared to REST APIs. We developers in example need to write a lot of imperative code to retrieve data to display in our applications and handle state. With GraphQL you cannot only decrease the amount of code needed around data fetching and state-management you'll also get increased flexibility, better performance and most of all an improved developer experience. In this workshop you'll learn how GraphQL can improve your work as a frontend developer and how to handle GraphQL in your frontend React application.
Build a Headless WordPress App with Next.js and WPGraphQL
React Summit 2022React Summit 2022
173 min
Build a Headless WordPress App with Next.js and WPGraphQL
Top Content
WorkshopFree
Kellen Mace
Kellen Mace
In this workshop, you’ll learn how to build a Next.js app that uses Apollo Client to fetch data from a headless WordPress backend and use it to render the pages of your app. You’ll learn when you should consider a headless WordPress architecture, how to turn a WordPress backend into a GraphQL server, how to compose queries using the GraphiQL IDE, how to colocate GraphQL fragments with your components, and more.
Relational Database Modeling for GraphQL
GraphQL Galaxy 2020GraphQL Galaxy 2020
106 min
Relational Database Modeling for GraphQL
Top Content
WorkshopFree
Adron Hall
Adron Hall
In this workshop we'll dig deeper into data modeling. We'll start with a discussion about various database types and how they map to GraphQL. Once that groundwork is laid out, the focus will shift to specific types of databases and how to build data models that work best for GraphQL within various scenarios.
Table of contentsPart 1 - Hour 1      a. Relational Database Data Modeling      b. Comparing Relational and NoSQL Databases      c. GraphQL with the Database in mindPart 2 - Hour 2      a. Designing Relational Data Models      b. Relationship, Building MultijoinsTables      c. GraphQL & Relational Data Modeling Query Complexities
Prerequisites      a. Data modeling tool. The trainer will be using dbdiagram      b. Postgres, albeit no need to install this locally, as I'll be using a Postgres Dicker image, from Docker Hub for all examples      c. Hasura
Fetch, useEffect, React Query, SWR, what else?
React Advanced Conference 2023React Advanced Conference 2023
102 min
Fetch, useEffect, React Query, SWR, what else?
Top Content
WorkshopFree
Ondrej Polesny
Ondrej Polesny
In this workshop, first, we’ll go over the different ways you can consume APIs in React. Then, we’ll test each one by fetching content from a headless CMS (with both REST and GraphQL) and checking in detail how they work.
While there is no advanced React knowledge required, this is going to be a hands-on session, so you’ll need to clone a preconfigured GitHub repository and utilize your preferred React programming editor, like VS Code.
You will learn:- What diverse data fetching options there are in React- What are advantages and disadvantages of each- What are the typical use cases and when each strategy is more beneficial than others