diff --git a/.changeset/tough-horses-return.md b/.changeset/tough-horses-return.md new file mode 100644 index 0000000000..425c668a74 --- /dev/null +++ b/.changeset/tough-horses-return.md @@ -0,0 +1,6 @@ +--- +"@medusajs/test-utils": patch +"@medusajs/index": patch +--- + +fix(index): merge filterable fields with default fields diff --git a/integration-tests/modules/__tests__/index/query-index.spec.ts b/integration-tests/modules/__tests__/index/query-index.spec.ts index 32335f429f..a55d9db35d 100644 --- a/integration-tests/modules/__tests__/index/query-index.spec.ts +++ b/integration-tests/modules/__tests__/index/query-index.spec.ts @@ -1,6 +1,12 @@ +import CustomerModule from "@medusajs/customer" +import ProductModule from "@medusajs/product" import { medusaIntegrationTestRunner } from "@medusajs/test-utils" import { RemoteQueryFunction } from "@medusajs/types" -import { ContainerRegistrationKeys, defaultCurrencies } from "@medusajs/utils" +import { + ContainerRegistrationKeys, + defaultCurrencies, + defineLink, +} from "@medusajs/utils" import { setTimeout } from "timers/promises" import { adminHeaders, @@ -26,6 +32,7 @@ async function populateData(api: any) { title: "Test Product", status: "published", description: "test-product-description", + origin_country: "USA", shipping_profile_id: shippingProfile.id, options: [{ title: "Denominations", values: ["100"] }], variants: [ @@ -82,6 +89,17 @@ async function populateData(api: any) { process.env.ENABLE_INDEX_MODULE = "true" medusaIntegrationTestRunner({ + hooks: { + beforeServerStart: async () => { + const customer = CustomerModule.linkable.customer + const product = ProductModule.linkable.product + + defineLink(customer, { + linkable: product, + filterable: ["origin_country"], + }) + }, + }, testSuite: ({ getContainer, dbConnection, api, dbConfig }) => { let appContainer @@ -417,6 +435,33 @@ medusaIntegrationTestRunner({ expect(resultset.data.length).toEqual(2) }) + + it("should query by custom linkable field and default field using query.index", async () => { + await populateData(api) + + const query = appContainer.resolve( + ContainerRegistrationKeys.QUERY + ) as RemoteQueryFunction + + const resultset = await fetchAndRetry( + async () => + await query.index({ + entity: "product", + fields: ["id", "origin_country"], + filters: { + origin_country: ["USA"], + }, + }), + ({ data }) => data.length > 0, + { + retries: 3, + waitSeconds: 3, + } + ) + + expect(resultset.data.length).toEqual(1) + expect(resultset.data[0].origin_country).toEqual("USA") + }) }) }, }) diff --git a/packages/medusa-test-utils/src/medusa-test-runner.ts b/packages/medusa-test-utils/src/medusa-test-runner.ts index abfd6cbc04..cc66edcca9 100644 --- a/packages/medusa-test-utils/src/medusa-test-runner.ts +++ b/packages/medusa-test-utils/src/medusa-test-runner.ts @@ -1,3 +1,4 @@ +import { logger } from "@medusajs/framework/logger" import { MedusaAppOutput } from "@medusajs/framework/modules-sdk" import { MedusaContainer } from "@medusajs/framework/types" import { @@ -7,7 +8,6 @@ import { mergePluginModules, } from "@medusajs/framework/utils" import { asValue } from "awilix" -import { logger } from "@medusajs/framework/logger" import { dbTestUtilFactory, getDatabaseURL } from "./database" import { applyEnvVarsToProcess, @@ -48,6 +48,9 @@ interface TestRunnerConfig { schema?: string debug?: boolean inApp?: boolean + hooks?: { + beforeServerStart?: (container: MedusaContainer) => Promise + } } class MedusaTestRunner { @@ -72,6 +75,7 @@ class MedusaTestRunner { private loadedApplication: any = null private shutdown: () => Promise = async () => void 0 private isFirstTime = true + private hooks: TestRunnerConfig["hooks"] = {} constructor(config: TestRunnerConfig) { const tempName = parseInt(process.env.JEST_WORKER_ID || "1") @@ -93,6 +97,7 @@ class MedusaTestRunner { schema: this.schema, debug: this.debug, } + this.hooks = config.hooks ?? {} this.setupProcessHandlers() } @@ -158,6 +163,10 @@ class MedusaTestRunner { [ContainerRegistrationKeys.LOGGER]: asValue(logger), }) + if (this.hooks?.beforeServerStart) { + await this.hooks.beforeServerStart(container) + } + await this.initializeDatabase() logger.info( @@ -309,6 +318,7 @@ export function medusaIntegrationTestRunner({ debug = false, inApp = false, testSuite, + hooks, }: { moduleName?: string env?: Record @@ -318,6 +328,7 @@ export function medusaIntegrationTestRunner({ debug?: boolean inApp?: boolean testSuite: (options: MedusaSuiteOptions) => void + hooks?: TestRunnerConfig["hooks"] }) { const runner = new MedusaTestRunner({ moduleName, @@ -327,6 +338,7 @@ export function medusaIntegrationTestRunner({ env, debug, inApp, + hooks, }) return describe("", () => { diff --git a/packages/modules/index/src/utils/build-config.ts b/packages/modules/index/src/utils/build-config.ts index dd924210bd..a761177406 100644 --- a/packages/modules/index/src/utils/build-config.ts +++ b/packages/modules/index/src/utils/build-config.ts @@ -1206,7 +1206,7 @@ function buildSchemaFromFilterableLinks( }) .join("\n") - return `type ${entity} ${events} { + return `extend type ${entity} ${events} { ${fieldDefinitions} }` })