From Vendure v2.2, it is possible to add support for custom fields to your custom entities. This is useful when you are defining a custom entity as part of a plugin which is intended to be used by other developers. For example, a plugin which defines a new entity for storing product reviews might want to allow the developer to add custom fields to the review entity.
First you need to update your entity class to implement the HasCustomFields interface, and provide an empty class
which will be used to store the custom field values:
Given the above entity your API extension might look like this:
Notice the lack of manually defining customFields on the types, this is because Vendure extends the types automatically once your entity implements HasCustomFields.
:::important Naming convention In order for Vendure to find the correct input types to extend to, they must conform to the naming convention of:
Create<EntityName>InputUpdate<EntityName>InputAnd if your entity is supporting translations:
<EntityName>Translation<EntityName>TranslationInputCreate<EntityName>TranslationInputUpdate<EntityName>TranslationInput
:::Following this caveat, codegen will now produce correct types including customFields-fields like so:
Creating and updating your entity works now by setting the fields like usual, with one important addition being, you mustn't forget to update relations via the CustomFieldRelationService. This is needed because a consumer of your plugin may extend the entity with custom fields of type relation which need to get saved separately.
Now you'll be able to add custom fields to the ProductReview entity via the VendureConfig:
Extending entities will alter the database schema requiring a migration. See the migrations guide for further details.