【问题标题】:How can I limit the objects from a group in a query in Gatsby?如何在 Gatsby 的查询中限制组中的对象?
【发布时间】:2020-09-12 01:06:18
【问题描述】:

我的代码中有这个查询,它允许我为此博客首页构建标签云

tagCloud:allContentfulBlogPost {
  group(field: tags, limit: 8) {
     fieldValue
  }
}

它正在传递我使用{data.tagCloud.group.map(tag => (...))}; 在我的组件中映射的数据。该代码运行良好,但不受我在查询中group(fields: tags, limit: 8) 上面传递的过滤器的限制。它呈现所有标签,而不仅仅是前八个。

为了看看它是否有效,我也尝试过跳过过滤器,但没有成功。 这是在 Gatsby 中限制我的映射组件计数的正确方法吗?

【问题讨论】:

    标签: javascript reactjs graphql gatsby


    【解决方案1】:

    不幸的是,Contentful 源插件没有在它创建的任何节点上定义参数。相反,您需要自己创建这些。最简单的方法是通过createResolvers API

    这是我的一个项目中的一个类似示例:

    // in gatsby-node.js
    exports.createResolvers = ({ createResolvers }) => {
      createResolvers({
        SourceArticleCollection: {
          // Add articles from the selected section(s)
          articles: {
            type: ["SourceArticle"],
            args: {
              // here's where the `limit` argument is added
              limit: {
                type: "Int",
              },
            },
            resolve: async (source, args, context, info) => {
              // this function just needs to return the data for the field;
              // in this case, I'm able to fetch a list of the top-level
              // entries that match a particular condition, but in your case
              // you might want to instead use the existing data in your
              // `source` and just slice it in JS.
              const articles = await context.nodeModel.runQuery({
                query: {
                  filter: {
                    category: {
                      section: {
                        id: {
                          in: source.sections.map((s) => s._ref),
                        },
                      },
                    },
                  },
                },
                type: "SourceArticle",
              })
    
              return (articles || []).slice(0, args.limit || source.limit || 20)
            },
          },
        },
      })
    }
    

    由于解析器作为支持 GraphQL API 的数据获取例程的一部分运行,因此它将在构建时在服务器端运行,并且只有截断/准备好的数据会在请求时发送到客户端。

    【讨论】:

    • 感谢您的回答。我没有那么有经验,所以为了确保我没有误解,这将使用 createResolvers API 来定义这些参数。那么,在types: ['SourceArticle'] 中,我将定义我的节点contentfulBlogPost?我不确定我是否理解这将如何只影响这样的特定查询。关于最后一个函数resolve: async() => { },会限制所有帖子吗?我在项目的其他地方查询过使用“整体”查询 { allContentfulBlogPost { group(fields: tags) 进行渲染。
    • type 字段定义了预期作为响应的 GraphQL 类型。在上面的示例中,它是一个 SourceArticle 类型的数组。如果你想扩展ContentfulBlogPost,你可以用它来代替SourceArticleCollection(传递给createResolvers的对象的键)。 resolve 参数是执行检索与字段关联的记录工作的函数。数量限制发生在最后一行(释义:return [].slice(0, args.limit))。
    • 也许我弄错了。做了一些更改并在构建时得到了这个回报:“7:26:28 PM:警告createResolvers传递了架构中不存在的allContentfulBlogPost类型的解析器。在添加解析器之前使用createTypes添加类型。”但没关系,我已经解决了设置高度的问题,并用 css 隐藏了所有溢出。谢谢!
    • allContentfulBlogPost 是根 Query 类型上的字段名称。您需要指定 type,在本例中为 ContentfulBlogPost,如上所述。
    猜你喜欢
    • 1970-01-01
    • 2019-07-29
    • 1970-01-01
    • 2019-12-21
    • 1970-01-01
    • 2021-09-06
    • 2018-04-14
    • 2018-11-15
    • 1970-01-01
    相关资源
    最近更新 更多