feat: add cartLoader to batch User.cart resolution#697
Closed
pozylon wants to merge 1 commit into
Closed
Conversation
The cart query in core-orders used a plain findOne with no batching, so
resolving carts for multiple users in one request (e.g. admin `users { cart }`)
issued N queries.
- Add a batched `findCarts({ userIds })` query plus a shared `buildCartSelector`
helper in configureOrdersModule-queries (cart now reuses it).
- Add `cartLoader` keyed by `{ userId, countryCode, orderNumber }`, batching all
keys into one `findCarts` query and resolving each via most-recent-first match,
mirroring `modules.orders.cart` semantics.
- Use the loader in the `User.cart` resolver (covers me/user/users).
Mutations, MCP tools and core services keep the direct module call.
Member
Author
|
Closing — this change should target the v4.8.x branch, not master. Re-doing it on a branch based off v4.8.x. |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Problem
The
cartquery incore-orders(configureOrdersModule-queries.ts) used a plainfindOnewith no batching. Resolving thecartfield across multiple users in a single GraphQL request (e.g. adminusers { cart { ... } }) issued one DB query per user.Change
Adds a request-scoped DataLoader for carts, keyed by
{ userId, countryCode }(plusorderNumber), following the existing loader conventions inpackages/api/src/loaders.core-orders: extract a sharedbuildCartSelectorhelper and add a batchedfindCarts({ userIds, ... })query (userId: { $in }, sortedupdated: -1).cartnow reuses the helper (behavior unchanged — every caller already suppliescountryCode). The loader lives in the API layer and can't query MongoDB directly (layer boundary), andfindOrders/buildFindSelectoronly support a singleuserId, so a dedicated batched method was needed.cartLoader.ts(new): batches all keys into onefindCarts({ userIds })query, then resolves each key in JS by matchinguserId+countryCode+orderNumberagainst the most-recently-updated-first list — mirroringmodules.orders.cartsemantics.User.cartresolver: now callsloaders.cartLoader.load(...), coveringme { cart },user(userId) { cart }, andusers { cart }.Intentionally left on the direct
modules.orders.cartcallnextUserCart,migrateOrderCart) and platform/worker — no request-scoped loaders there.signPaymentProviderForCheckoutmutation — avoids loader-cache staleness in the write path.getUserCartMCP tool — single read, no batching benefit.Verification
npm run build(full typecheck) passes; ESLint clean on changed files.heartbeat,cart-products,cart-migrate,auth-admin(all selectme/cross-usercart), pluscart-checkout,cart-delivery-payment,cart-discounts,cart-quotations(exercise the refactoredcart/findCartspath via checkout services).