graphql - graphql 输入不接受接口和联合的最佳解决方法
问题描述
我有一个突变,它可以做这个人为的例子:
mutation {
updateShelter(body: {
addedAnimals: [],
updatedAnimals: [{type: 'DOG', somethingSpecificWithDogType: 'value'}, {type: 'CAT', somethingSpecificWithCatType: 'value'}]
}) {
# some fields I want returned
}
}
我的类型大致如下:
enum AnimalTypes {
DOG
CAT
}
interface Animal {
type: AnimalTypes!
}
input Dog implements Animal {
type: AnimalTypes!
somethingSpecificWithDogType: String
}
input Cat implements Animal {
type: AnimalTypes!
somethingSpecificWithCatType: String
}
input UpdateShelterInput {
addedDogs: Animal
updatedDogs: Animal
}
extend type Mutation {
updateShelter(body: UpdateShelterInput): SomeResponse
}
我知道输入必须引用其他输入,所以下面的类型不起作用,因为输入是指一个接口
input UpdateShelterInput {
addedDogs: Animal
updatedDogs: Animal
}
有一个 RFC 多年来一直悬而未决,以实现联合/接口类型作为输入,但与此同时,克服这一限制的最佳方法是什么。
一个想法是将正文作为 JSON 字符串/将正文中的 updatedAnimals 设为 JSON 字符串,然后 JSON 解析输入,但这似乎不是最优雅的方法,因为类型安全是使用 graphql 的全部要点。
有什么想法值得用来克服这个限制吗?
解决方案
推荐阅读
- lighthouse - 如何修复“灯塔返回错误:NO_FCP”。运行 Google Page Speed Test 时?
- r - 是否可以将矩阵映射到 igraph 对象?
- node.js - 如何在 Node 项目中使用 AWS 发送电子邮件
- python - 有没有办法在 Flask 应用程序中实现不同链接的列表?
- c# - 如何为按钮设置不同的值并将其传递给监听函数?
- python - 列表超出范围:我试图查看文件但找不到错误所在
- java - Java - 连接到远程虚拟机并复制文件
- javascript - 在 Google 街景中隐藏街道名称
- php - 在 wordpress 模板中使用 php readfile() 时出现空白页
- python - 调整子图中的颜色条