盖茨比公司;Graphql:筛选与变量匹配的页面的allMarkdownRemark

盖茨比公司;Graphql:筛选与变量匹配的页面的allMarkdownRemark,graphql,markdown,gatsby,Graphql,Markdown,Gatsby,我正在尝试筛选我的所有降价页面,以查找与某个类别(在“降价frontmatter中指定)匹配的页面以及当前未访问的页面。尽管所有通过markdown创建的页面都会收到与allMarkdownRemark相同的结果,并且不会过滤任何结果 我想知道如何使所有页面对allMarkdownRemark的结果不相同,并希望对allMarkdownRemark中的结果进行筛选 我的页面查询看起来像 export const pageQuery = graphql` query Art

我正在尝试筛选我的所有降价页面,以查找与某个
类别
(在“降价
frontmatter
中指定)匹配的页面以及当前未访问的页面。尽管所有通过markdown创建的页面都会收到与
allMarkdownRemark
相同的结果,并且不会过滤任何结果


我想知道如何使所有页面对
allMarkdownRemark
的结果不相同,并希望对
allMarkdownRemark
中的结果进行筛选

我的页面查询看起来像

    export const pageQuery = graphql`
        query ArticleQuery($path: String, $category: String, $title: String) {
            allMarkdownRemark(
                filter: {
                    frontmatter: {
                        category: {eq: $category},
                        title: {ne: $title}
                    }
                },
                sort: {
                    order: DESC, fields: [frontmatter___date] 
                }
            ) {
            ...
我的
gatsby node.js
看起来像

const { createFilePath } = require(`gatsby-source-filesystem`);
const { fmImagesToRelative } = require('gatsby-remark-relative-images-v2');
const path = require("path");

exports.createPages = async ({ actions: { createPage }, graphql }) => {
    const postTemplate = path.resolve(`src/components/ArticlePage.jsx`);

    const result = await graphql(`
    {
      allMarkdownRemark(
        sort: { order: DESC, fields: [frontmatter___title] }
      ) {
        edges {
          node {
            fields {
              slug
            }
            frontmatter {
              category
              title
            }
          }
        }
      }
    }
  `);

    if (result.errors) {
        return Promise.reject(result.errors);
    };

    result.data.allMarkdownRemark.edges.forEach(({ node }) => {
        createPage({
            path: `${node.fields.slug}`,
            category: `${node.frontmatter.category}`,
            title: `${node.frontmatter.title}`,
            component: postTemplate,
            context: {node}
        });
    });
};

exports.onCreateNode = ({ node, getNode, actions }) => {
  fmImagesToRelative(node);
  if (node.internal.type === `MarkdownRemark`){
    actions.createNodeField({
        node,
        name: `slug`,
        value: createFilePath({ node, getNode, basePath: `pages`, trailingSlash: true }),
    });
  }
};
所有降价文件中都包含类似的内容

---
title: "My title"
category: "My category"
---
我想知道如何使所有的页面不具有相同的内容
allmarkdownmark
的结果,并希望在 要筛选的所有Markdown备注


在这些情况下,通常使用的是一个
字段,用于您要分组的所有类型的标记文件。正如您所说,
allmarkdownmark
是盖茨比(及其变形金刚和锐器)在您允许它访问您的文件系统时推断出的模式,因此您无法直接区分标记的类型。这是最简单、最干净、侵入性更小的选择。你只需要:

---
title: "My title"
category: "My category"
key: "pageTypeOne"
---
然后,在您的查询中,您只需要在需要时过滤
key
字段:

export const pageQuery = graphql`
    query ArticleQuery($path: String, $category: String, $title: String) {
        allMarkdownRemark(
            filter: {
                frontmatter: {
                    category: {eq: $category},
                    title: {ne: $title}
                    key: {eq: "pageTypeOne" }
                }
            },
            sort: {
                order: DESC, fields: [frontmatter___date] 
            }
        ) {
        ...
如果需要,您可以在
gatsby node.js
中的
createPage
API中将基于字符串的方法更改为上下文方法。或者,根据您的需要,在您的
gatsby node.js
中创建一个过滤查询,为每个页面创建不同的查询,这样您的
markdownmark
就已经被过滤了

或者,您可以添加不同的文件系统()并使用推断的
sourceInstanceName
获取数据:

{
  resolve: `gatsby-source-filesystem`,
  options: {
    name: `pages`,
    path: `${__dirname}/src/pages/`,
  },
},
{
  resolve: `gatsby-source-filesystem`,
  options: {
    name: `posts`,
    path: `${__dirname}/src/posts/`,
  },
},
然后:


问题/问题是什么?我更新了我的问题我不是在用类别来做这件事吗?我不明白
键的意思。我正在创建的页面是从模板创建的,但我想根据该页面的类别筛选不同的降价备注。想法是相同的,但这不是
category
的目的,想法是将不同类型的页面分组,并使用适当的
filter
获得它们。如果您添加一个
(可以随意命名),您将能够根据备注获得您的页面类型。或者,您可以使用
allFile
并使用
sourceInstanceName
来获取您的类型,但想法是相同的(答案已更新)。>如果需要,您可以在gatsby node.js中的createPage API中将基于字符串的方法更改为基于上下文的方法,但这会导致在每页上过滤相同的
标记备注。你能说明你的意思吗?(我把它解释为意义“为了使用变量作为
键:
而不是常量字符串
\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu(页面、具有类型的帖子等)您可以在
gatsby node.js
中创建不同的查询,并通过该
字段对其进行过滤,然后基于该唯一查询创建页面
{
  allFile(filter: { sourceInstanceName: { eq: "posts" } }) {
    edges {
      node {
        extension
        dir
        modifiedTime
      }
    }
  }
}