首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >GraphQL查询中使用GatsbyJS确定文件夹结构的作用域

GraphQL查询中使用GatsbyJS确定文件夹结构的作用域
EN

Stack Overflow用户
提问于 2019-03-04 10:25:45
回答 1查看 1.8K关注 0票数 2

我确实有分类,作品和图片。它们都是层次分明的,典型的亲子关系。文件夹结构已经表示了这个层次结构。最后,我将更详细地解释我的主要问题。

文件夹结构:

代码语言:javascript
复制
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

这是一个简单的投资组合层次结构。work是根文件夹,有不同的类别,例如drawing__。在里面你会发现文件夹,它们代表一个特定的部分。每个片段都有一个包含详细信息的index.md和多个图像(jpeg、png等)。

gatsby-config.js:

代码语言:javascript
复制
// ...
{
  resolve: 'gatsby-source-filesystem',
  options: {
    name: 'work',
    path: `${__dirname}/work/`,
  },
},
// ...

为了解析这些文件,我使用gatsby-source-filesystem插件。因此,我可以通过sourceInstanceName: { eq: "work" }__查询该文件夹。

gatsby-node.js:

代码语言:javascript
复制
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,  
        })
      }
    }
  }
}

此代码帮助我为以后的目的标记类别,例如在概述页面中显示类别列表。

示例查询:

代码语言:javascript
复制
{
  allDirectory(
    filter: {
      sourceInstanceName: { eq: "work" }
      relativeDirectory: { eq: "" }
    }
  ) {
    edges {
      node {
        dir
        name
        extension
        relativeDirectory
        relativePath
      }
    }
  }
}

查询所有类别。

代码语言:javascript
复制
{
  allDirectory(
    filter: {
      sourceInstanceName: { eq: "work" }
      relativeDirectory: { eq: "drawing" }
    }
  ) {
    edges {
      node {
        dir
        name
        extension
        relativeDirectory
        relativePath
      }
    }
  }
}

查询类别drawing__的所有片段。

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

在类别drawing-1中查询drawing__中的所有图片。

问题:

在最好的情况下,我想遍历每个类别,并显示来自index.md的图片和描述。但是我如何单独提取类别来查询这些片段呢?我应该如何与盖茨比一起映射这些实体?我的概念错了吗?如果你有什么好的建议,我应该怎么想才能实现我的目标,我会很高兴的。

编辑:

现在,我正在摆弄sourceNodes(),并从文件夹结构创建抽象节点。所需的JSON可能如下所示:

代码语言:javascript
复制
{
  "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"
            }
          }
        }
      ]
    }
  }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-03-04 18:08:04

可以使用 method在盖茨比节点之间创建父/子关系,以便找到可以使用 undocumented method的父节点。

代码语言:javascript
复制
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
                })
            }
        }
    }
}

相应的查询如下所示:

代码语言:javascript
复制
    {
      allDirectory(
        filter: {
          sourceInstanceName: { eq: "work" }
            relativeDirectory: { eq: "" }
        }
      ) {
        edges {
          node {
            name
            relativePath
            children {
              __typename ... on Directory {
                name
                relativePath
              }
            }
          }
        }
      }
    }

输出结果如下:

代码语言:javascript
复制
    {
      "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"
                  }
                ]
              }
            }
          ]
        }
      }
    }

作为解释,__typename ... on Directory为您提供了查询整个相应节点的机会。否则,您将只获得子节点的ID。为更好地理解访问:https://graphql.org/learn/schema/#union-types

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54981238

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档