2

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?

Nicolas S.Xu
  • 13,794
  • 31
  • 84
  • 129

1 Answers1

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
}
Ariel Henryson
  • 1,316
  • 1
  • 10
  • 13
  • 2
    If fields are the same, e.g. query type can be "type User {name: String}", same as UserInput. Why not type and input type are interchangeable? – Nicolas S.Xu Aug 30 '17 at 20:32
  • Is there a way to [reuse input types as fragments](https://stackoverflow.com/questions/52452982/reusing-input-type-as-fragment-in-graphql)? – Dan Dascalescu Sep 22 '18 at 02:28