with-custom-entity.ts 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import { Collection, PluginCommonModule, VendureEntity, VendurePlugin } from '@vendure/core';
  2. import gql from 'graphql-tag';
  3. import { DeepPartial, Entity, ManyToMany, OneToMany } from 'typeorm';
  4. declare module '@vendure/core/dist/entity/custom-entity-fields' {
  5. interface CustomCollectionFields {
  6. customEntity: TestCustomEntity;
  7. customEntityList: TestCustomEntity[];
  8. }
  9. }
  10. @Entity()
  11. export class TestCustomEntity extends VendureEntity {
  12. constructor(input?: DeepPartial<TestCustomEntity>) {
  13. super(input);
  14. }
  15. @ManyToMany(() => Collection, c => c.customFields?.customEntityList, {
  16. eager: true,
  17. })
  18. customEntityListInverse: Collection[];
  19. @OneToMany(() => Collection, c => c.customFields.customEntity)
  20. customEntityInverse: Collection[];
  21. }
  22. @VendurePlugin({
  23. imports: [PluginCommonModule],
  24. entities: [TestCustomEntity],
  25. adminApiExtensions: {
  26. schema: gql`
  27. type TestCustomEntity {
  28. id: ID!
  29. }
  30. `,
  31. },
  32. configuration: config => {
  33. config.customFields = {
  34. ...(config.customFields ?? {}),
  35. Collection: [
  36. ...(config.customFields?.Collection ?? []),
  37. {
  38. name: 'customEntity',
  39. type: 'relation',
  40. entity: TestCustomEntity,
  41. list: false,
  42. public: false,
  43. inverseSide: (t: TestCustomEntity) => t.customEntityInverse,
  44. graphQLType: 'TestCustomEntity',
  45. },
  46. {
  47. name: 'customEntityList',
  48. type: 'relation',
  49. entity: TestCustomEntity,
  50. list: true,
  51. public: false,
  52. inverseSide: (t: TestCustomEntity) => t.customEntityListInverse,
  53. graphQLType: 'TestCustomEntity',
  54. },
  55. ],
  56. };
  57. return config;
  58. },
  59. })
  60. export class WithCustomEntity {}