Code-first has many advantages and removes duplication. However, one downside is that the types defined have to match compiled Kotlin code. In some cases, it is possible to define a schema that is valid in SDL but it would be impossible to return a Kotlin type that matches exactly that type. In these cases, you can pass in custom types in the schema generator config and annotate the schema with the type info.
A common example is when you need to return a type or union defined in library JAR, but you can not change the code. For example, let's say there is a type in a library. You can not change the fields, add annotations, or have it implement interfaces.
If you want to have this type be used in a new interface or union defined in your API, it is not possible to do in Kotlin code since you can not modify the compiled code.
You can use this annotation to change the return type of a field. The annotation accepts the type name, which will be added as a type reference in the schema. This means that you will have to define the type and its schema with the same name in the configuration.
Doing this could still be serialization issues, so you should make sure that the data you return from the field matches the defined schema of the type.
In our above example there is no Kotlin code for the type
FooOrBar. It only exists by reference right now.
To add the type into the schema, specify the additional types in the SchemaGeneratorConfiguration.
This is using the grapqhl-java schema object builders.
In our above example, since the return type of the Kotlin code did not reference the Kotlin types
reflection will not pick those up by default. They will also need to be added as additional Kotlin types (
KType) when generating the schema.
With all the above code, the final resulting schema should like this: