custom-mappings.resolver.ts 1001 B

123456789101112131415161718192021222324
  1. import { Inject } from '@nestjs/common';
  2. import { ResolveField, Resolver } from '@nestjs/graphql';
  3. import { DeepRequired } from '@vendure/common/lib/shared-types';
  4. import { ELASTIC_SEARCH_OPTIONS } from '../constants';
  5. import { ElasticsearchOptions } from '../options';
  6. /**
  7. * This resolver is only required if both customProductMappings and customProductVariantMappings are
  8. * defined, since this particular configuration will result in a union type for the
  9. * `SearchResult.customMappings` GraphQL field.
  10. */
  11. @Resolver('CustomMappings')
  12. export class CustomMappingsResolver {
  13. constructor(@Inject(ELASTIC_SEARCH_OPTIONS) private options: DeepRequired<ElasticsearchOptions>) {}
  14. @ResolveField()
  15. __resolveType(value: any): string {
  16. const productPropertyNames = Object.keys(this.options.customProductMappings);
  17. return Object.keys(value).every(k => productPropertyNames.includes(k))
  18. ? 'CustomProductMappings'
  19. : 'CustomProductVariantMappings';
  20. }
  21. }