I want to execute multiple queries nodes at the same time in gatsby-node.js in order to create pages accordingly to my query , so these are the queries :
{
allPrismicLastPosts {
nodes {
data {
blogs {
blog {
document {
... on PrismicBlog {
uid
}
... on PrismicCulture {
uid
}
}
}
}
}
}
}
}
allPrismicMainBlogs {
nodes {
data {
blogs {
blog {
document {
... on PrismicBlogMain {
uid
}
}
}
}
}
}
}
}
So in order to createPage in gatsby-node.js I need both the uid of allPrismicLastPosts and allPrismicMainBlogs from these queries , this is what I 've tried :
{
allPrismicLastPosts {
nodes {
data {
blogs {
blog {
document {
... on PrismicBlog {
uid
}
... on PrismicCulture {
uid
}
}
}
}
}
}
}
}
allPrismicMainBlogs {
nodes {
data {
blogs {
blog {
document {
... on PrismicBlogMain {
uid
}
}
}
}
}
}
}
}
But this will only fetch the allPrismicLastPosts uid instead I want all the uid ,Any idea to solve this ?
You have many ways of doing this. The easiest one is to assign each result of the query to a variable like:
const posts = await graphql(`
query {
allPrismicLastPosts {
nodes {
data {
blogs {
blog {
document {
... on PrismicBlog {
uid
}
... on PrismicCulture {
uid
}
}
}
}
}
}
}
}
`);
const mainBlog = await graphql(`
query {
allPrismicMainBlogs {
nodes {
data {
blogs {
blog {
document {
... on PrismicBlogMain {
uid
}
}
}
}
}
}
}
}
`);
Then you just need to loop through each variable:
posts.data.allPrismicLastPosts.nodes.forEach(node => {
createPage({
// your data here
}),
});
mainBlog.data.allPrismicMainBlogs.nodes.forEach(node => {
createPage({
// your data here
}),
});