issue-1664.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import { OnApplicationBootstrap } from '@nestjs/common';
  2. import {
  3. Asset,
  4. PluginCommonModule,
  5. Product,
  6. TransactionalConnection,
  7. User,
  8. VendurePlugin,
  9. } from '@vendure/core';
  10. import gql from 'graphql-tag';
  11. import { ProfileAsset } from './profile-asset.entity';
  12. import { Profile } from './profile.entity';
  13. const schema = gql`
  14. type Profile implements Node {
  15. id: ID!
  16. createdAt: DateTime!
  17. updatedAt: DateTime!
  18. name: String!
  19. user: User!
  20. }
  21. `;
  22. /**
  23. * Test plugin for https://github.com/vendure-ecommerce/vendure/issues/1664
  24. *
  25. * Test query:
  26. * ```graphql
  27. * query {
  28. * product(id: 1) {
  29. * name
  30. * customFields {
  31. * owner {
  32. * id
  33. * identifier
  34. * customFields {
  35. * profile {
  36. * id
  37. * name
  38. * }
  39. * }
  40. * }
  41. * }
  42. * }
  43. * }
  44. * ```
  45. */
  46. @VendurePlugin({
  47. imports: [PluginCommonModule],
  48. entities: () => [Profile, ProfileAsset],
  49. shopApiExtensions: { schema, resolvers: [] },
  50. adminApiExtensions: { schema, resolvers: [] },
  51. configuration: config => {
  52. config.customFields.Product.push({
  53. name: 'owner',
  54. nullable: true,
  55. type: 'relation',
  56. entity: User,
  57. public: false,
  58. eager: true, // needs to be eager to enable indexing of user->profile attributes like name, etc.
  59. readonly: true,
  60. });
  61. // User
  62. config.customFields.User.push({
  63. name: 'profile',
  64. type: 'relation',
  65. entity: Profile,
  66. nullable: true,
  67. public: false,
  68. internal: false,
  69. readonly: true,
  70. eager: true, // needs to be eager to enable indexing of profile attributes like name, etc.
  71. });
  72. return config;
  73. },
  74. })
  75. export class Test1664Plugin implements OnApplicationBootstrap {
  76. constructor(private connection: TransactionalConnection) {}
  77. async onApplicationBootstrap() {
  78. const profilesCount = await this.connection.rawConnection.getRepository(Profile).count();
  79. if (0 < profilesCount) {
  80. return;
  81. }
  82. // Create a Profile and assign it to all the products
  83. const users = await this.connection.rawConnection.getRepository(User).find();
  84. // tslint:disable-next-line:no-non-null-assertion
  85. const user = users[1]!;
  86. const profile = await this.connection.rawConnection.getRepository(Profile).save(
  87. new Profile({
  88. name: 'Test Profile',
  89. user,
  90. }),
  91. );
  92. (user.customFields as any).profile = profile;
  93. await this.connection.rawConnection.getRepository(User).save(user);
  94. const asset = await this.connection.rawConnection.getRepository(Asset).findOne(1);
  95. if (asset) {
  96. const profileAsset = this.connection.rawConnection.getRepository(ProfileAsset).save({
  97. asset,
  98. profile,
  99. });
  100. }
  101. const products = await this.connection.rawConnection.getRepository(Product).find();
  102. for (const product of products) {
  103. (product.customFields as any).owner = user;
  104. await this.connection.rawConnection.getRepository(Product).save(product);
  105. }
  106. }
  107. }