Type Definition Factory
The Type Definition Factory has been added with to be able to dynamically add type definitions to the schema instead of having to define them manually in the SDL. There are a couple of use cases where the types that have to be defined are very much alike and only certain parts are different. For example the connection and edge types used by Relay. Since generics isn’t supported in the definition language this type definition factory has been added.
Basic usage
Immediately after the SDL has been parsed the schema parser checks if any
TypeDefinitionFactory
instances have been defined in SchemaParserOptions
.
By default the RelayConnectionFactory
is available.
You can add your own type definition factories by implementing the TypeDefinitionFactory
.
It contains just one method. All definitions that have been found after parsing
the SDL and any preceding type definition factories are passed in. This allows you
to perform some logic based on the current schema to determine which type definitions
to create. You must only return the new type definition you want to add to the schema.
The existing definitions that were passed in should not be included in the returned list.
class MyTypeDefinitionFactory implements TypeDefinitionFactory {
public List<Definition<?>> create(final List<Definition<?>> existing) {
return ObjectTypeDefinition.newObjectTypeDefinition()
.name("MyType")
.fieldDefinition(new fieldDefinition("myField", new TypeName("String")))
.build();
}
}
Make sure to let graphql-spring-boot
know you want to use your custom
type definition factory by simply exposing it as a bean, e.g.:
@Bean
public TypeDefinitionFactory myTypeDefinitionFactory() {
return new MyTypeDefinitionFactory();
}