Using GraphQL on WordPress

Rate this content
Bookmark

WordPress has been around for a really long time and is the most popular CMS on the web. Consuming its REST API however to build modern static sites leaves a lot to be desired. In this talk, we’ll live code a site that consumes WordPress over GraphQL and see the power of GraphQL in making mature and familiar services, easy to consume.

This talk has been presented at GraphQL Galaxy 2020, check out the latest edition of this Tech Conference.

FAQ

Sid is a developer who lives in Mumbai, India. He works at Gatsby Inc and helps maintain Gatsby open source projects.

Sid is currently working on Gatsby Functions, a serverless implementation built into Gatsby.

Sid is writing a book called 'Web Development for Everyone.' You can find it at webdevelopmentforeveryone.com.

Sid enjoys scuba diving, learning to fly a plane, and is trying to buy a PS5.

The Jamstack is a modern web development architecture. Common frameworks used with the Jamstack include Gatsby, Next, Hugo, and Eleventy.

WordPress is popular because it powers about 30% of all websites, offers a familiar writing experience, has a wealth of plugins, is open source, and is flexible.

Common issues with using the WordPress REST API include overfetching, underfetching, and requiring multiple network requests to render a single page.

WPGraphQL improves data fetching by providing a one-click GraphQL API that reduces the number of network requests and handles data more efficiently.

Using WPGraphQL over the WordPress REST API reduces the number of network requests, decreases data load times, and improves scalability.

WPGraphQL supports popular WordPress plugins like WooCommerce and Advanced Custom Fields through additional plugins.

Sid Chatterjee
Sid Chatterjee
26 min
02 Jul, 2021

Comments

Sign in or register to post your comment.
Video Summary and Transcription
The video discusses how to build modern websites using WordPress and GraphQL. WordPress powers about 30% of websites and offers a flexible CMS with many plugins. However, its REST API can cause overfetching and underfetching issues. By using the WPGraphQL plugin, you can switch to GraphQL and solve these problems efficiently. The speaker demonstrates this with a news site, showing that while the REST API needed nine requests to render a page, WPGraphQL required only one. The video also covers setting up GraphQL queries, using urql, and transforming data to render correctly. Popular WordPress features like WooCommerce and Advanced Custom Fields are supported by WPGraphQL. Gatsby is mentioned as a static site generator that integrates well with WordPress and WPGraphQL. The new Gatsby Source WordPress plugin offers real-time preview and incremental builds, making it ideal for large sites. The video concludes with a Q&A session addressing data subscriptions and community contributions.
Available in Español: Usando GraphQL en WordPress

1. Introduction to WordPress and GraphQL

Short description:

Hi, I'm Sid and today I'm going to be talking about building modern websites with WordPress and GraphQL. WordPress is still really popular, powering about 30% of all websites on the web in 2020. It has a familiar writing experience and a wealth of plugins. With WordPress, you're not tied into a specific platform and there are no limits on the number of users or pricing. However, when consuming the WordPress CMS instance using its REST API, familiar issues like overfetching and underfetching arise, resulting in multiple network requests for rendering a simple post.

Hi, I'm Sid and today I'm going to be talking about building modern websites with WordPress and GraphQL. So a little bit about me, I live in Mumbai in India. I work at Gatsby Inc, I help maintain Gatsby open source, I also helped build incremental builds on Gatsby Cloud. Currently, I'm working on Gatsby Functions, which is a serverless implementation built into Gatsby, it's coming soon. I'm also writing a book called web development for everyone. You can check it out at webdevelopmentforeveryone.com.

Besides that more stuff about me but fun stuff now, I like diving, especially in wrecks and jacks. I'm also learning how to fly a plane this year on weekends. And I'm also trying to buy a PS5 if you know where it's in stock, tell me please.

So now let's talk about the Jamstack because that's what I get paid to talk about. So if you like the Jamstack like me, you're probably building sites using Gatsby or Next or Hugo or Eleventy. And when you build these static sites, you're likely going to pull data from multiple different data sources. One of which could be a CMS. Now there's a lot of great CMSs to pick from. These include Contentful, Sanity, Tattoo CMS. But turns out WordPress is still really popular. WordPress still powers about 30% of all websites on the web in 2020. And WordPress is a great CMS. I mean, you know, it's got a familiar writing experience. People have been using it for years now, so they're really comfortable with how it works. It's got a wealth of plugins to do really anything you want to do with it, including stuff like advanced custom fields, WooCommerce, free e-commerce, and so on. WordPress also happens to be open source, and that's great because that means there's no limit on, I mean, you can build it anywhere and run it anywhere. You don't have, you're not tied into a specific platform. And typically, with a lot of CMSs, we see that users, you know, the number of users you can have on a space is typically limited, and that ends up being a bottleneck, especially when it comes to pricing. With WordPress, this isn't the case. So WordPress is still super flexible, and it's a great CMS to use. But if you're building a site in today's day and age, you're probably using a JavaScript framework, you could be using React, Vue, Svelte, really anything you like, but when you build your site using one of these frameworks, you're typically going to consume your WordPress CMS instance using its REST API. And the moment REST API is getting to the picture, we're back to seeing those familiar issues, right? Stuff like overfetching, stuff like underfetching. And it turns out that rendering a simple post then becomes a matter of more than one, two or even five network requests. Let's look at a concrete example of what this looks like.

