gpt4 book ai didi

graphql - 将输入类型用于 graphql 查询是一种不好的做法吗?

转载 作者:行者123 更新时间:2023-12-03 21:19:38 24 4
gpt4 key购买 nike

我已经看到在突变的上下文中建议插入输入类型,但没有说明任何关于查询的内容。

例如,在 learn tutorial 中只需说:

This is particularly valuable in the case of mutations, where you might want to pass in a whole object to be created



我有这个查询:
type query {
person(personID: ID!): Person
brazilianPerson(rg: ID!): BrazilizanPerson
foreignerPerson(passport: ID!): ForeignerPerson
}

我不能仅仅因为字段的名称(rg,passport)而使用不同的类型,或者在查询中再添加一个类似类型的参数,我不能只拥有带有 documentNr 字段的 Person 并执行这样的输入类型?
input PersonInput {
documentNr : ID!
type: PersonType # this type is Foreign or Brazilian and with this I k
}
PersonType 是一个枚举,和他一起我知道文件是 rg 还是护照。

最佳答案

不,您的方法没有任何不正确之处。 GraphQL 规范允许任何字段有一个参数,并允许任何参数接受输入对象类型,而不管操作如何。实际上,查询和突变之间的区别在于 largely symbolic

值得指出的是,任何字段都可以接受参数——而不仅仅是根级别的参数。因此,如果它适合您的需求,您可以轻松设置一个允许查询的架构,例如:

query {
person(id: 1) {
powers(onlyMutant: true) {
name
}
}
}

关于graphql - 将输入类型用于 graphql 查询是一种不好的做法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55620542/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com