如果GraphQL中的变量为null,如何跳过查询?

时间:2019-09-12 10:29:32

标签: graphql

如果该对象不存在,我试图跳过查询,但是它似乎不起作用,我想知道对此是否有解决方案。

这是我的查询

graphql(GET_UBO_DECLARATIONS, {
    name: "getUboDeclarations",
    options: () => ({
      fetchPolicy: "network-only"
    })
  }),
  graphql(GET_UBOS, {
    name: "getUbos",
    skip: props =>
      !props.getUboDeclarations ||
      !props.getUboDeclarations.getUboDeclarationsForThisUser,
    options: props => ({
      fetchPolicy: "network-only",
      variables: {
        _id: props.getUboDeclarations.getUboDeclarationsForThisUser._id
      }
    })
  }),

props.getUboDeclarations.getUboDeclarationsForThisUser返回null,因此应为false并跳过查询

2 个答案:

答案 0 :(得分:0)

问题不是skipvariables一样多-无法保证评估这些选项的顺序,因此即使skip的评估结果为true ,仍可以调用options函数。如果调用它,而props.getUboDeclarations.getUboDeclarationsForThisUser实际上是null,则将收到TypeError,因为您试图读取_id值的null属性。解决方法是修改variables以解决可能的null:

variables: {
  _id: props.getUboDeclarations && props.getUboDeclarations.getUboDeclarationsForThisUser && props.getUboDeclarations.getUboDeclarationsForThisUser_id
}

请注意,您可以使用lodash的get之类的东西来使这些null保护子句更易于处理。

答案 1 :(得分:-1)

我相信您在where查询中缺少getUbos

graphql(GET_UBO_DECLARATIONS, {
name: "getUboDeclarations",
options: () => ({
  fetchPolicy: "network-only"
})
}),
graphql(GET_UBOS, {
name: "getUbos",
skip: props =>
  !props.getUboDeclarations ||
  !props.getUboDeclarations.getUboDeclarationsForThisUser,
options: props => ({
  fetchPolicy: "network-only",
  variables: {
    where: {// this is missing
       _id: props.getUboDeclarations.getUboDeclarationsForThisUser._id
    }
  }
})

}),