在输入可以采用不同类型的情况下生成Graphql输入
我想创建变异API,其中输入可以是不同的类型,类似于我们在类型中拥有的接口。我知道我们不能在输入类型中使用接口,我想知道如何在一个输入中支持多个输入类型。为了解释这个问题,我使用了一个虚拟示例:在输入可以采用不同类型的情况下生成Graphql输入,graphql,graphql-java,graphql-schema,Graphql,Graphql Java,Graphql Schema,我想创建变异API,其中输入可以是不同的类型,类似于我们在类型中拥有的接口。我知道我们不能在输入类型中使用接口,我想知道如何在一个输入中支持多个输入类型。为了解释这个问题,我使用了一个虚拟示例: input CreateCatInput{ id: String name: String } input CreateDogInput{ id: String name: String breed: String } input CreateElephantInput{ i
input CreateCatInput{
id: String
name: String
}
input CreateDogInput{
id: String
name: String
breed: String
}
input CreateElephantInput{
id: String
name: String
weight: String
}
现在,如果我们想为它编写api,我必须为每种类型编写api
createCat(input: CreateCatInput!)
createDog(input: CreateDogInput!)
createElephant(input: CreateElephantInput!)
这种方法的问题是:
createAnimal(input: CreateAnimalInput!)
由于目前没有接口支持,公司是如何实现多种类型的输入的?如何定义输入,以便在api中只能提供一个输入
我读过这篇文章,但它涉及到定义注释,我目前正在尝试。我想看看其他人是如何解决这个问题的
编辑:看起来在这个主题上已经做了很多工作,该功能很快就可以使用。输入联合类型可以解决您的问题,但不幸的是,现在不支持它。然而,好消息是,这个特性已经有了一个新版本,这意味着它有可能包含在下一个GraphQL规范版本中 现在,我将使用带有枚举的嵌套输入对其进行建模,以区分用户实际想要创建的动物类型。有些事情看起来像:
input CreateAnimalInput{
id: String
name: String
animalType :AnimalType!
dogParam : CreateDogInput
elephantParam : CreateElephantInput
}
enum AnimalType{
DOG
ELEPHANT
}
input CreateDogInput{
breed: String
}
input CreateElephantInput{
weight: String
}
createAnimal(input: CreateAnimalInput!)
如果
animalType
设置为DOG
,则只考虑dogParam
字段中的值,其他动物参数字段将被忽略。Hey@Ken Chan,感谢您的回答。由于没有联合支持,开发人员只能使用您讨论过的组合方式,或者为dog/cat创建单独的实体。@Key Chan。谢谢你的详细回答。我也想到了这个嵌套的解决方案,但你解释得很好。我在这里看到的唯一问题是更新api也将被嵌套,在一个更新api中,我们将同时更新狗和猫。更新还有其他方法吗?更新模式将与名称完全相同。因此@Key Chan无需添加更新部分。