【问题标题】:Why can't i filter a Deep nested query in graphql?为什么我不能在 graphql 中过滤深层嵌套查询?
【发布时间】:2020-02-19 21:08:42
【问题描述】:

所以我最近才开始使用 graphql(准确地说是上周)。我得到了能够使用“where”参数过滤查询的概念,但我注意到,当我尝试从深层嵌套查询中获取对象时,它变得有点不可能。所以说我有'domestic_worker_nextofkin'查询应该得到全名、mobile_phone1和'archived'_is_null等于true的关系,如下所示:

domestic_workers_nextofkin(where: {archived:{_is_null: true}}){
    full_name
    mobile_phone1
    domestic_workers_relationships{
      relationship
    }
  }
}

上面的查询完成了它的预期工作,并获得了 full_name、mobile_phone1 和关系。

但我目前的问题是,当这个查询嵌套很深时,如下所示(星号代码):

query User($userId: Int) {
     domestic_workers_news{
          title
          date_created
          summary
          url
        }
    users_user(where: {id: {_eq: $userId}}) {
      domestic_workers_pictures(order_by: {id: desc}, limit: 1) {
        id
      }
      id
      password
      username
      first_name
      last_name
      email
      gender
      birth_date
      mobile_phone1
      mobile_phone2
      home_address_street
      home_address_town
      home_address_lga
      home_address_state
      home_address_country
      home_address_postal_code
      job_title
      workplace
      verified
      agreed_terms
      date_joined
      last_modified
      domestic_workers_workers {
        domestic_workers_pictures(order_by: {id: desc}, limit: 1) {
          id
        }
        id
        first_name
        last_name
        other_name
        email
        mobile_phone1
        mobile_phone2
        home_town
        home_state
        service
        birth_date
        gender
        date_created
        current_employer_id
        domestic_workers_relationships{
          id
          payment
          payment_currency
          start_date
          relationship
          domestic_workers_agent {
            full_name
            mobile_phone1
            domestic_workers_relationships{
              relationship
            }
            domestic_workers_pictures(order_by: {id: desc}, limit: 1) {
              id
            }
          }


**domestic_workers_nextofkin (where: {archived:{_is_null: true}}) {
        full_name
        mobile_phone1
         domestic_workers_relationships{
          relationship
        }
      }
    }**
        domestic_workers_reviews {
          id
          score
          summary
          date_created
          users_user {
            first_name
            last_name
            domestic_workers_pictures(order_by: {id: desc}, limit: 1) {
              id
            }
          }
        }
        employerRelationships: domestic_workers_relationships(order_by: {id: desc}, limit: 1, where: {relationship: {_eq: "Employer"}}) {
         end_date
         relationship
        }
      }
    }
   }

它显示“where”参数以红色突出显示,这可能意味着“where”参数不应该放在那里,这对我来说似乎相当混乱。谁能解释为什么会发生这种情况并向我展示一种解决方法来完成我正在尝试执行的任务?谢谢。

【问题讨论】:

    标签: graphql hasura


    【解决方案1】:

    来自the docs

    where 参数也可用于数组关系 以及过滤嵌套对象。 对象关系只有一个嵌套对象,因此它们不公开 where 参数。

    users_user 是一个对象关系(它返回单个对象而不是它们的数组),因此无法过滤。

    【讨论】:

    • 非常感谢您的解释。但是根据您对 graphql 的了解,您对这个问题有何建议?
    • 好的,我刚刚阅读了文档,我明白了你的意思。但就我而言,它实际上是返回单个对象的国内工人nextofkin。非常感谢。
    猜你喜欢
    • 2017-10-01
    • 2015-10-16
    • 2018-08-04
    • 1970-01-01
    • 2018-07-09
    • 1970-01-01
    • 2015-11-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多