|
| 1 | +/* eslint-disable no-console */ |
| 2 | +import { ID, FacetValue, VendureConfig } from '@vendure/core'; |
| 3 | +import { createTestEnvironment } from '@vendure/testing'; |
| 4 | +import { gql } from 'graphql-tag'; |
| 5 | +import path from 'path'; |
| 6 | +import { afterAll, beforeAll, describe, expect, it } from 'vitest'; |
| 7 | + |
| 8 | +import { initialData } from '../e2e-initial-data'; |
| 9 | +import { testConfig } from '../test-config'; |
| 10 | + |
| 11 | +describe('ListQueryBuilder Optimization Benchmark', () => { |
| 12 | + let capturedQueries: string[] = []; |
| 13 | + const baseConfig = testConfig(); |
| 14 | + |
| 15 | + const benchmarkConfig: VendureConfig = { |
| 16 | + ...baseConfig, |
| 17 | + customFields: { |
| 18 | + Product: [ |
| 19 | + { |
| 20 | + name: 'testManyToMany', |
| 21 | + type: 'relation', |
| 22 | + entity: FacetValue, |
| 23 | + graphQLType: 'FacetValue', |
| 24 | + list: true, |
| 25 | + }, |
| 26 | + { |
| 27 | + name: 'testManyToOne', |
| 28 | + type: 'relation', |
| 29 | + entity: FacetValue, |
| 30 | + graphQLType: 'FacetValue', |
| 31 | + list: false, |
| 32 | + }, |
| 33 | + ], |
| 34 | + }, |
| 35 | + dbConnectionOptions: { |
| 36 | + ...baseConfig.dbConnectionOptions, |
| 37 | + logging: ['query'], |
| 38 | + logger: { |
| 39 | + logQuery(query: string) { |
| 40 | + if ( |
| 41 | + query.includes('SELECT') && |
| 42 | + (query.includes('"product"') || query.includes('`product`')) |
| 43 | + ) { |
| 44 | + capturedQueries.push(query); |
| 45 | + } |
| 46 | + }, |
| 47 | + logQueryError: (error: string) => console.error(error), |
| 48 | + logQuerySlow: (time: number, query: string) => console.warn(query, time), |
| 49 | + logSchemaBuild: () => { |
| 50 | + /* no-op */ |
| 51 | + }, |
| 52 | + logMigration: () => { |
| 53 | + /* no-op */ |
| 54 | + }, |
| 55 | + log: () => { |
| 56 | + /* no-op */ |
| 57 | + }, |
| 58 | + } as any, |
| 59 | + }, |
| 60 | + }; |
| 61 | + |
| 62 | + const { server, adminClient } = createTestEnvironment(benchmarkConfig); |
| 63 | + |
| 64 | + beforeAll(async () => { |
| 65 | + await server.init({ |
| 66 | + initialData, |
| 67 | + productsCsvPath: path.join(__dirname, '../../packages/core/e2e/fixtures/e2e-products-minimal.csv'), |
| 68 | + customerCount: 1, |
| 69 | + }); |
| 70 | + await adminClient.asSuperAdmin(); |
| 71 | + }, 240000); |
| 72 | + |
| 73 | + afterAll(async () => { |
| 74 | + await server.destroy(); |
| 75 | + }); |
| 76 | + |
| 77 | + it('uses multiple EXISTS for ManyToMany custom field relation AND filter', async () => { |
| 78 | + const GET_PRODUCTS = gql` |
| 79 | + query GetProducts($options: ProductListOptions) { |
| 80 | + products(options: $options) { |
| 81 | + items { |
| 82 | + id |
| 83 | + } |
| 84 | + totalItems |
| 85 | + } |
| 86 | + } |
| 87 | + `; |
| 88 | + |
| 89 | + capturedQueries = []; |
| 90 | + |
| 91 | + await adminClient.query(GET_PRODUCTS, { |
| 92 | + options: { |
| 93 | + filter: { |
| 94 | + _and: [ |
| 95 | + { testManyToManyId: { eq: '1' } }, |
| 96 | + { testManyToManyId: { eq: '2' } } |
| 97 | + ], |
| 98 | + }, |
| 99 | + }, |
| 100 | + }); |
| 101 | + |
| 102 | + const lastQuery = capturedQueries.find(q => q.includes('WHERE') && q.includes('testManyToMany')); |
| 103 | + expect(lastQuery, 'Should have a query with WHERE and testManyToMany').toBeDefined(); |
| 104 | + if (lastQuery) { |
| 105 | + const existsCount = (lastQuery.match(/EXISTS/g) || []).length; |
| 106 | + expect(existsCount).toBe(2); |
| 107 | + // Verify no JOIN was added for the filter |
| 108 | + expect(lastQuery).not.toContain('LEFT JOIN'); |
| 109 | + } |
| 110 | + }); |
| 111 | + |
| 112 | + it('uses EXISTS for ManyToOne custom field relation when filtering (optimized)', async () => { |
| 113 | + const GET_PRODUCTS = gql` |
| 114 | + query GetProducts($options: ProductListOptions) { |
| 115 | + products(options: $options) { |
| 116 | + items { |
| 117 | + id |
| 118 | + } |
| 119 | + totalItems |
| 120 | + } |
| 121 | + } |
| 122 | + `; |
| 123 | + |
| 124 | + capturedQueries = []; |
| 125 | + |
| 126 | + await adminClient.query(GET_PRODUCTS, { |
| 127 | + options: { |
| 128 | + filter: { |
| 129 | + testManyToOneId: { eq: '1' }, |
| 130 | + }, |
| 131 | + }, |
| 132 | + }); |
| 133 | + |
| 134 | + const lastQuery = capturedQueries.find(q => q.includes('WHERE') && q.includes('testManyToOne')); |
| 135 | + expect(lastQuery, 'Should have a query with WHERE and testManyToOne').toBeDefined(); |
| 136 | + if (lastQuery) { |
| 137 | + const existsCount = (lastQuery.match(/EXISTS/g) || []).length; |
| 138 | + expect(existsCount).toBe(1); |
| 139 | + // Verify no JOIN was added for the ManyToOne filter (optimization) |
| 140 | + expect(lastQuery).not.toContain('LEFT JOIN'); |
| 141 | + } |
| 142 | + }); |
| 143 | + |
| 144 | + it('uses JOIN for ManyToOne custom field relation when sorting', async () => { |
| 145 | + const GET_PRODUCTS = gql` |
| 146 | + query GetProducts($options: ProductListOptions) { |
| 147 | + products(options: $options) { |
| 148 | + items { |
| 149 | + id |
| 150 | + } |
| 151 | + } |
| 152 | + } |
| 153 | + `; |
| 154 | + |
| 155 | + capturedQueries = []; |
| 156 | + |
| 157 | + await adminClient.query(GET_PRODUCTS, { |
| 158 | + options: { |
| 159 | + sort: { |
| 160 | + testManyToOneId: 'ASC', |
| 161 | + }, |
| 162 | + }, |
| 163 | + }); |
| 164 | + |
| 165 | + const lastQuery = capturedQueries.find(q => q.includes('testManyToOne')); |
| 166 | + expect(lastQuery, 'Should have a query with testManyToOne').toBeDefined(); |
| 167 | + if (lastQuery) { |
| 168 | + // Verify JOIN was added for sorting |
| 169 | + expect(lastQuery).toContain('LEFT JOIN'); |
| 170 | + // EXISTS is not used for sorting |
| 171 | + const existsCount = (lastQuery.match(/EXISTS/g) || []).length; |
| 172 | + expect(existsCount).toBe(0); |
| 173 | + } |
| 174 | + }); |
| 175 | +}); |
0 commit comments