Is this a good approach for creating pages for multiple content typs with GatsbyJS? I'm trying to create pages for multiple content types in Gatsby and I was wondering if this is the right approach:

My gatsby-node.js file:

const path = require(`path`)

const { createFilePath } = require("gatsby-source-filesystem")
exports.onCreateNode = ({ node, actions, getNode }) =&gt; {
  const { createNodeField } = actions
  // Is this OK?
  if (
    node.internal.type === "Mdx" &amp;&amp;
    node.fileAbsolutePath.includes(`content/posts`)
  ) {
    const value = createFilePath({ node, getNode })
    createNodeField({
      name: "slug",
      node,
      value: `/blog${value}`,
    })
  }
  if (
    node.internal.type === "Mdx" &amp;&amp;
    node.fileAbsolutePath.includes(`content/projects`)
  ) {
    const value = createFilePath({ node, getNode })
    createNodeField({
      name: "slug",
      node,
      value: `/project${value}`,
    })
  }
}
exports.createPages = async ({ graphql, actions, reporter }) =&gt; {
  const { createPage } = actions
  const blogPostTemplate = path.resolve(`./src/templates/template-blog-post.js`)
  const projectEntryTemplate = path.resolve(
    `./src/templates/template-project-entry.js`
  )
  const result = await graphql(`
    query {
      posts: allMdx(
        sort: { order: DESC, fields: [frontmatter___date, fields___slug] }
        limit: 10000
        filter: { fileAbsolutePath: { regex: "/content/posts/" } }
      ) {
        edges {
          node {
            id
            fields {
              slug
            }
            frontmatter {
              title
            }
          }
        }
      }
      projects: allMdx(
        sort: { order: DESC, fields: [frontmatter___date, fields___slug] }
        limit: 1000
        filter: { fileAbsolutePath: { regex: "/content/projects/" } }
      ) {
        edges {
          node {
            id
            fields {
              slug
            }
            frontmatter {
              title
            }
          }
        }
      }
    }
  `)
  if (result.errors) {
    reporter.panicOnBuild(
      '  ERROR: Loading "createPages" query.'
    )
  }
  const posts = result.data.posts.edges
  const projects = result.data.projects.edges
  posts.forEach(({ node }) =&gt; {
    createPage({
      path: node.fields.slug,
      component: blogPostTemplate,
      context: { id: node.id },
    })
  })

  projects.forEach(({ node }) =&gt; {
    createPage({
      path: node.fields.slug,
      component: projectEntryTemplate,
      context: { id: node.id },
    })
  })
}



My (redacted) gatsby-config.js file:

module.exports = {
  plugins: [
    {
      resolve: `gatsby-source-filesystem`,
      options: {
        name: `posts`,
        path: `${__dirname}/content/posts/`,
      },
    },
    {
      resolve: "gatsby-source-filesystem",
      options: {
        name: `projects`,
        path: `${__dirname}/content/projects/`,
      },
    },
  ],
}



Although this works fine and I'm not getting any errors, I'm not sure using two if-statements in gatsby-node.js is the right approach to generating pages for multiple content types. I've searched online and I have not yet found a guide or 'best-practices' when it comes to handling multiple content types with GatsbyJS. Please share if you have!

Could you guys help me out? Am I on the right track or is there improvement to be found?

Cheers!
There's definitely a lot of duplicate code in those 2 ifs. I'd extract it into function and pass different things as parameters.

Then code would look let's say:

//not sure about name of function and parameters
function nodeFieldPerType(filePath, getNodePath) {
  if (
    node.internal.type === "Mdx" &amp;&amp;
    node.fileAbsolutePath.includes(filePath)
  ) {
    const value = createFilePath({ node, getNode })
    createNodeField({
      name: "slug",
      node,
      value: getNodePath(value),
    })
  }
}


Then you call it like like:

nodeFieldPerType(`content/posts`, value =&gt; `/blog${value}`);
nodeFieldPerType(`content/projects`, value =&gt; `/project${value}`);


This cleans it up a bit. If you keep adding more possible branches, you may need to add more parameters. You can go further to make this as a static configuration and put this into configuration structure, that you fill in advance. Then in your code you just pick the one you need and call it.
Ex: 

config = {
`content/posts`: { getNodePath:  value =&gt; `/blog${value}` }, //object on purpose so that you can add more parameters later
`content/projects`: { getNodePath: value =&gt; `/project${value}` },
}


And then instead of your ifs you check if filePath exists in this config and if it does, you call function with parameters in config.