Scoping folder structure in GraphQL Queries with GatsbyJS
我确实有分类、片段和图片。它们都是级联的;典型的亲子关系。并且文件夹结构已经代表了这个层次结构。最后,我将更详细地解释我的主要问题。
文件夹结构:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | 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 |
这是一个简单的投资组合层次结构。
gatsby-config.js:
1 2 3 4 5 6 7 8 9 | // ... { resolve: 'gatsby-source-filesystem', options: { name: 'work', path: `${__dirname}/work/`, }, }, // ... |
为了解析文件,我使用
查询该文件夹
gatsby-node.js:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | 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, }) } } } } |
此代码可帮助我标记类别以供以后使用,例如在概览页面上显示类别列表。
示例查询:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | { allDirectory( filter: { sourceInstanceName: { eq:"work" } relativeDirectory: { eq:"" } } ) { edges { node { dir name extension relativeDirectory relativePath } } } } |
查询所有类别。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | { allDirectory( filter: { sourceInstanceName: { eq:"work" } relativeDirectory: { eq:"drawing" } } ) { edges { node { dir name extension relativeDirectory relativePath } } } } |
查询类别
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | { allFile( filter: { sourceInstanceName: { eq:"work" } extension: { in: ["jpg","jpeg","png"] } relativeDirectory: { eq:"drawing/drawing-1" } } ) { edges { node { dir name extension relativeDirectory relativePath } } } } |
查询分类
问题:
在最好的情况下,我想遍历每个类别并显示带有
编辑:
现在我正在摆弄
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | { "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" } } } ] } } } |
你可以使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | 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 }) } } } } |
相应的查询可能如下所示:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | { allDirectory( filter: { sourceInstanceName: { eq:"work" } relativeDirectory: { eq:"" } } ) { edges { node { name relativePath children { __typename ... on Directory { name relativePath } } } } } } |
输出看起来像:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | { "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" } ] } } ] } } } |
为了说明,