graphiql.service.ts 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import { Injectable } from '@nestjs/common';
  2. import { ConfigService } from '@vendure/core';
  3. /**
  4. * This service is responsible for providing GraphiQL configuration
  5. * and a fallback UI if needed.
  6. */
  7. @Injectable()
  8. export class GraphiQLService {
  9. constructor(private configService: ConfigService) {}
  10. /**
  11. * Get the Admin API URL
  12. */
  13. getAdminApiUrl(): string {
  14. const adminApiPath = this.configService.apiOptions.adminApiPath || 'admin-api';
  15. return this.createApiUrl(adminApiPath);
  16. }
  17. /**
  18. * Get the Shop API URL
  19. */
  20. getShopApiUrl(): string {
  21. const shopApiPath = this.configService.apiOptions.shopApiPath || 'shop-api';
  22. return this.createApiUrl(shopApiPath);
  23. }
  24. /**
  25. * Create a fully-qualified API URL
  26. */
  27. private createApiUrl(apiPath: string): string {
  28. // Get API host and port from the config
  29. const apiHost = this.configService.apiOptions.hostname || '';
  30. const apiPort = this.configService.apiOptions.port || '';
  31. const host = apiHost || '';
  32. const port = apiPort ? `:${apiPort}` : '';
  33. const pathUrl = apiPath.startsWith('/') ? apiPath : `/${apiPath}`;
  34. // If the host is specified, create a fully-qualified URL
  35. if (host) {
  36. const protocol = host.startsWith('https') ? '' : 'http://';
  37. return `${protocol}${host}${port}${pathUrl}`;
  38. }
  39. // Otherwise use a relative URL
  40. return pathUrl;
  41. }
  42. }