Skip to content

Instantly share code, notes, and snippets.

@sidharthachatterjee
Created April 1, 2019 10:40
Show Gist options
  • Save sidharthachatterjee/a3f21834a67185a370427e37ef4d245f to your computer and use it in GitHub Desktop.
Save sidharthachatterjee/a3f21834a67185a370427e37ef4d245f to your computer and use it in GitHub Desktop.
Dynamic GraphQL queries with String interpolation
exports.createPages = ({ graphql, actions }) => {
return graphql(`
{
site {
siteMetadata {
githubOrgs
}
}
}
`).then(result => {
const { githubOrgs } = result.data.site.siteMetadata
const query = `
{
githubOrgs: github{
${githubOrgs
.map(
org => `
${org}: organization(login:"${org}"){
...orgFragments
}
`
)
.join(``)}
}
}
fragment orgFragments on GitHub_Organization{
id
url
}
`
console.log(query)
return graphql(query).then(result => {
console.log(result.data.githubOrgs)
// createPage etc
})
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment