如何在Gatsby GraphQL中过滤子数组?

时间:2020-10-27 10:57:08

标签: graphql gatsby sanity

这使我获得所有与showOnSite === true相关的事件:

query MyQuery {
  allSanityP2Event(filter: {showOnSite: {eq: true}}) {
    edges {
      node {
        showOnSite
        title
        relatedEvents {
          showOnSite
          title
        }
      }
    }
  }
}

然后,我还要在这些结果中过滤relatedEvents,也要过滤showOnSite === true。 GraphQL当然可以做到吗?

使用elemMatch不起作用,因为它仅向我提供relatedEvents具有showOnSite === true的事件,例如:

query MyQuery {
  allSanityP2Event(filter: {showOnSite: {eq: true}, relatedEvents: {elemMatch: {showOnSite: {eq: true}}}}) {
    edges {
      node {
        showOnSite
        title
        relatedEvents {
          showOnSite
          title
        }
      }
    }
  }
}

从逻辑上讲,我希望这能奏效,但是没有...

query MyQuery {
  allSanityP2Event(filter: {showOnSite: {eq: true}}) {
    edges {
      node {
        showOnSite
        title
        relatedEvents(filter: {showOnSite: {eq: true}}) {
          showOnSite
          title
        }
      }
    }
  }
}

1 个答案:

答案 0 :(得分:0)

就是这样解决的。原来这是理智的错。

enter image description here