Why does GraphQL choose to use an 'input'

2019-07-18 16:36发布

问题:

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 is another special type in graphql, because in graphql you can't mix input and output types in your schema.

I am still not fully clear why. Why in graphql, I can't mix input and output type?

回答1:

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
}

and your type will be

type User {
   id: ID!
   name: String
}


标签: graphql