是否可以忽略所需的类型(在这种情况下,createCarEvent
的类型输入包含变量 $from
作为 String!
)使用@include
?我尝试使用 $from
作为 String
而不是 String!
但它导致了异常:
Variable "$from" of type "String" used in position expecting type "String!"
$isNeedCreateCarEvent
检查两个变量($from
和 $to
)是否存在。
mutation rejectBooking(... $from: String, $to: String, $isNeedCreateCarEvent: Boolean!) {
...
createCarEvent(
input: {
...
from: $from
to: $to
}
) @include(if: $isNeedCreateCarEvent) {
...eventItem
}
方案:
input CarEventInput {
...
from: String!
to: String
}
最佳答案
如果 $isNeedCreateCarEvent
为 true
,GraphQL 无法知道始终提供 $from
,类型声明无法表达这一点 (并且您的调用代码不能保证这一点)。
如果您想让 $from
和 $to
可选,您最好使用两种单独的突变,一种带有 createCarEvent
,另一种不带有 $from
和 $to
.
如果这对您来说意味着太多重复(因为查询的其余部分),唯一的方法是在使用 $isNeedCreateCarEvent = false 时将参数声明为非空并将空字符串作为虚拟值传递
.
关于javascript - 是否可以忽略 graphql 中与 @include 一起使用的所需类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59652934/