Should I write two times each objects as 'input' and 'type' in a graphql schema file
Asked Answered
A

1

13

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
}
Array answered 14/2, 2018 at 12:22 Comment(0)
I
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.

Inequitable answered 14/2, 2018 at 13:8 Comment(5)
thank you.. how would we impliment two different names in schema for a single object ?Array
@Array 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.Inequitable
@Array 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.Inequitable
Can input encapsulate type to avoid duplicate member definition?Lafreniere
@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).Inequitable

© 2022 - 2024 — McMap. All rights reserved.