为什么GraphQL选择使用“输入"类型? [英] Why does GraphQL choose to use an 'input' type?
问题描述
这是我对GraphQL的Client和ClientInput的定义
Here is my definition for Client and ClientInput for GraphQL
type Client {
_id: String
short_name: String
full_name: String
address: String
contact_name: String
contact_email: String
contract_currency: String
location: String
}
input ClientInput {
short_name: String
full_name: String
address: String
contact_name: String
contact_email: String
contract_currency: String
location: String
}
它们大致相同.他们为什么选择发明一种输入类型?
They are more or less the same. Why do they choose to invent an input type?
这是我从他们的正式文件中发现的内容:
Here is what I found from their official document:
input是graphql中的另一种特殊类型,因为在graphql中您不能在模式中混合输入和输出类型.
input is another special type in graphql, because in graphql you can't mix input and output types in your schema.
我仍然不太清楚为什么.为什么在graphql中,我不能混合输入和输出类型?
I am still not fully clear why. Why in graphql, I can't mix input and output type?
推荐答案
由于将数据插入数据库时,查询数据时不一定具有相同的数据.例如,您想强迫人们使用id查询"user",但是在创建用户时没有id,因此您的输入将是
Because when you insert data to DB you not necessarily have the same data when you query the data. For example you want to force people to query 'user' with id but you don't have id when you create the user so your input will be
input UserInput {
name: String
}
您的类型将是
type User {
id: ID!
name: String
}
这篇关于为什么GraphQL选择使用“输入"类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!