11

I have to use a Java object in GraphQL in response as well as in request.

Should I have to write two times each objects as 'input' and 'type' in a GraphQL schema file? For getting that object in request as well as in response.

Should I define the same object two times with input and type?

file: test.graphqls

input Employee {
  id: Integer
  name: String
  dept: String
  active: String
}

type Employee {
  id: Integer
  name: String
  dept: String
  active: String
}
jbarros
  • 628
  • 10
  • 28
Pranav
  • 115
  • 1
  • 6

1 Answers1

8

Yes, because the type system rules are very different for input and output types. Input types can not be unions, can not implement interfaces etc, so you can not simply use the same definition for both purposes.

Moreover, you must give each type a unique name. So it should be Employee and EmployeeInput for example.

kaqqao
  • 12,984
  • 10
  • 64
  • 118
  • thank you.. how would we impliment two different names in schema for a single object ? – Pranav Feb 15 '18 at 03:44
  • @Pranav I'm confused by the question. How are you making the schema? There's no relation between the Java class and the GraphQL type unless you make it. When defining the types simply give them different names. Whether the DataFetchers are backed by the same class or not has no bearing on the name. – kaqqao Feb 15 '18 at 08:07
  • @Pranav e.g. in your schema just have `type Employee` and `input EmployeeInput`, and wire them both in a similar way when making an executable schema. – kaqqao Feb 15 '18 at 09:14
  • Can `input` encapsulate `type` to avoid duplicate member definition? – Kok How Teh Mar 18 '19 at 03:49
  • 1
    @KokHowTeh An input type can only refer to scalars, other input types and lists of those. If they could refer to output types what would the purpose of separating them in the first place be? You'd still end up with potential infinite recursion, interfaces etc (which are all allowed for output types but not inputs). – kaqqao Mar 18 '19 at 08:08