2. Building Modern Websites with WPGraphQL

Short description:

This is a site that I built up called The Good News Times. It resembles popular news sites and has cards representing articles with images, titles, text, authors, and author images. To render this page, using the REST API requires nine API calls for just four posts, which is slow, data-intensive, and not scalable. To address this, we can move to GraphQL, specifically using the WPGraphQL plugin for WordPress.

This is a site that I built up. This is a mockup. And it's called The Good News Times because we could all use some of that. And this resembles a lot of popular news sites. And as you can see, there's a bunch of cards. Each card is an article. It has an image, it has a title, some text. It also has an author and an author image. Right. And we have four articles on this home page.

Now to render this page, which again, like I said, has posts and authors, you need to make nine API calls to WordPress if you happen to use the REST API. And I've listed all of these API calls and you'll see that there's one to get all the posts themselves, and then there's one for each author and one for each featured image. That sums up to nine API calls for just four posts. And that's a lot. Right. Now, you know, in the past, we've used GraphQL to wrap REST APIs. And when you do that, on the client, that kind of helps simplify your code base. But stuff is still slow because you're still making nine API calls. And it's a lot of data over the wire. Right. And it's also brittle because if you happen to have code that maybe one of those API calls fails in, you're still going to have to sort of handle that for each and every one of them and make sure you gracefully take care of all those error cases. Also, needless to say, this doesn't scale really well. We're looking at a lot of API requests for a single page load. And the moment we talk at scale, this stuff adds up.

So let's try to move this over to GraphQL. That's what this talk is all about. It's about building modern websites on WordPress with GraphQL. So the way we're going to do this is we're going to use WPGraphQL. If you haven't seen WPGraphQL before, it's a plugin for WordPress. It happens to give you a one-click GraphQL API right out of the box.

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
Tom Pressenwurter introduces Redwood.js, a full stack app framework for building GraphQL APIs easily and maintainably. He demonstrates a Redwood.js application with a React-based front end and a Node.js API. Redwood.js offers a simplified folder structure and schema for organizing the application. It provides easy data manipulation and CRUD operations through GraphQL functions. Redwood.js allows for easy implementation of new queries and directives, including authentication and limiting access to data. It is a stable and production-ready framework that integrates well with other front-end technologies.
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
This Talk discusses handling local state in software development, particularly when dealing with asynchronous behavior and API requests. It explores the challenges of managing global state and the need for actions when handling server data. The Talk also highlights the issue of fetching data not in Vuex and the challenges of keeping data up-to-date in Vuex. It mentions alternative tools like Apollo Client and React Query for handling local state. The Talk concludes with a discussion on GitLab going public and the celebration that followed.
Batteries Included Reimagined - The Revival of GraphQL Yoga
GraphQL Galaxy 2021GraphQL Galaxy 2021
33 min
Batteries Included Reimagined - The Revival of GraphQL Yoga
Envelope is a powerful GraphQL plugin system that simplifies server development and allows for powerful plugin integration. It provides conformity for large corporations with multiple GraphQL servers and can be used with various frameworks. Envelope acts as the Babel of GraphQL, allowing the use of non-spec features. The Guild offers GraphQL Hive, a service similar to Apollo Studio, and encourages collaboration with other frameworks and languages.
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
The Talk discusses the challenges and advancements in using GraphQL and React together. It introduces RedwoodJS, a framework that simplifies frontend-backend integration and provides features like code generation, scaffolding, and authentication. The Talk demonstrates how to set up a Redwood project, generate layouts and models, and perform CRUD operations. Redwood automates many GraphQL parts and provides an easy way for developers to get started with GraphQL. It also highlights the benefits of Redwood and suggests checking out RedwoodJS.com for more information.
Adopting GraphQL in an Enterprise
GraphQL Galaxy 2021GraphQL Galaxy 2021
32 min
Adopting GraphQL in an Enterprise
Today's Talk is about adopting GraphQL in an enterprise. It discusses the challenges of using REST APIs and the benefits of GraphQL. The Talk explores different approaches to adopting GraphQL, including coexistence with REST APIs. It emphasizes the power of GraphQL and provides tips for successful adoption. Overall, the Talk highlights the advantages of GraphQL in terms of efficiency, collaboration, and control over APIs.
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
GraphQL has made a huge impact in the way we build client applications, websites, and mobile apps. Despite the dominance of resolvers, the GraphQL specification does not mandate their use. Introducing Graphast, a new project that compiles GraphQL operations into execution and output plans, providing advanced optimizations. In GraphFast, instead of resolvers, we have plan resolvers that deal with future data. Graphfast plan resolvers are short and efficient, supporting all features of modern GraphQL.

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
Build Modern Applications Using GraphQL and Javascript
Node Congress 2024Node Congress 2024
152 min
Build Modern Applications Using GraphQL and Javascript
Featured Workshop
Emanuel Scirlet
Miguel Henriques
2 authors
Come and learn how you can supercharge your modern and secure applications using GraphQL and Javascript. In this workshop we will build a GraphQL API and we will demonstrate the benefits of the query language for APIs and what use cases that are fit for it. Basic Javascript knowledge required.
End-To-End Type Safety with React, GraphQL & Prisma
React Advanced 2022React Advanced 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