-
Notifications
You must be signed in to change notification settings - Fork 1.4k
fix(core): Optimize collection variants N+1 and persist catalog filters #4636
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
RubenDarioGuerreroNeira
wants to merge
1
commit into
vendurehq:master
Choose a base branch
from
RubenDarioGuerreroNeira:fix/collection-variants-n-plus-1-v3
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,189 @@ | ||
| /* eslint-disable no-console */ | ||
| import { ID, FacetValue, VendureConfig } from '@vendure/core'; | ||
| import { createTestEnvironment } from '@vendure/testing'; | ||
| import { gql } from 'graphql-tag'; | ||
| import path from 'path'; | ||
| import { afterAll, beforeAll, describe, expect, it } from 'vitest'; | ||
|
|
||
| import { initialData } from '../e2e-initial-data'; | ||
| import { testConfig } from '../test-config'; | ||
|
|
||
| describe('ListQueryBuilder Optimization Benchmark', () => { | ||
| let capturedQueries: string[] = []; | ||
| const baseConfig = testConfig(); | ||
|
|
||
| const benchmarkConfig: VendureConfig = { | ||
| ...baseConfig, | ||
| customFields: { | ||
| Product: [ | ||
| { | ||
| name: 'testManyToMany', | ||
| type: 'relation', | ||
| entity: FacetValue, | ||
| graphQLType: 'FacetValue', | ||
| list: true, | ||
| }, | ||
| { | ||
| name: 'testManyToOne', | ||
| type: 'relation', | ||
| entity: FacetValue, | ||
| graphQLType: 'FacetValue', | ||
| list: false, | ||
| }, | ||
| ], | ||
| }, | ||
| dbConnectionOptions: { | ||
| ...baseConfig.dbConnectionOptions, | ||
| logging: ['query'], | ||
| logger: { | ||
| logQuery(query: string) { | ||
| if ( | ||
| query.includes('SELECT') && | ||
| (query.includes('"product"') || query.includes('`product`')) | ||
| ) { | ||
| capturedQueries.push(query); | ||
| } | ||
| }, | ||
| logQueryError: (error: string) => console.error(error), | ||
| logQuerySlow: (time: number, query: string) => console.warn(query, time), | ||
| logSchemaBuild: () => { | ||
| /* no-op */ | ||
| }, | ||
| logMigration: () => { | ||
| /* no-op */ | ||
| }, | ||
| log: () => { | ||
| /* no-op */ | ||
| }, | ||
| } as any, | ||
| }, | ||
| }; | ||
|
|
||
| const { server, adminClient } = createTestEnvironment(benchmarkConfig); | ||
|
|
||
| beforeAll(async () => { | ||
| await server.init({ | ||
| initialData, | ||
| productsCsvPath: path.join(__dirname, '../../packages/core/e2e/fixtures/e2e-products-minimal.csv'), | ||
| customerCount: 1, | ||
| }); | ||
| await adminClient.asSuperAdmin(); | ||
| }, 240000); | ||
|
|
||
| afterAll(async () => { | ||
| await server.destroy(); | ||
| }); | ||
|
|
||
| it('uses multiple EXISTS for ManyToMany custom field relation AND filter', async () => { | ||
| const GET_PRODUCTS = gql` | ||
| query GetProducts($options: ProductListOptions) { | ||
| products(options: $options) { | ||
| items { | ||
| id | ||
| } | ||
| totalItems | ||
| } | ||
| } | ||
| `; | ||
|
|
||
| capturedQueries = []; | ||
|
|
||
| await adminClient.query(GET_PRODUCTS, { | ||
| options: { | ||
| filter: { | ||
| _and: [ | ||
| { testManyToManyId: { eq: '1' } }, | ||
| { testManyToManyId: { eq: '3' } } | ||
| ], | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| const lastQuery = capturedQueries.slice().reverse().find( | ||
| q => q.includes('WHERE') && q.includes('testManyToMany') && !/SELECT\s+COUNT/i.test(q), | ||
| ); | ||
| expect(lastQuery, 'Should have a query with WHERE and testManyToMany').toBeDefined(); | ||
| if (lastQuery) { | ||
| const existsCount = (lastQuery.match(/EXISTS/g) || []).length; | ||
| expect(existsCount).toBe(2); | ||
| // Verify no JOIN was added for the filter | ||
| expect(lastQuery).not.toContain('LEFT JOIN'); | ||
| } | ||
|
|
||
| // Verify that the query executes and returns a valid paginated result | ||
| // Even if empty, totalItems should be a number. | ||
| const { products } = await adminClient.query(GET_PRODUCTS, { | ||
| options: { filter: { testManyToManyId: { eq: '1' } } } | ||
| }); | ||
| expect(products.totalItems).toBeDefined(); | ||
| expect(typeof products.totalItems).toBe('number'); | ||
| }); | ||
|
|
||
| it('uses EXISTS for ManyToOne custom field relation when filtering (optimized)', async () => { | ||
| const GET_PRODUCTS = gql` | ||
| query GetProducts($options: ProductListOptions) { | ||
| products(options: $options) { | ||
| items { | ||
| id | ||
| } | ||
| totalItems | ||
| } | ||
| } | ||
| `; | ||
|
|
||
| capturedQueries = []; | ||
|
|
||
| await adminClient.query(GET_PRODUCTS, { | ||
| options: { | ||
| filter: { | ||
| testManyToOneId: { eq: '1' }, | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| const lastQuery = capturedQueries.slice().reverse().find( | ||
| q => q.includes('WHERE') && q.includes('testManyToOne') && !/SELECT\s+COUNT/i.test(q), | ||
| ); | ||
| expect(lastQuery, 'Should have a query with WHERE and testManyToOne').toBeDefined(); | ||
| if (lastQuery) { | ||
| const existsCount = (lastQuery.match(/EXISTS/g) || []).length; | ||
| expect(existsCount).toBe(1); | ||
| // Verify no JOIN was added for the ManyToOne filter (optimization) | ||
| expect(lastQuery).not.toContain('LEFT JOIN'); | ||
| } | ||
| }); | ||
|
|
||
| it('uses JOIN for ManyToOne custom field relation when sorting', async () => { | ||
| const GET_PRODUCTS = gql` | ||
| query GetProducts($options: ProductListOptions) { | ||
| products(options: $options) { | ||
| items { | ||
| id | ||
| } | ||
| } | ||
| } | ||
| `; | ||
|
|
||
| capturedQueries = []; | ||
|
|
||
| await adminClient.query(GET_PRODUCTS, { | ||
| options: { | ||
| sort: { | ||
| testManyToOneId: 'ASC', | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| const lastQuery = capturedQueries.slice().reverse().find( | ||
| q => q.includes('testManyToOne') && !/SELECT\s+COUNT/i.test(q), | ||
| ); | ||
| expect(lastQuery, 'Should have a query with testManyToOne').toBeDefined(); | ||
| if (lastQuery) { | ||
| // Verify JOIN was added for sorting | ||
| expect(lastQuery).toContain('LEFT JOIN'); | ||
| // EXISTS is not used for sorting | ||
| const existsCount = (lastQuery.match(/EXISTS/g) || []).length; | ||
| expect(existsCount).toBe(0); | ||
| } | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.