Browse Source

docs: Add docs on custom field inverseSide

Michael Bromley 2 years ago
parent
commit
5da18f2113
1 changed files with 40 additions and 0 deletions
  1. 40 0
      docs/docs/guides/developer-guide/custom-fields/index.md

+ 40 - 0
docs/docs/guides/developer-guide/custom-fields/index.md

@@ -870,6 +870,46 @@ const config = {
 
 In the above example, the `CmsArticle` entity is being used as a related entity. Howeer, the GraphQL type name is `BlogPost`, so we must specify this in the `graphQLType` property, otherwise Vendure will try to extend the GraphQL schema with reference to a non-existent "CmsArticle" type.
 
+### `inverseSide`
+
+<span class="badge badge--secondary">Optional</span>
+
+`string | ((object: VendureEntity) => any);`
+
+Allows you to specify the [inverse side of the relation](https://typeorm.io/#inverse-side-of-the-relationship). Let's say you are adding a relation from `Product`
+to a custom entity which refers back to the product. You can specify this inverse relation like so:
+
+```ts title="src/vendure-config.ts"
+import { Product } from '\@vendure/core';
+import { ProductReview } from './entities/product-review.entity';
+
+const config = {
+    // ...
+    customFields: {
+        Product: [
+            {
+                name: 'reviews',
+                list: true,
+                type: 'relation',
+                entity: ProductReview,
+                // highlight-start
+                inverseSide: (review: ProductReview) => review.product,
+                // highlight-end
+            },
+        ]
+    }
+};
+```
+
+This then allows you to query the `ProductReview` entity and include the `product` relation:
+
+```ts
+const { productReviews } = await this.connection.getRepository(ProductReview).findOne({
+    where: { id: 1 },
+    relations: ['product'],
+});
+```
+
 ## Custom Field UI
 
 In the Admin UI, an appropriate default form input component is used for each custom field type. The Admin UI comes with a set of ready-made form input components, but it is also possible to create custom form input components. The ready-made components are: