如果该对象不存在,我试图跳过查询,但是它似乎不起作用,我想知道对此是否有解决方案。
这是我的查询
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
并跳过查询
答案 0 :(得分:0)
问题不是skip
到variables
一样多-无法保证评估这些选项的顺序,因此即使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
}
}
})
}),