Created
April 1, 2019 10:40
-
-
Save sidharthachatterjee/a3f21834a67185a370427e37ef4d245f to your computer and use it in GitHub Desktop.
Dynamic GraphQL queries with String interpolation
This file contains hidden or 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
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