|
| 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: '3' } } |
| 97 | + ], |
| 98 | + }, |
| 99 | + }, |
| 100 | + }); |
| 101 | + |
| 102 | + const lastQuery = capturedQueries.slice().reverse().find( |
| 103 | + q => q.includes('WHERE') && q.includes('testManyToMany') && !/SELECT\s+COUNT/i.test(q), |
| 104 | + ); |
| 105 | + expect(lastQuery, 'Should have a query with WHERE and testManyToMany').toBeDefined(); |
| 106 | + if (lastQuery) { |
| 107 | + const existsCount = (lastQuery.match(/EXISTS/g) || []).length; |
| 108 | + expect(existsCount).toBe(2); |
| 109 | + // Verify no JOIN was added for the filter |
| 110 | + expect(lastQuery).not.toContain('LEFT JOIN'); |
| 111 | + } |
| 112 | + |
| 113 | + // Verify that the query executes and returns a valid paginated result |
| 114 | + // Even if empty, totalItems should be a number. |
| 115 | + const { products } = await adminClient.query(GET_PRODUCTS, { |
| 116 | + options: { filter: { testManyToManyId: { eq: '1' } } } |
| 117 | + }); |
| 118 | + expect(products.totalItems).toBeDefined(); |
| 119 | + expect(typeof products.totalItems).toBe('number'); |
| 120 | + }); |
| 121 | + |
| 122 | + it('uses EXISTS for ManyToOne custom field relation when filtering (optimized)', async () => { |
| 123 | + const GET_PRODUCTS = gql` |
| 124 | + query GetProducts($options: ProductListOptions) { |
| 125 | + products(options: $options) { |
| 126 | + items { |
| 127 | + id |
| 128 | + } |
| 129 | + totalItems |
| 130 | + } |
| 131 | + } |
| 132 | + `; |
| 133 | + |
| 134 | + capturedQueries = []; |
| 135 | + |
| 136 | + await adminClient.query(GET_PRODUCTS, { |
| 137 | + options: { |
| 138 | + filter: { |
| 139 | + testManyToOneId: { eq: '1' }, |
| 140 | + }, |
| 141 | + }, |
| 142 | + }); |
| 143 | + |
| 144 | + const lastQuery = capturedQueries.slice().reverse().find( |
| 145 | + q => q.includes('WHERE') && q.includes('testManyToOne') && !/SELECT\s+COUNT/i.test(q), |
| 146 | + ); |
| 147 | + expect(lastQuery, 'Should have a query with WHERE and testManyToOne').toBeDefined(); |
| 148 | + if (lastQuery) { |
| 149 | + const existsCount = (lastQuery.match(/EXISTS/g) || []).length; |
| 150 | + expect(existsCount).toBe(1); |
| 151 | + // Verify no JOIN was added for the ManyToOne filter (optimization) |
| 152 | + expect(lastQuery).not.toContain('LEFT JOIN'); |
| 153 | + } |
| 154 | + }); |
| 155 | + |
| 156 | + it('uses JOIN for ManyToOne custom field relation when sorting', async () => { |
| 157 | + const GET_PRODUCTS = gql` |
| 158 | + query GetProducts($options: ProductListOptions) { |
| 159 | + products(options: $options) { |
| 160 | + items { |
| 161 | + id |
| 162 | + } |
| 163 | + } |
| 164 | + } |
| 165 | + `; |
| 166 | + |
| 167 | + capturedQueries = []; |
| 168 | + |
| 169 | + await adminClient.query(GET_PRODUCTS, { |
| 170 | + options: { |
| 171 | + sort: { |
| 172 | + testManyToOneId: 'ASC', |
| 173 | + }, |
| 174 | + }, |
| 175 | + }); |
| 176 | + |
| 177 | + const lastQuery = capturedQueries.slice().reverse().find( |
| 178 | + q => q.includes('testManyToOne') && !/SELECT\s+COUNT/i.test(q), |
| 179 | + ); |
| 180 | + expect(lastQuery, 'Should have a query with testManyToOne').toBeDefined(); |
| 181 | + if (lastQuery) { |
| 182 | + // Verify JOIN was added for sorting |
| 183 | + expect(lastQuery).toContain('LEFT JOIN'); |
| 184 | + // EXISTS is not used for sorting |
| 185 | + const existsCount = (lastQuery.match(/EXISTS/g) || []).length; |
| 186 | + expect(existsCount).toBe(0); |
| 187 | + } |
| 188 | + }); |
| 189 | +}); |
0 commit comments