Skip to content

Instantly share code, notes, and snippets.

@delineas
Last active January 28, 2018 21:03
Show Gist options
  • Save delineas/8a566299ce5323c72c713e83b09ea73c to your computer and use it in GitHub Desktop.
Save delineas/8a566299ce5323c72c713e83b09ea73c to your computer and use it in GitHub Desktop.
gatsby-node.js strapi two sources
const path = require(`path`);
const makeRequest = (graphql, request) => new Promise((resolve, reject) => {
// Query for article 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 = ({ boundActionCreators, graphql }) => {
const { createPage } = boundActionCreators;
const getArticles = makeRequest(graphql, `
{
allStrapiArticle {
edges {
node {
id
}
}
}
}
`).then(result => {
// Create pages for each article.
result.data.allStrapiArticle.edges.forEach(({ node }) => {
createPage({
path: `/${node.id}`,
component: path.resolve(`src/templates/article.js`),
context: {
id: node.id,
},
})
})
});
const getAuthors = makeRequest(graphql, `
{
allStrapiUser {
edges {
node {
id
}
}
}
}
`).then(result => {
// Create pages for each user.
result.data.allStrapiUser.edges.forEach(({ node }) => {
createPage({
path: `/authors/${node.id}`,
component: path.resolve(`src/templates/user.js`),
context: {
id: node.id,
},
})
})
});
// Queries for articles and authors nodes to use in creating pages.
return Promise.all([
getArticles,
getAuthors,
])
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment