Created
March 17, 2020 11:05
-
-
Save pierreburgy/89622fce6990aa31840e6dac1aa3f67f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Implement Gatsby's Node APIs in this file. | |
* | |
* See: https://www.gatsbyjs.org/docs/node-apis/ | |
*/ | |
// You can delete this file if you're not using it | |
const path = require(`path`); | |
const makeRequest = (graphql, request) => new Promise((resolve, reject) => { | |
// Query for nodes to use in creating pages. | |
resolve( | |
graphql(request).then(result => { | |
if (result.errors) { | |
reject(result.errors) | |
} | |
return result; | |
}) | |
) | |
}); | |
// Implement the Gatsby API “createPages”. This is called once the | |
// data layer is bootstrapped to let plugins create pages from data. | |
exports.createPages = ({ actions, graphql }) => { | |
const { createPage } = actions; | |
const getArticles = makeRequest(graphql, ` | |
{ | |
allStrapiArticle { | |
edges { | |
node { | |
slug | |
} | |
} | |
} | |
} | |
`).then(result => { | |
// Create pages for each article. | |
result.data.allStrapiArticle.edges.forEach(({ node }) => { | |
createPage({ | |
path: `/${node.slug}`, | |
component: path.resolve(`src/templates/article.js`), | |
context: { | |
slug: node.slug, | |
id: node.id | |
}, | |
}) | |
}) | |
}); | |
// Query for articles nodes to use in creating pages. | |
return getArticles; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment