Going headless with Next.js and WPGraphQL | Buddy Webinar #23
Going headless with Next.js and WPGraphQL | Buddy Webinar #23
In this webinar, Fran Agulto will show us how to combine Jamstack with WordPress by decoupling it and bringing it into the modern API-driven web landscape!\r \r Who is the webinar for:\r - Traditional WordPress developers who want to try the headless approach\r - Developers who didn’t have a chance to try the WP + Next.js + GraphQL combo\r - Project leaders who are thinking about jumping onto the headless bandwagon\r \r What you’re going to learn:\r - How to build a simple Next.js application with WordPress at the backend\r - How to use some cool Next.js features\r - Brief introduction to GraphQL and WPGraphQL\r - The basics of setting up a Jamstack pipeline in Buddy with testing and deployment
[00:00] Introduction [04:40] What does “headless” mean? [06:05] Why we should decouple WordPress [13:30] How to convert WordPress to a headless site [19:33] Connecting NextJS with WordPress using GraphQL [49:36] Deploying NextJS using Buddy [1:05:02] Q\u0026A
Content
72.99 -> Maciek: Hello, everyone, welcome
to yet another Buddy webinar.
77.16 -> Today we are going to dive into
the world of headless WordPress.
82.8 -> And together with Fran Agulto,
we will try to create a kind of
88.71 -> simple WordPress index js
application together with a bit
93.39 -> of Graph QL. So friend, because
you are much more of an expert
98.7 -> when it comes to headless and
everything, tell us a few words
103.83 -> about yourself.
105.47 -> Fran: Yeah, absolutely Maciek.
I'm super stoked to be here. And
109.43 -> my kind of battle cry if you
will, is I'm jam stoked to be
113.54 -> here Extra extra excited. Thank
you Maciek for having me. So a
118.94 -> little bit about myself just to
know where your information is
122.27 -> coming from. I'm a dev rel
advocate at WP Engine focused on
127.16 -> headless WordPress. I started my
career actually, in tech at WP
132.89 -> engine on the sales side of
things. And I was telling Maciek
137.27 -> offline when we first met, I was
like, you know, the inner nerd
140.66 -> hit me. And during COVID, one of
the one one of the few things I
146.06 -> mean, there's mostly negative
with COVID. But one of the
148.61 -> positive things is, when we work
from home, I had so much time to
152.06 -> learn. And I was like, You know
what? I saw a demo that our old
158.15 -> Developer Relations Manager Matt
landers did on headless
162.35 -> WordPress. And I'm like, You
know what? I Maciek. I said to
165.71 -> myself, I need to learn that I
enrolled in a JavaScript
169.25 -> bootcamp focus on react in the
mean stack. And the rest is
172.94 -> history. Now I'm here two and a
half years later and still
175.49 -> breaking React code. So
178.89 -> Maciek: yes, and now, we are we
are here. So this is I mean,
182.67 -> yeah, this is one of the great
things about COVID we had more
185.73 -> time to learn really cool and
amazing things. So they're one
191.49 -> of the few things we can say
that COVID was cool about. And I
197.01 -> am Maciek Palmowski and I work
here at Buddy as a WordPress
201.12 -> ambassador. So Oh, and good
news. We have Mwale with us. And
208.14 -> he's saying hi to us. Oh, hi,
Mwale. So today, we'll learn
216.87 -> about how to decouple WordPress
and what does it means what is
220.71 -> this headless WordPress, we will
learn how to create the simple
224.7 -> next JS app using WordPress
Graph QL. And of course, next,
231.15 -> and in the end, we'll try to
deploy everything using Buddy en
235.89 -> and Netlify account. Also, if
you like what we are doing,
243.42 -> remember to press the subscribe
button on YouTube. You will
247.41 -> always get informed about all
the upcoming webinars. And also
252.06 -> you will have a chance to watch
a lot of interesting stuff that
255.18 -> we have on our channel. And the
last thing if you have any
260.7 -> questions regarding headless
WordPress, don't hesitate to ask
265.62 -> them in the comment section
either on YouTube or LinkedIn or
269.61 -> wherever you are watching this.
Okay, so I think everything I've
275.22 -> remembered about everything,
yes. So we can start talking
279.18 -> about headless WordPress. So
let's start with the basic
282.48 -> question. What does it mean?
What does it mean headless?
286.77 -> Fran: Yeah, that's a that's a
good start to the webinars to
290.61 -> ask that basic question. So
headless WordPress essentially,
294.78 -> in a few words, Maciek is the
decoupled approach A lot of
300.63 -> cutting off the front end
presentation layer that PHP
304.86 -> usually handles in a monolithic
sense, with traditional
308.34 -> WordPress, and allowing a front
end JavaScript framework within
313.05 -> the node ecosystem to handle the
front end UI layer. And then
317.4 -> making WordPress basically just
a database with an endpoint that
321.66 -> you're going to consume that
data out of. So essentially,
324.6 -> that's, that's, that's what it
is. It's cutting the hence the
328.23 -> word headless. It's not because
the you know, Sleepy Hollow and
332.76 -> Headless Horseman story that we
we knew when we were kids during
337.65 -> Halloween. No, it's, it's not
that, although it kind of
340.65 -> relates to that when people
think oh, Fran, what is headless
342.87 -> WordPress? Is it? Does it have
to do with a headless horseman?
345.33 -> No, you're literally cutting off
the head of WordPress, and
348.87 -> making it having having the
JavaScript front end do the work
353.04 -> on the front end UI. So so
that's what that's what it is.
357.51 -> Now, the first thing I wanted to
like just tell you Maciek unless
362.19 -> did you have like another
question real quick? Or can I
364.86 -> dive right in?
365.76 -> Maciek: Quick question, why? Why
should we decouple WordPress?
369.84 -> Fran: Ah, yes, that's actually a
next. That's a great question.
374.19 -> So why should you decouple
WordPress? Well, there's
378.09 -> actually, there's three main
reasons that you would, okay,
383.67 -> when you sit down as a
developer, and this is something
387.33 -> that, you know, you and I being
in tech Maciek, especially web
391.5 -> development, at the end of the
day. Typically, if you work for
396.03 -> an agency, or you're a mom and
pop that has your own business
400.86 -> that relies on your digital
property, aka, your website, or
404.55 -> your web application, in order
to generate money at the bottom
408.72 -> line, you're going to have to
make a decision on like a now
413.04 -> that it's available, headless,
or traditional. Now, here's the
416.34 -> whys. Okay? If you're if if you
want an uptick of perceived
422.19 -> performance and actual
performance, as far as the jam
426.15 -> stack is concerned, because I
throw that word out there. And I
430.83 -> will for those that aren't
familiar, because there might be
433.92 -> some kind of super new beginners
to the crowd of headless
437.88 -> WordPress, jam, stack Sanford
JavaScript API and markup. And
441.93 -> essentially, that methodology
means that at build time,
446.97 -> meaning not at runtime, because
traditional WordPress is server
450.9 -> side rendered, right, it's
dynamic. So when you hit that
453.96 -> URL, every request is going to
go back to the server, PHP runs,
459.63 -> everything creates the files,
and all the things that you
462.12 -> requested, or that template
sends back into HTML form. And
465.9 -> then there Wallah, you can read
it on the browser. In the
469.59 -> decoupled approach, in this
sense, from a performance uptake
473.25 -> wise, you're not only cutting
the front end off that
476.97 -> JavaScript framework, you're
going to pre build or pre render
481.29 -> everything, meaning you're going
to build everything before a
485.1 -> user requested. Okay. And then
you're going to throw it on a
489.33 -> CDN, which means static files,
which then in turn means Quick
495.93 -> Access. When you hit that
endpoint, literally, there is no
499.74 -> going back to a server, all the
files are right there already.
503.58 -> So that's, that's one thing
Maciek is that like, literally,
507.69 -> the performance uptick is can be
quite noticeable, perceived by
513.96 -> the user, and also within the
tech stack. So that's number
516.87 -> one. Number two, from a security
perspective, when you decouple
523.2 -> your WordPress site, the surface
area of attack for hackers, is
530.37 -> very small. That's number one.
Number two, because your back
534.93 -> end is hidden, and this is with
any decoupled approach it you
538.86 -> could have a Shopify back end,
you could have a Drupal back
542.79 -> end, you could have a MongoDB,
back end, whatever Contentful,
547.32 -> you get the idea. It's hidden,
it's hidden, so they won't even
551.55 -> know you're using WordPress in
this case. So the surface area
555.18 -> of attack is more secure. And
then there's only so much
559.2 -> nefariousness you could really
do in a browser. And then the
563.64 -> third and last thing, and for
me, Maciek, I think this is the
567.48 -> most important because yes, you
could you could aggressively
571.44 -> aggressively cache pages in
traditional WordPress, speed,
575.31 -> all that stuff, right? Depending
on how dynamic the site is,
580.23 -> yada, yada yada. However, from a
future proof perspective, I
585.72 -> truly believe and this is why I
joined the dev rel team here
588.96 -> focused on headless WordPress.
The future of the web as far as
593.04 -> web technology is concerned is
decoupled movement. It's, it's
597.87 -> cutting the front end layer off
and Having third and backend
601.32 -> party services with all that
data on your back end, consumed
605.85 -> by endpoints so that you could
essentially future proof your
609.69 -> front end UI, right? That way
thing. When users are trying to
616.14 -> access your application or
website through different nodes,
620.61 -> a kiosk, a phone, it's much
easier. It's much easier to grab
626.64 -> any front end library or
framework and whatnot and
629.31 -> manipulate that data from your
back end API into what the you
635.34 -> want your users to see. So those
are the three main things that I
638.43 -> think a business or an agency
would decouple. Now, Maciek, let
644.79 -> me just add something on the
flip side. On the flip side, why
648.66 -> wouldn't you? Overkill? Because
I'm telling you, you know,
657.42 -> traditional WordPress is just
fine for small medium businesses
662.07 -> that
664.02 -> don't have that many users like,
or my sister who has a therapy
669.6 -> practice in California, she uses
traditional WordPress, she does
673.38 -> not want to touch code. And
WordPress is excellent for that.
676.8 -> She can just download a bunch of
plugins, manipulate her front
680.07 -> end, and not even ask a
developer to do anything. You
684.66 -> know what I mean?
686.22 -> Maciek: Yeah, this is a great
thing that about WordPress, that
691.47 -> it can be really almost no code
or low code. And the moment we
695.88 -> are trying to go headless, we
require this one extra layer of
701.28 -> work, we have to prepare this
front end. And there was a thing
705.12 -> because, together with friend,
we had that dry run, which
708.45 -> turned into quite an interesting
discussion. And we also
712.17 -> discussed that the headless is a
really great solution when it
715.65 -> comes to the moment when you
are, for example, let's say a
720.45 -> React developer, you don't want
to learn PHP. So in this moment,
725.91 -> why learn PHP, it's easier just
to grab, for example, Gatsby, or
730.83 -> sure shopping or something
similar. And using your
735.09 -> knowledge that you already have.
Just grab the data from
738.63 -> WordPress, mix it together. And
yeah, you will have a working
742.77 -> website without the need of
learning new technology, which
746.91 -> is sometimes especially right
now very important, because we
750.54 -> don't have too much time and
learning new tech takes time. So
755.22 -> Fran: yes, 100% agreed Maciek,
it does take a lot of time. And
760.62 -> that's the thing too, from a
financial perspective, the
765.96 -> decision going headless, you
have to account for money,
769.47 -> because to your to your point
Maciek, you're going to have to
772.53 -> hire if you don't know React
code, say you're not even savvy
776.43 -> too much on JavaScript, you're
gonna have to hire a React
780.24 -> JavaScript, you're gonna have to
hire a developer team. And that
784.62 -> costs money unless you want to
learn it. yourself. Now, again,
789.51 -> there's trade offs to
everything. That's why like, I
792.96 -> truly believe that like, there's
always going to be the room for
798.33 -> both monolithic and headless
WordPress. And I think that's
802.71 -> the beauty that you have a
choice in web web development,
807.3 -> especially, especially with
WordPress.
810.54 -> Maciek: Hmm, yeah, that's,
that's true. So I think it's
814.62 -> time to show how to convert how
to decouple WordPress, right?
821.04 -> Fran: Yeah, let's let's let's do
it Maciek. Let's talk through
823.41 -> this. So the first thing that
you have to consider when you're
828.48 -> decoupling WordPress is the API
layer. Right? Because as
833.46 -> mentioned, when you go the jam
stack approach, the decoupled
837.81 -> approach API driven development,
whatever you want to call it,
841.47 -> it's just that it's your API
endpoint that you're going to
844.29 -> consume this data off of. And
luckily, there's two ways you
848.37 -> can do this in WordPress. I'm
going to use Graph QL. And don't
852.9 -> worry, everybody in the webinar
throughout this whole webinar
858.36 -> that I'm going through, in our
developers.wp engine.com
863.19 -> website, we have a whole
tutorial that I'm playing off
867.18 -> of, as well as rest vs Graph QL,
and all these things. So if you
871.02 -> want to dive deeper in and
whatnot, I'll work with Maciek
874.98 -> to like, send some links, a post
webinar. But anyway, there's
880.35 -> two, two ways you can do this.
You can use the REST API, which
885.66 -> some of you that are already
developing in traditional
888.48 -> WordPress already used because
it ships with WordPress Core.
892.92 -> And with the REST API, you use
the HTTP verbs or I like to call
898.47 -> them verbs but methods like get
create, read, update, delete, to
902.22 -> grab up that data off it. And
then I always pull up this
907.17 -> handbook here, from
wordpress.org to so I have a
910.92 -> reference of like all the
endpoints that I have, so that I
915.18 -> can grab that data from posts,
categories, tags, etc. Now,
921.81 -> here's the difference. And
there's trade offs which we
924.3 -> won't go deep dive because this
is not a rest versus Graph QL.
927.63 -> webinar, but major difference
between will actually the two
933.57 -> major difference I want to
address Maciek is the way you
936.6 -> grab data. So first of all, as I
mentioned, HTTP method, REST
940.56 -> API, Graph QL queries and
mutations. Because what Graph QL
946.02 -> is, at its core is a query
language. And when you have a
950.88 -> Graph QL layer on whatever
database data data schema you're
955.98 -> using, it exposes it as a Graph
QL schema this way? How it's,
963.06 -> it's interesting, because one of
the benefits of it is that Graph
965.91 -> QL itself kind of makes each API
is kind of different, right?
973.05 -> Whether you're using a Shopify
API, WordPress Core REST API,
977.49 -> whatever back end, you might be,
it's hard as a developer,
980.13 -> because you're like, Oh, great.
Now I have to like expose that
982.77 -> schema and see how the API
endpoints are all laid out and
987.96 -> everything with Graph QL it
uniforms it and turns everything
992.43 -> into a Graph QL schema. And
most, I'll show you real quick
998.22 -> most Graph QL instals. Now,
within a database, have this
1003.98 -> graphical IDE editor, where you
have a query composer, and this
1009.41 -> is that my WordPress schema
here, exposed in a Graph QL
1013.4 -> scheme. So that's the beauty of
Graph QL, it kind of unsniff
1018.29 -> flakes, each API because of API
to me, it's like a snowflake
1021.59 -> Maciek, it's all different,
unique, and Graph QL, we'll
1024.47 -> just, um, snowflake that and now
you were like kind of used to
1028.4 -> this graphical IDE. And then the
other thing I wanted to point
1031.97 -> out is that I showed you guys
can reference that in
1036.02 -> wordpress.org, all begin points
within WordPress that you have
1039.2 -> access to and rest. But in Graph
QL it is just one endpoint. And
1046.1 -> within that endpoint, in rest,
you're you you're at the mercy
1049.97 -> of what's in the API, you can't
declare exactly what you want.
1054.53 -> But as I mentioned here, in
Graph QL, in this case, again,
1059.72 -> queries and mutations I'm
clearing and asking my Graph QL
1065.75 -> server, which is on top of
WordPress, now, hey, Graph QL.
1070.88 -> From my WordPress data schema, I
want all the posts. And within
1075.77 -> those posts, those posts have
nodes. And those nodes have
1079.25 -> fields of title, your eye, I'm
pulling out the date the
1082.64 -> content, you can pull out other
things. But for the webinars
1086 -> sake, we're just going to run
this press play against the
1090.26 -> Graph QL server.
1092.63 -> And there you go. I asked
exactly for this data, and in
1097.73 -> this form, and it's giving it
back to me in this forum. JSON.
1102.38 -> So simple as that. That's how we
decouple WordPress with Graph
1108.41 -> QL. Now, skipping one thing in
the plugins, and I gotta give a
1113.12 -> shout out Maciek, I know you
know what, I'm gonna shout out
1116.06 -> to one of my favourite people in
the decoupled movement in
1121.13 -> WordPress. Mr. Jason
1123.65 -> Maciek: Fried Pizza. Yeah.
1126.74 -> Fran: In fact, I, I think I'm
doing an injustice to Jason.
1131.33 -> Because I should have had like
an Arab fried pizza for lunch
1137.84 -> here, but anyway, brought to you
by air fried pizza and Jason
1140.9 -> ball. So WP graph. QL is an open
source plugin created by Jason
1146.6 -> ball who works at WP Engine and
he's one of my favourite people
1150.53 -> in the world. But essentially,
all you do is download this
1154.13 -> plugin. And then it turns your
WordPress instal, like I said,
1158.39 -> into a graph, QL server, and
then all of a sudden, you have a
1161.42 -> Graph QL option in the menu on
the left side of WordPress Core.
1165.77 -> And then you have your graphical
ID. So that's it. That's all you
1169.4 -> have to do to decouple
WordPress. Now, Maciek, we're
1174.29 -> ready to talk about the front
end. In this case, we're going
1179.03 -> to use next JS for our front end
framework. And then for some
1185.96 -> helper functionality to grab the
data. On the front end, we're
1191.45 -> going to use the Apollo client
and its Graph QL library. So
1195.17 -> let's jump over to my Visual
Studio code here. And I have
1201.77 -> some things already pre made for
the sake of time here. And
1205.28 -> again, don't worry, we have an
step by step tutorial on the
1209.27 -> website that I'll link over
after the webinar is over. So
1215.36 -> the first thing I want to talk
about is next Js itself, right?
1221.33 -> Because it last time I checked,
which was like last week, and I
1229.34 -> always see Lee Robinson, who's
the head of Developer Relations
1233.09 -> at verse cell. Next, Js is made
by Marcel. And it's the most,
1238.43 -> it's the most popular, most used
downloaded framework in this
1244.61 -> ecosystem, and there's a reason
for that. And for me, developers
1249.44 -> are full of opinions, and
whether you agree with them or
1252.74 -> not, that's just how they are
Maciek, you know that you've
1255.59 -> been doing this longer. You know
what I'm saying, Man, so I'm
1260.18 -> going to share with you guys my
opinion about why I think next
1263.15 -> Js is so popular. So the first
thing is the developer
1266.72 -> experience and the productivity
of the developer flow that
1270.8 -> allows you to get into off the
bat as a developer, okay?
1276.32 -> Because the first thing is how
its structured, boilerplate
1281.33 -> wise, when you spin up and pull,
it's an NPM package out of the
1284.75 -> box. So if I jump into my
terminal right now, which I did
1290.87 -> before this, and then put the
utility command in there, MPX
1295.31 -> create next app, it's going to
pull out all the dependencies
1298.58 -> and the packages and all that.
And this is basically a next Js
1304.76 -> boilerplate, I've made some
things, but I'm gonna go over
1307.25 -> what it spins up out of the box
here. The first thing I think is
1310.85 -> super sweet is the page based
routing that it does out of the
1314.45 -> box, because as a web developer,
especially on websites, next to
1318.98 -> us is actually built
architecturally already for you
1323.63 -> for pages and routes. And as a
web developer, you're like, oh,
1327.65 -> great, sweet, my friend. And my
framework that I'm using already
1331.97 -> does that for me. So in this
pages directory, all you do is
1336.56 -> in this case, the index.js comes
out of the box with it, that's
1339.92 -> the homepage, when you hit the
just the forward slash, that's,
1345.05 -> that's the default homepage
route. But I can drop in this
1348.53 -> page, and it relates to the
file. The route is related to
1354.5 -> the file name. So if I put like
about.js Maciek in this, and
1359 -> then render a, create a React
component, a default component
1363.05 -> and render some JSX, like, Hello
World, or whatever. If I go to
1367.88 -> my browser, and then visit that
path, slash about, it's right
1374.69 -> there for you. So that's one of
the things that I think is
1377.57 -> pretty sweet. And then
obviously, this is built on top
1380.15 -> of React. So it uses React
component style library. And
1386.33 -> then the second thing that I
think is super sweet about next,
1390.47 -> Js, and I'll get down into this,
once I start talking about the
1394.58 -> files and how to create a
headless WordPress site here
1399.41 -> is the rendering patterns that
next Js allows you to have as a
1404.45 -> developer at a page level,
right? Because I mentioned at
1409.19 -> the beginning that like going
decoupled and JAMstack in your
1412.1 -> site is the approach of hey, as
much as we can, at build time,
1419.57 -> before user even is available to
request and hit that URL at