WordCamp Phoenix 2019
Jason Bahl | @jasonbahl
WordPress as CMS, Static Gatsby Site to render the content
Dynamic Source Site
Resulting Static Site
{
viewer {
name
}
}
{
data: {
viewer: {
name: "Jason Bahl"
}
}
}
Post
Category
Category
Category
Post
title
"Hello World"
title
"GoodBye Mars"
Image
Image
Image
name
"news"
name
"crime"
name
"sports"
Image
query {
post(id: "cG9zdDox") {
title
link
categories {
nodes {
name
}
}
}
}
{
data: {
post: {
title: "Hello World!"
link: "http://site.com/hello-world"
categories: {
nodes: [
{
name: "sports"
},
{
name: "crime"
}
]
}
}
}
}
Post
Category
Category
Category
Post
title
"Hello World"
title
"GoodBye Mars"
Image
Image
Image
name
"news"
name
"crime"
name
"sports"
Image
const createPosts = require(`./gatsby/createPosts`)
const createPages = require(`./gatsby/createPages`)
const createUsers = require(`./gatsby/createUsers`)
const createCategories = require(`./gatsby/createCategories`)
const createTags = require(`./gatsby/createTags`)
exports.createPages = async ({ actions, graphql }) => {
await createPosts({ actions, graphql })
await createPages({ actions, graphql })
await createUsers({ actions, graphql })
await createCategories({ actions, graphql })
await createTags({ actions, graphql })
}
gatsby-node.js
gatsby/createPosts.js
graphql(
`
query GET_POSTS($first:Int $after:String){
wpgraphql {
posts(
first: $first
after:$after
) {
pageInfo {
endCursor
hasNextPage
}
nodes {
id
uri
postId
title
}
}
}
}
`).then(({data}) => { ... })
const postTemplate = `./src/templates/post.js`
allPosts.map(post => {
createPage({
path: `/blog/${post.uri}/`,
component: postTemplate,
context: post,
})
})
templates/post.js
export const pageQuery = graphql`
query GET_POST($id: ID!) {
wpgraphql {
post(id: $id) {
title
content
author {
name
slug
avatar {
url
}
}
categories {
nodes {
name
link
}
}
}
}
}
`