[Bug]: query.graph prices only the first 4,000 price sets; the rest silently come back without calculated_price
Package.json file
{
"dependencies": {
"@medusajs/admin-sdk": "2.19.0",
"@medusajs/cli": "2.19.0",
"@medusajs/framework": "2.19.0",
"@medusajs/medusa": "2.19.0"
}
}(Resolved: @medusajs/query 2.19.0, @medusajs/pricing 2.19.0. The same code is unchanged on develop at 08f73ec, after v2.21.1 — permalinks below.)
Node.js version
v22.22.2
Database and its version
PostgreSQL 17.11
Operating system name and version
Ubuntu 24.04.2 LTS
Browser name
n/a (server-side)
What happended?
A single query.graph that resolves calculated_price for 4,000 or more price sets returns prices for exactly the first 4,000. Every price set after that comes back with no calculated_price at all. There is no error and no warning, so the result looks like a catalogue where most variants have no price.
It affects every store route that prices a list, since none of them cap limit: GET /store/products?limit=5000&fields=*variants.calculated_price®ion_id=… returns 4,910 products whose 8,935 variants include only 4,000 with a calculated_price. GET /store/product-variants?limit=5000&fields=*calculated_price returns 5,000 variants and prices 4,000 of them. Any custom code that sorts or filters by price over a query.graph result gets wrong answers. In our case a price-band filter matched 560 products when a direct SQL count gives 2,344.
Cause. Two pieces of code that are each fine alone:
- The data fetcher splits a relation fetch of
>= MAX_BATCH_SIZEids into batches. It copiesfiltersfor each batch but gives every batch the sameoptionsobject, and with it the samerelationsarray:module-data-fetcher.tsL14:const MAX_BATCH_SIZE = 4000- L267:
if (isIdsArray && idsLength >= MAX_BATCH_SIZE && !hasPagination)→fetchRemoteDataBatched - L109:
const batchFilters = { ...filters, [keyField]: batch }, while L115 / L121 pass the sharedoptions - 2.19.0 dist:
@medusajs/query/dist/query/module-data-fetcher.jsL7, L57, L64, L184
PricingModuleService.setupCalculatedPriceConfig_removes the virtualcalculated_pricerelation by splicing it out of that array in place.normalizePriceSetConfigmakes only a shallow copy ({ options: {…}, ...config }), sorelationsis still the caller's array:pricing-module.tsL170:config.relations?.splice(fieldIdx, 1), called fromlistPriceSets(L253) andlistAndCountPriceSets(L294)normalizePriceSetConfigL1797-1806- 2.19.0 dist:
@medusajs/pricing/dist/services/pricing-module.jsL56 (splice), L881-888 (shallow copy)
The first batch splices calculated_price out of the shared array. Every later batch then sees no calculated_price relation, so setupCalculatedPriceConfig_ returns undefined and the batch is returned unpriced. The pricing maths is fine: pricing.calculatePrices({ id: <all 9,386 price sets> }, { context: { currency_code: "myr" } }) on the same database prices 9,384 of them. The other 2 have no MYR price.
Expected behavior
Every price set in the fetch gets a calculated_price, whatever the batch size. A price set with no price for the context gets calculated_price: null, the same as it does below 4,000.
Actual behavior
Only the first 4,000 are priced. The rest have no calculated_price key, and on the variant this surfaces as a missing or null calculated_price.
Measured on a fresh 2.19.0 database with 5,300 synthetic products, 9,383 variants and one MYR price each:
| query | variants | priced |
|---|---|---|
one query.graph({ entity: "variant", fields: ["id", "calculated_price.calculated_amount"], context }) |
9,383 | 4,000 |
| the same, split into chunks of 1,000 ids | 9,383 | 9,383 |
GET /store/products?limit=5000&fields=id,*variants.calculated_price |
8,935 | 4,000 |
GET /store/product-variants?limit=5000&fields=id,*calculated_price |
5,000 | 4,000 |
At 9,314 products / 16,992 variants the same query priced exactly 4,000, 12,992 went unpriced, and 7,059 products had no price on any variant.
Link to reproduction repo
Our project repo is private, so the reproduction is inline below. It is one medusa exec script, run in a fresh create-medusa-app project (2.19.0) after seeding more than 4,000 variants with a price in one currency. The count is always 4,000, however many variants there are:
// src/scripts/repro-price-batch.ts
// npx medusa exec ./src/scripts/repro-price-batch.ts <currency_code>
import { ExecArgs } from "@medusajs/framework/types"
import { ContainerRegistrationKeys, QueryContext } from "@medusajs/framework/utils"
export default async function repro({ container, args }: ExecArgs) {
const query = container.resolve(ContainerRegistrationKeys.QUERY)
const currency_code = args?.[0] ?? "usd"
const { data } = await query.graph({
entity: "variant",
fields: ["id", "calculated_price.calculated_amount"],
context: { calculated_price: QueryContext({ currency_code }) },
})
const priced = data.filter((v) => v.calculated_price?.calculated_amount != null)
console.log({ variants: data.length, priced: priced.length }) // priced === 4000 for any count >= 4000
}Suggested fix. Either of these is enough, and doing both is safest:
normalizePriceSetConfig: copy the array, e.g.relations: config?.relations ? [...config.relations] : undefined, sosetupCalculatedPriceConfig_never mutates the caller's options. The same mutation would bite anyone who passes oneconfigtolistPriceSetstwice.fetchRemoteDataBatched: give each batch its own copy ofoptions, or at least ofoptions.relations, the same wayfiltersis already copied.
We now log whenever this happens in our project and chunk our own pricing queries. A fix upstream would let us remove both workarounds.
Source: medusajs/medusa