| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- /* eslint-disable @typescript-eslint/no-non-null-assertion */
- import { Args, Query, Resolver } from '@nestjs/graphql';
- import {
- Asset,
- ChannelService,
- Ctx,
- EntityHydrator,
- ID,
- LanguageCode,
- OrderService,
- PluginCommonModule,
- Product,
- ProductService,
- ProductVariantService,
- RequestContext,
- TransactionalConnection,
- VendurePlugin,
- } from '@vendure/core';
- import gql from 'graphql-tag';
- @Resolver()
- export class TestAdminPluginResolver {
- constructor(
- private connection: TransactionalConnection,
- private orderService: OrderService,
- private channelService: ChannelService,
- private productVariantService: ProductVariantService,
- private productService: ProductService,
- private entityHydrator: EntityHydrator,
- ) {}
- @Query()
- async hydrateProduct(@Ctx() ctx: RequestContext, @Args() args: { id: ID }) {
- const product = await this.connection.getRepository(ctx, Product).findOne({
- where: { id: args.id },
- relations: ['facetValues'],
- });
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
- await this.entityHydrator.hydrate(ctx, product!, {
- relations: [
- 'variants.options',
- 'variants.product',
- 'assets.product',
- 'facetValues.facet',
- 'featuredAsset',
- 'variants.stockMovements',
- ],
- applyProductVariantPrices: true,
- });
- return product;
- }
- // Test case for https://github.com/vendure-ecommerce/vendure/issues/1153
- @Query()
- async hydrateProductAsset(@Ctx() ctx: RequestContext, @Args() args: { id: ID }) {
- const product = await this.connection.getRepository(ctx, Product).findOne({ where: { id: args.id } });
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
- await this.entityHydrator.hydrate(ctx, product!, {
- relations: ['assets'],
- });
- return product;
- }
- // Test case for https://github.com/vendure-ecommerce/vendure/issues/1161
- @Query()
- async hydrateProductVariant(@Ctx() ctx: RequestContext, @Args() args: { id: ID }) {
- const [variant] = await this.productVariantService.findByIds(ctx, [args.id]);
- await this.entityHydrator.hydrate(ctx, variant, {
- relations: ['product.facetValues.facet'],
- });
- return variant;
- }
- // Test case for https://github.com/vendure-ecommerce/vendure/issues/1324
- @Query()
- async hydrateProductWithNoFacets(@Ctx() ctx: RequestContext) {
- const product = await this.productService.create(ctx, {
- enabled: true,
- translations: [
- {
- languageCode: LanguageCode.en,
- name: 'test',
- slug: 'test',
- description: 'test',
- },
- ],
- });
- await this.entityHydrator.hydrate(ctx, product, {
- relations: ['facetValues', 'facetValues.facet'],
- });
- return product;
- }
- // Test case for https://github.com/vendure-ecommerce/vendure/issues/1172
- @Query()
- async hydrateOrder(@Ctx() ctx: RequestContext, @Args() args: { id: ID }) {
- const order = await this.orderService.findOne(ctx, args.id);
- await this.entityHydrator.hydrate(ctx, order!, {
- relations: ['payments'],
- });
- return order;
- }
- // Test case for https://github.com/vendure-ecommerce/vendure/issues/1229
- @Query()
- async hydrateOrderReturnQuantities(@Ctx() ctx: RequestContext, @Args() args: { id: ID }) {
- const order = await this.orderService.findOne(ctx, args.id);
- await this.entityHydrator.hydrate(ctx, order!, {
- relations: [
- 'lines',
- 'lines.productVariant',
- 'lines.productVariant.product',
- 'lines.productVariant.product.assets',
- ],
- });
- return order?.lines.map(line => line.quantity);
- }
- // Test case for https://github.com/vendure-ecommerce/vendure/issues/1284
- @Query()
- async hydrateChannel(@Ctx() ctx: RequestContext, @Args() args: { id: ID }) {
- const channel = await this.channelService.findOne(ctx, args.id);
- await this.entityHydrator.hydrate(ctx, channel!, {
- relations: ['customFields.thumb'],
- });
- return channel;
- }
- }
- @VendurePlugin({
- imports: [PluginCommonModule],
- adminApiExtensions: {
- resolvers: [TestAdminPluginResolver],
- schema: gql`
- extend type Query {
- hydrateProduct(id: ID!): JSON
- hydrateProductWithNoFacets: JSON
- hydrateProductAsset(id: ID!): JSON
- hydrateProductVariant(id: ID!): JSON
- hydrateOrder(id: ID!): JSON
- hydrateOrderReturnQuantities(id: ID!): JSON
- hydrateChannel(id: ID!): JSON
- }
- `,
- },
- configuration: config => {
- config.customFields.Channel.push({ name: 'thumb', type: 'relation', entity: Asset, nullable: true });
- return config;
- },
- })
- export class HydrationTestPlugin {}
|