Q:

Scoping folder structure in GraphQL Queries with GatsbyJS

I do have categories, pieces and pictures. They're all in cascading order; Typical child-parent-relationship. And the folder structure already represents this hierarchy. At the end I will explain my main problem in more detail.
Folder structure:
work
├── drawing
│   ├── drawing-1
│   │   ├── image.1.jpg
│   │   ├── image.2.jpg
│   │   ├── image.3.jpg
│   │   ├── image.jpg
│   │   └── index.md
│   └── index.md
├── sculpture
│   ├── gaehnschreier
│   │   ├── image.1.JPG
│   │   ├── image.2.jpg
│   │   ├── image.3.JPEG
│   │   ├── image.4.png
│   │   ├── image.PNG
│   │   └── index.md
│   └── index.md
└── watercolor
    ├── index.md
    ├── portrait-1
    │   ├── image.jpg
    │   └── index.md
    └── portrait-2
        ├── image.jpg
        └── index.md

This is a simple hierarchy of a portfolio. work is the root folder and has different categories e.g. drawing. Inside you'll find folders, which represent a specific piece. Each piece has a index.md with detailed information about that piece and multiple images (jpeg, png etc.). 

gatsby-config.js:
// ...
{
  resolve: 'gatsby-source-filesystem',
  options: {
    name: 'work',
    path: `${__dirname}/work/`,
  },
},
// ...

For resolving the files, I use the gatsby-source-filesystem plugin. So, I can query that folder by sourceInstanceName: { eq: "work" }.

gatsby-node.js:
exports.onCreateNode = ({ node, getNode, actions }) => {

  const { createNodeField } = actions

  if (node.internal.type === `Directory`) {

    if (node.sourceInstanceName === `work`) {

      if (!node.relativeDirectory) {
        createNodeField({
          node,   
          name: `workCategory`,
          value: true,  
        })
      }
    }
  }
}

This code helps me to flag the categories for later purpose, e.g. displaying a list of categories on a overview page.

Example Queries:
{
  allDirectory(
    filter: {
      sourceInstanceName: { eq: "work" }
      relativeDirectory: { eq: "" }
    }
  ) {
    edges {
      node {
        dir
        name
        extension
        relativeDirectory
        relativePath
      }
    }
  }
}

Querying all categories.

{
  allDirectory(
    filter: {
      sourceInstanceName: { eq: "work" }
      relativeDirectory: { eq: "drawing" }
    }
  ) {
    edges {
      node {
        dir
        name
        extension
        relativeDirectory
        relativePath
      }
    }
  }
}

Querying all pieces of the category drawing. 

{
  allFile(
    filter: {
      sourceInstanceName: { eq: "work" }
      extension: { in: ["jpg", "jpeg", "png"] }
        relativeDirectory: { eq: "drawing/drawing-1" }
    }
  ) {
    edges {
      node {
        dir
        name
        extension
        relativeDirectory
        relativePath
      }
    }
  }
}

Querying all pictures of the piece drawing-1 inside category drawing.

The problem:
In the best case I'd like to iterate through each category and display the work pieces with its pictures and descriptions from the index.md. But how can I extract the categories seprately to query for the pieces? How should I map these entities together with Gatsby? Is my concept missleading? If you're having any good suggestion, what I should think of to achieve my goal, I'll be very happy with it.
EDIT:
Right now I am fiddling around with sourceNodes() and creating abstract nodes from the folder structure. The desired JSON could look like this:
{
  "data": {
    "allWorkCategory": {
      "edges": [
        {
          "node": {
            "path": "work/scuplture",
            "children": [
              {
                "node": {
                  "internal": {
                    "type": "WorkItem",
                    "name": "Drawing 1",
                    "pictures": {
                       // ...
                    }
                  }
                }
              }
            ],
            "internal": {
              "type": "WorkCategory"
            }
          }
        },
        {
          "node": {
            "path": "work/drawing",
            "children": [],
            "internal": {
              "type": "WorkCategory"
            }
          }
        },
        {
          "node": {
            "path": "work/watercolor",
            "children": [],
            "internal": {
              "type": "WorkCategory"
            }
          }
        }
      ]
    }
  }
}

A:

You can create parent / child relationship between gatsby node using the createParentChildLink method, in order to find the parent node you can use the getNodesByType undocumented method. 
const path = require('path')
exports.onCreateNode = ({
    node,
    getNodesByType,
    actions
}) => {
    const {
        createParentChildLink
    } = actions

    if (node.internal.type === 'Directory') {
        if (node.sourceInstanceName === 'work') {
            // in some case the trailing slash is missing.
            // Always add it and normalize the path to remove duplication
            const parentDirectory = path.normalize(node.dir + '/')
            const parent = getNodesByType('Directory').find(
                n => path.normalize(n.absolutePath + '/') === parentDirectory
            )
            if (parent) {
                node.parent = parent.id
                createParentChildLink({
                    child: node,
                    parent: parent
                })
            }
        }
    }
}

The corresponding query could look like this:
    {
      allDirectory(
        filter: {
          sourceInstanceName: { eq: "work" }
            relativeDirectory: { eq: "" }
        }
      ) {
        edges {
          node {
            name
            relativePath
            children {
              __typename ... on Directory {
                name
                relativePath
              }
            }
          }
        }
      }
    }

And the output would look like:
    {
      "data": {
        "allDirectory": {
          "edges": [
            {
              "node": {
                "name": "drawing",
                "relativePath": "drawing",
                "children": [
                  {
                    "__typename": "Directory",
                    "name": "drawing-1",
                    "relativePath": "drawing/drawing-1"
                  }
                ]
              }
            },
            {
              "node": {
                "name": "sculpture",
                "relativePath": "sculpture",
                "children": [
                  {
                    "__typename": "Directory",
                    "name": "gaehnschreier",
                    "relativePath": "sculpture/gaehnschreier"
                  }
                ]
              }
            },
            {
              "node": {
                "name": "watercolor",
                "relativePath": "watercolor",
                "children": [
                  {
                    "__typename": "Directory",
                    "name": "portrait-1",
                    "relativePath": "watercolor/portrait-1"
                  },
                  {
                    "__typename": "Directory",
                    "name": "portrait-2",
                    "relativePath": "watercolor/portrait-2"
                  }
                ]
              }
            }
          ]
        }
      }
    }

For explanation, __typename ... on Directory gives you the opportunity to query the corresponding node as a whole. Otherwise you'll get only the ID of the child node. For better understanding visit: https://graphql.org/learn/schema/#union-types

