【问题标题】:Attempting to sort graphQL order by position with DatoCMS adn Gatsby JS尝试使用 DatoCMS 和 Gatsby JS 按位置对 graphQL 进行排序
【发布时间】:2020-04-23 11:21:36
【问题描述】:

我正在使用这个 Gatsby JS 插件来探索 DatoCMS GraphQL API https://github.com/datocms/gatsby-source-datocms 在 dato 文档中它说您可以按 cms 中的位置排序 https://www.datocms.com/docs/content-delivery-api/ordering/

我基本上想保留cms中的顺序,而不是基于日期的顺序。

在 dato API Explorer 中,我可以像这样按位置排序帖子。

query MyQuery {
  allProjects(orderBy: position_ASC) {
    slug
  }
}

但是在 Gatsby 中,同样的查询暗示我我在下面尝试过类似的事情,但它给了我这个error Expected type SortOrderEnum, found position_ASC

query MyQuery {
  allDatoCmsProject(sort: {order: position_ASC}) {
    edges {
      node {
        slug
      }
    }
  }
}

在 gatsby 插件中,语法略有不同 DatoCMS graphQL 浏览器 如果我想按特定字段对其进行排序,这就是插件希望我进行排序的方式。但是我找不到任何允许我按 Dato cms 中元素的位置排序的字段

{
  allDatoCmsBlogPost(sort: { fields: [publicationDate], order: DESC }, limit: 5) {
    edges {
      node {
        title
        excerpt
        publicationDate(formatString: "MM-DD-YYYY")
        author {
          name
          avatar {
            url
          }
        }
      }
    }
  }
}

【问题讨论】:

    标签: javascript reactjs graphql content-management-system gatsby


    【解决方案1】:

    来自gatsby-source-datocms README:

    重要提示:如果您使用此插件,您将无法按照 DatoCMS Content Delivery API 文档中的说明编写查询。内容将使用 Gatsby 的模式生成公开。如果您想直接在 Gatsby 中使用我们的 GraphQL API,请考虑使用 gatsby-source-graphql 插件。

    【讨论】:

    • 该死的我刚看到,所以我想我得重写一堆我的查询?
    • 谢天谢地,我找到了一种方法,但感谢您的回复,选择源插件时要注意这一点。
    【解决方案2】:

    当您的模型具有表格的“可视化模式”时,会添加一个名为 position 的属性。

    我们可以使用这个position 对记录进行如下排序:

    allDatoCmsProject(sort: { fields: [position], order: ASC }){
    ...
    }
    

    【讨论】:

      猜你喜欢
      • 2020-10-08
      • 1970-01-01
      • 1970-01-01
      • 2013-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-04
      相关资源
      最近更新 更多