Skip to content

Instantly share code, notes, and snippets.

@jonlow
Created November 27, 2019 22:42
Show Gist options
  • Save jonlow/90a9ff1a873e5da48294d977f04f3b08 to your computer and use it in GitHub Desktop.
Save jonlow/90a9ff1a873e5da48294d977f04f3b08 to your computer and use it in GitHub Desktop.
Create Posts used on IG gatsby build
const path = require(`path`)
const {
postData,
} = require('../src/fragments/data.js');
require("dotenv").config({
path: `.env.${process.env.NODE_ENV}`,
})
module.exports = async ({ actions, graphql }) => {
const GET_POSTS = `
query GET_POSTS($first:Int $after:String){
wpgraphql {
posts(
first: $first
after:$after
) {
pageInfo {
endCursor
hasNextPage
}
nodes {
${postData}
}
}
}
}
`
const { createPage } = actions
const allPosts = []
let pageNumber = 0
const fetchPosts = async variables =>
await graphql(GET_POSTS, variables).then(({ data }) => {
const {
wpgraphql: {
posts: {
nodes,
pageInfo: { hasNextPage, endCursor },
},
},
} = data
if(process.env.GATSBY_LIMIT_WORDPRESS_DATA) {
console.log('cya');
if(pageNumber == process.env.GATSBY_LIMIT_WORDPRESS_DATA) return allPosts
}
nodes.map(post => {
allPosts.push(post)
})
if (hasNextPage) {
pageNumber++
return fetchPosts({ first: 100, after: endCursor })
}
return allPosts
})
await fetchPosts({ first: 100, after: null }).then(allPosts => {
const postTemplate = path.resolve(`./src/templates/post.js`)
allPosts.map(post => {
createPage({
path: `/${post.slug}/`,
component: postTemplate,
context: post,
})
})
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment