From ab70c2764b66fae08f0df4ae305b665693498ded Mon Sep 17 00:00:00 2001 From: Vishnu KS Date: Mon, 7 Jun 2021 00:10:21 +0530 Subject: [PATCH] Use promise in gatsby-node. --- app/gatsby-node.js | 83 +++++++++++++++++++++++----------------------- 1 file changed, 41 insertions(+), 42 deletions(-) diff --git a/app/gatsby-node.js b/app/gatsby-node.js index 56b730b..e9bf00c 100644 --- a/app/gatsby-node.js +++ b/app/gatsby-node.js @@ -1,50 +1,49 @@ -const fs = require("fs") -const categories = JSON.parse(fs.readFileSync("src/data/categories.json")) -var slugify = require('slugify') +const fs = require('fs'); + +const categories = JSON.parse(fs.readFileSync('src/data/categories.json')); +const slugify = require('slugify'); exports.createPages = async function ({ actions, graphql }) { - const { createPage } = actions - categories.forEach(async function(category) { - const data = await graphql(`query categoryBooksQuery($categoryName: String) { - allBooksJson( - filter: { - category: { - eq: $categoryName + const { createPage } = actions; + await Promise.all( + categories.map(async (category) => { + const data = await graphql( + ` + query categoryBooksQuery($categoryName: String) { + allBooksJson( + filter: { category: { eq: $categoryName } } + sort: { fields: [rating], order: DESC } + ) { + edges { + node { + id + title + url + rating + author + year + category + image_url + description + amazon_url } - } - sort: { - fields: [rating] - order: DESC - } - ){ - edges { - node { - id - title - url - rating - author - year - category - image_url - description - amazon_url + } } } - } - } - `, - {categoryName: category.name}) - console.log(category.name, data.data) - createPage({ + `, + { categoryName: category.name }, + ); + console.log(category.name, data.data); + createPage({ path: slugify(category.name), - component: require.resolve("./src/templates/categoryTemplate.js"), + component: require.resolve('./src/templates/categoryTemplate.js'), context: { - categoryName: category.name, - data: data.data, - image: category.emoji, - limit: null, + categoryName: category.name, + data: data.data, + image: category.emoji, + limit: null, }, - }) - }) -} + }); + }), + ); +};