conradlin

gatsby-node.js

May 4th, 2020
389
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // graphql function doesn't throw an error so we have to check to check for the result.errors to throw manually
  2. const path = require(`path`)
  3.  
  4. exports.createPages = async ({ graphql, actions }) => {
  5.   const { createPage } = actions
  6.  
  7.   const blogPost = await graphql(`
  8.   query {
  9.       allPosts(filter: {status: {eq: "published"}, content_type: {eq: "article"}}) {
  10.           nodes {
  11.             slug
  12.             url
  13.           }
  14.         }
  15.       }
  16.     `).then(result => {
  17.       if (result.errors) {
  18.         Promise.reject(result.errors);
  19.       }
  20.      
  21.       result.data.allPosts.nodes.forEach(({ slug, url }) => {
  22.         createPage({
  23.             path: `blog/posts/${url}`,
  24.             component: path.resolve(`./src/templates/blogPost.js`),
  25.             context: {
  26.                 // Data passed to context is available
  27.                 // in page queries as GraphQL variables.
  28.                 slug: slug,
  29.             },
  30.         });
  31.     });
  32.   });
  33.   const newsPost = await graphql(`
  34.   query {
  35.       allPosts(filter: {status: {eq: "published"}, content_type: {eq: "newsletter"}}) {
  36.           nodes {
  37.             slug
  38.             url
  39.           }
  40.         }
  41.       }
  42.     `).then(result => {
  43.       if (result.errors) {
  44.         Promise.reject(result.errors);
  45.       }
  46.      
  47.       result.data.allPosts.nodes.forEach(({ slug, url }) => {
  48.         createPage({
  49.             path: `subscribe/posts/${url}`,
  50.             component: path.resolve(`./src/templates/blogPost.js`),
  51.             context: {
  52.                 // Data passed to context is available
  53.                 // in page queries as GraphQL variables.
  54.                 slug: slug,
  55.             },
  56.         });
  57.     });
  58.   });
  59.  
  60.   return Promise.all([blogPost, newsPost]);
  61. };
Add Comment
Please, Sign In to add comment