apollo-cache.e2e-spec.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import type { KeyValueCache } from '@apollo/utils.keyvaluecache';
  2. import { mergeConfig } from '@vendure/core';
  3. import { createTestEnvironment } from '@vendure/testing';
  4. import path from 'path';
  5. import { afterAll, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest';
  6. import { initialData } from '../../../e2e-common/e2e-initial-data';
  7. import { TEST_SETUP_TIMEOUT_MS, testConfig } from '../../../e2e-common/test-config';
  8. import * as Codegen from './graphql/generated-e2e-admin-types';
  9. import { GET_PRODUCT_LIST, GET_PRODUCT_SIMPLE } from './graphql/shared-definitions';
  10. describe('Apollo cache configuration', () => {
  11. describe('with custom cache implementation', () => {
  12. const mockCache: KeyValueCache<string> = {
  13. get: vi.fn().mockResolvedValue(undefined),
  14. set: vi.fn().mockResolvedValue(undefined),
  15. delete: vi.fn().mockResolvedValue(true),
  16. };
  17. const { server, adminClient, shopClient } = createTestEnvironment(
  18. mergeConfig(testConfig(), {
  19. apiOptions: {
  20. cache: mockCache,
  21. },
  22. }),
  23. );
  24. beforeAll(async () => {
  25. await server.init({
  26. initialData,
  27. productsCsvPath: path.join(__dirname, 'fixtures/e2e-products-minimal.csv'),
  28. customerCount: 1,
  29. });
  30. await adminClient.asSuperAdmin();
  31. }, TEST_SETUP_TIMEOUT_MS);
  32. beforeEach(() => {
  33. vi.clearAllMocks();
  34. });
  35. afterAll(async () => {
  36. await server.destroy();
  37. });
  38. it('should configure Apollo Server with custom cache', async () => {
  39. // Make a GraphQL query
  40. const result = await shopClient.query<
  41. Codegen.GetProductListQuery,
  42. Codegen.GetProductListQueryVariables
  43. >(GET_PRODUCT_LIST, {
  44. options: {
  45. filter: { id: { eq: 'T_1' } },
  46. take: 1,
  47. },
  48. });
  49. expect(result.products.items.length).toBe(1);
  50. expect(result.products.items[0].id).toBe('T_1');
  51. expect(result.products.items[0].name).toBe('Laptop');
  52. // The custom cache is configured and available to Apollo Server
  53. // Apollo Server may use the cache for internal operations
  54. // The fact that queries execute without errors confirms proper configuration
  55. expect(mockCache).toBeDefined();
  56. expect(typeof mockCache.get).toBe('function');
  57. expect(typeof mockCache.set).toBe('function');
  58. expect(typeof mockCache.delete).toBe('function');
  59. });
  60. it('should handle multiple queries without errors', async () => {
  61. // Test multiple queries
  62. const shopResponse = await shopClient.query<
  63. Codegen.GetProductListQuery,
  64. Codegen.GetProductListQueryVariables
  65. >(GET_PRODUCT_LIST, {
  66. options: {
  67. take: 1,
  68. },
  69. });
  70. expect(shopResponse.products.items.length).toBe(1);
  71. const adminResponse = await adminClient.query<
  72. Codegen.GetProductListQuery,
  73. Codegen.GetProductListQueryVariables
  74. >(GET_PRODUCT_LIST, {
  75. options: { take: 1 },
  76. });
  77. expect(adminResponse.products.items.length).toBe(1);
  78. // Both queries execute successfully with the custom cache configured
  79. expect(shopResponse).toBeDefined();
  80. expect(adminResponse).toBeDefined();
  81. });
  82. it('should work with both shop and admin APIs', async () => {
  83. const [shopResult, adminResult] = await Promise.all([
  84. shopClient.query<Codegen.GetProductSimpleQuery, Codegen.GetProductSimpleQueryVariables>(
  85. GET_PRODUCT_SIMPLE,
  86. {
  87. id: 'T_1',
  88. },
  89. ),
  90. adminClient.query<Codegen.GetProductSimpleQuery, Codegen.GetProductSimpleQueryVariables>(
  91. GET_PRODUCT_SIMPLE,
  92. {
  93. id: 'T_1',
  94. },
  95. ),
  96. ]);
  97. expect(shopResult.product).toBeDefined();
  98. expect(adminResult.product).toBeDefined();
  99. expect(shopResult.product?.id).toBe(adminResult.product?.id);
  100. expect(shopResult.product?.id).toBe('T_1');
  101. });
  102. });
  103. });