graphql - 如果 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
并且查询被跳过
解决方案
问题不在于skip
-variables
无法保证这些选项的评估顺序,因此即使skip
评估为true
,该options
函数仍可能被调用。如果它被调用并且props.getUboDeclarations.getUboDeclarationsForThisUser
实际上是null
,你会得到一个 TypeError 因为你试图读取一个值的_id
属性。null
解决方法是修改variables
以考虑可能的空值:
variables: {
_id: props.getUboDeclarations && props.getUboDeclarations.getUboDeclarationsForThisUser && props.getUboDeclarations.getUboDeclarationsForThisUser_id
}
请注意,您可以使用 lodash 之类的东西get
来使这些空保护子句更易于处理。
推荐阅读
- javascript - 为什么 reCAPTCHA v3 不提交我的表单?
- html - 如何获取表的当前列值
- c# - 在 .NET Core 中序列化 FeatureCollection GEOJSON
- java - 使用 Spring Boot 和 Ecache 时 javax.cache 依赖的意义何在?
- c# - 检查 Blazor Progressiv Web 应用程序中的权限状态
- sql - 9 月 1 日在 8 月 31 日之前在 Zeppelin 中按日期排序的条形图,请问如何解决?
- amazon-web-services - 如何在 AWS API Gateway 中映射每条路由
- javascript - 在 javascript 或 jquery 中添加指向数据表行的链接
- json - 我如何使用表单请求和 ajax json 验证 laravel 8 的响应
- wpf - Powershell - 在 WinForm 选项卡中嵌入 WPF 表单