Last active
January 28, 2018 21:03
-
-
Save delineas/8a566299ce5323c72c713e83b09ea73c to your computer and use it in GitHub Desktop.
gatsby-node.js strapi two sources
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
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