I am "playing" with graphql-java
and the related kickstarter servlet, modifying it to build the schema programmatically based on the types I discover automatically. The documentation is (almost) there but is outdated in spots and references deprecated methods. My particular issue is that the GraphQLFieldDefinition.Builder
has four variants of the type(...)
method - three accepting various (other) builders and one accepting the GraphQLOutputType
, none of which I may have. Furthermore, those that accept builder actually invoke the build()
method on those builders so I am anxious to pass any of my "unfinished" type builders either. I thought of defining all types upfront with no fields at all, then add fields to them after that but if build()
gets called on these at that point in time, even if they don't get into an unwanted state today I can see how a future update may demand at most a single build()
invocation.
So the question is how does one declare a field to yet-undefined or not fully defined type? For example, an Employee
's manager
field being itself of the Employee
type such as:
type Employee {
name: String!
manager: Employee
reports: [Employee!]
buddy: Employee
...
}
As I noted in my comments, instead of attempting to (directly) use the builder or the built type one should create an instance of GraphQLTypeReference
instead like so:
GraphQLTypeReference.typeRef(typeName)
This will be resolved automatically when the schema is built.
Should we want to indicate that this is not nullable even if the referenced type is, we further need to wrap into a GraphQLNonNull
wrapper like so:
GraphQLNonNull.nonNull(typeRef)
... and use that as the type.