【问题标题】:GraphQL query to filter GraphCMS posts用于过滤 GraphCMS 帖子的 GraphQL 查询
【发布时间】:2018-08-02 11:01:03
【问题描述】:

我正在使用 React 开发一个 GraphCMS 站点,并希望设置一个 GraphQL 查询来过滤帖子。我们有这个查询设置,它可以工作并获取所有帖子(收养):

export const allAdoptions = gql`
  query allAdoptions($first: Int!, $skip: Int!) {
    allAdoptions(orderBy: date_DESC, first: $first, skip: $skip) {
        id
        name
        date
        image {
          handle
        }
        desc
        sex
        species
        neutered
    },
    _allAdoptionsMeta {
      count
    }
  }
`

代码可在此处获得:https://github.com/foxreymann/foranimals/blob/master/src/components/Adoptions.js

我们想设置一个过滤器组件来按物种获取帖子:例如“Cat”,我们尝试只修改上述物种查询:“Cat”,但没有成功。任何建议都非常感谢,谢谢

【问题讨论】:

    标签: reactjs graphql-js


    【解决方案1】:

    您检查了吗:https://graphcms.com/docs/api_simple/#filtering-entries

    它应该像这样工作:

    query {
      allAdoptions(
        filter: {
          species: "maine-coon"
        }
      ) {
        id
        name
      }
    }
    

    迈克尔

    【讨论】:

    • 感谢您的帮助迈克尔。我已经尝试过了,但现在出现错误:“参数 'filter' 预期类型 'AdoptionFilter' 但得到:{species: \"Cat\"}。原因:'species' Enum value expected
    • 我正在为 React 使用 Apollo 客户端,我认为这就是问题所在。
    • 嘿!如果它是一个 Enum,那么您需要删除 '"'。所以 ...species: Cat... 可以解决问题。物种是否允许多个值?那么目前不支持,但很快就会登陆。跨度>
    猜你喜欢
    • 1970-01-01
    • 2021-07-15
    • 1970-01-01
    • 2021-05-07
    • 2019-11-13
    • 2019-11-04
    • 2018-08-29
    • 2019-12-29
    • 2019-07-26
    相关资源
    最近更新 更多