1
0

hydration-test-plugin.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /* eslint-disable @typescript-eslint/no-non-null-assertion */
  2. import { Args, Query, Resolver } from '@nestjs/graphql';
  3. import {
  4. Asset,
  5. ChannelService,
  6. Ctx,
  7. EntityHydrator,
  8. ID,
  9. LanguageCode,
  10. OrderService,
  11. PluginCommonModule,
  12. Product,
  13. ProductService,
  14. ProductVariantService,
  15. RequestContext,
  16. TransactionalConnection,
  17. VendurePlugin,
  18. } from '@vendure/core';
  19. import gql from 'graphql-tag';
  20. @Resolver()
  21. export class TestAdminPluginResolver {
  22. constructor(
  23. private connection: TransactionalConnection,
  24. private orderService: OrderService,
  25. private channelService: ChannelService,
  26. private productVariantService: ProductVariantService,
  27. private productService: ProductService,
  28. private entityHydrator: EntityHydrator,
  29. ) {}
  30. @Query()
  31. async hydrateProduct(@Ctx() ctx: RequestContext, @Args() args: { id: ID }) {
  32. const product = await this.connection.getRepository(ctx, Product).findOne({
  33. where: { id: args.id },
  34. relations: ['facetValues'],
  35. });
  36. // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
  37. await this.entityHydrator.hydrate(ctx, product!, {
  38. relations: [
  39. 'variants.options',
  40. 'variants.product',
  41. 'assets.product',
  42. 'facetValues.facet',
  43. 'featuredAsset',
  44. 'variants.stockMovements',
  45. ],
  46. applyProductVariantPrices: true,
  47. });
  48. return product;
  49. }
  50. // Test case for https://github.com/vendure-ecommerce/vendure/issues/1153
  51. @Query()
  52. async hydrateProductAsset(@Ctx() ctx: RequestContext, @Args() args: { id: ID }) {
  53. const product = await this.connection.getRepository(ctx, Product).findOne({ where: { id: args.id } });
  54. // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
  55. await this.entityHydrator.hydrate(ctx, product!, {
  56. relations: ['assets'],
  57. });
  58. return product;
  59. }
  60. // Test case for https://github.com/vendure-ecommerce/vendure/issues/1161
  61. @Query()
  62. async hydrateProductVariant(@Ctx() ctx: RequestContext, @Args() args: { id: ID }) {
  63. const [variant] = await this.productVariantService.findByIds(ctx, [args.id]);
  64. await this.entityHydrator.hydrate(ctx, variant, {
  65. relations: ['product.facetValues.facet'],
  66. });
  67. return variant;
  68. }
  69. // Test case for https://github.com/vendure-ecommerce/vendure/issues/1324
  70. @Query()
  71. async hydrateProductWithNoFacets(@Ctx() ctx: RequestContext) {
  72. const product = await this.productService.create(ctx, {
  73. enabled: true,
  74. translations: [
  75. {
  76. languageCode: LanguageCode.en,
  77. name: 'test',
  78. slug: 'test',
  79. description: 'test',
  80. },
  81. ],
  82. });
  83. await this.entityHydrator.hydrate(ctx, product, {
  84. relations: ['facetValues', 'facetValues.facet'],
  85. });
  86. return product;
  87. }
  88. // Test case for https://github.com/vendure-ecommerce/vendure/issues/1172
  89. @Query()
  90. async hydrateOrder(@Ctx() ctx: RequestContext, @Args() args: { id: ID }) {
  91. const order = await this.orderService.findOne(ctx, args.id);
  92. await this.entityHydrator.hydrate(ctx, order!, {
  93. relations: ['payments'],
  94. });
  95. return order;
  96. }
  97. // Test case for https://github.com/vendure-ecommerce/vendure/issues/1229
  98. @Query()
  99. async hydrateOrderReturnQuantities(@Ctx() ctx: RequestContext, @Args() args: { id: ID }) {
  100. const order = await this.orderService.findOne(ctx, args.id);
  101. await this.entityHydrator.hydrate(ctx, order!, {
  102. relations: [
  103. 'lines',
  104. 'lines.productVariant',
  105. 'lines.productVariant.product',
  106. 'lines.productVariant.product.assets',
  107. ],
  108. });
  109. return order?.lines.map(line => line.quantity);
  110. }
  111. // Test case for https://github.com/vendure-ecommerce/vendure/issues/1284
  112. @Query()
  113. async hydrateChannel(@Ctx() ctx: RequestContext, @Args() args: { id: ID }) {
  114. const channel = await this.channelService.findOne(ctx, args.id);
  115. await this.entityHydrator.hydrate(ctx, channel!, {
  116. relations: ['customFields.thumb'],
  117. });
  118. return channel;
  119. }
  120. }
  121. @VendurePlugin({
  122. imports: [PluginCommonModule],
  123. adminApiExtensions: {
  124. resolvers: [TestAdminPluginResolver],
  125. schema: gql`
  126. extend type Query {
  127. hydrateProduct(id: ID!): JSON
  128. hydrateProductWithNoFacets: JSON
  129. hydrateProductAsset(id: ID!): JSON
  130. hydrateProductVariant(id: ID!): JSON
  131. hydrateOrder(id: ID!): JSON
  132. hydrateOrderReturnQuantities(id: ID!): JSON
  133. hydrateChannel(id: ID!): JSON
  134. }
  135. `,
  136. },
  137. configuration: config => {
  138. config.customFields.Channel.push({ name: 'thumb', type: 'relation', entity: Asset, nullable: true });
  139. return config;
  140. },
  141. })
  142. export class HydrationTestPlugin {}