Skip to content

Commit 9e4d2df

Browse files
fix(dashboard): avoid unnecessary product relations to be returned by default (medusajs#14175)
## Summary **What** — What changes are introduced in this PR? Improve Admin UI product detail performance after updating the entity. **Why** — Why are these changes relevant or necessary? Products with a lot of relations would cause issues when trying to be updated through various edit components, since the core update api route returns these entities by default. **How** — How have these changes been implemented? We remove these unnecessary relations when calling the update route, by passing the `fields` query param with the negation sign for each of these. For example: `fields=-type,-collection...` **Testing** — How have these changes been tested, or how can the reviewer test the feature? Tested all of the update components and validated they still work correctly, plus, none depend on the returned product to perform an action or render information. --- ## Examples Provide examples or code snippets that demonstrate how this feature works, or how it can be used in practice. This helps with documentation and ensures maintainers can quickly understand and verify the change. ```ts // Example usage ``` --- ## Checklist Please ensure the following before requesting a review: - [x] I have added a **changeset** for this PR - Every non-breaking change should be marked as a **patch** - To add a changeset, run `yarn changeset` and follow the prompts - [ ] The changes are covered by relevant **tests** - [x] I have verified the code works as intended locally - [x] I have linked the related issue(s) if applicable --- ## Additional Context Add any additional context, related issues, or references that might help the reviewer understand this PR. fixes medusajs#13783, medusajs#14183 closes CORE-1296, SUP-2791 Co-authored-by: Adrien de Peretti <[email protected]>
1 parent 842c0f5 commit 9e4d2df

File tree

23 files changed

+90
-36
lines changed

23 files changed

+90
-36
lines changed

.changeset/tall-cycles-allow.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@medusajs/dashboard": patch
3+
---
4+
5+
fix(dashboard): avoid unnecessary product relations to be returned by default

packages/admin/dashboard/src/components/search/use-search-results.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,9 @@ const useDynamicSearchResults = (
117117
{
118118
q: debouncedSearch,
119119
limit,
120-
fields: "id,title,thumbnail",
120+
// TODO: Remove exclusion once we avoid including unnecessary relations by default in the query config
121+
fields:
122+
"id,title,thumbnail,-type,-collection,-options,-tags,-images,-variants,-sales_channels",
121123
},
122124
{
123125
enabled: isAreaEnabled(currentArea, "product"),

packages/admin/dashboard/src/hooks/api/products.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,11 @@ export const useUpdateProduct = (
340340
>
341341
) => {
342342
return useMutation({
343-
mutationFn: (payload) => sdk.admin.product.update(id, payload),
343+
mutationFn: (payload) =>
344+
sdk.admin.product.update(id, payload, {
345+
fields:
346+
"-type,-collection,-options,-tags,-images,-variants,-sales_channels",
347+
}),
344348
onSuccess: async (data, variables, context) => {
345349
await queryClient.invalidateQueries({
346350
queryKey: productsQueryKeys.lists(),

packages/admin/dashboard/src/hooks/table/query/use-product-table-query.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ type UseProductTableQueryProps = {
77
}
88

99
const DEFAULT_FIELDS =
10-
"id,title,handle,status,*collection,*sales_channels,variants.id,thumbnail"
10+
// TODO: Remove exclusion once we avoid including unnecessary relations by default in the query config
11+
"id,title,handle,status,*collection,*sales_channels,variants.id,thumbnail,-type,-options,-tags,-images,-variants"
1112

1213
export const useProductTableQuery = ({
1314
prefix,

packages/admin/dashboard/src/routes/price-lists/price-list-create/components/price-list-create-form/price-list-prices-form.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ export const PriceListPricesForm = ({
3535
const { products, isLoading, isError, error } = useProducts({
3636
id: ids.map((id) => id.id),
3737
limit: ids.length,
38-
fields: "title,thumbnail,*variants",
38+
// TODO: Remove exclusion once we avoid including unnecessary relations by default in the query config
39+
fields:
40+
"title,thumbnail,*variants,-type,-collection,-options,-tags,-images,-sales_channels",
3941
})
4042

4143
const { setCloseOnEscape } = useRouteModal()

packages/admin/dashboard/src/routes/price-lists/price-list-prices-add/components/price-list-prices-add-form/price-list-prices-add-prices-form.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ export const PriceListPricesAddPricesForm = ({
3636
const { products, isLoading, isError, error } = useProducts({
3737
id: ids.map((id) => id.id),
3838
limit: ids.length,
39-
fields: "title,thumbnail,*variants",
39+
// TODO: Remove exclusion once we avoid including unnecessary relations by default in the query config
40+
fields:
41+
"title,thumbnail,*variants,-type,-collection,-options,-tags,-images,-sales_channels",
4042
})
4143

4244
const { setValue } = form

packages/admin/dashboard/src/routes/price-lists/price-list-prices-edit/price-list-prices-edit.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ export const PriceListPricesEdit = () => {
2222
id: productIds,
2323
limit: productIds?.length || 9999, // Temporary until we support lazy loading in the DataGrid
2424
price_list_id: [id!],
25-
fields: "title,thumbnail,*variants",
25+
// TODO: Remove exclusion once we avoid including unnecessary relations by default in the query config
26+
fields:
27+
"title,thumbnail,*variants,-type,-collection,-options,-tags,-images,-sales_channels",
2628
})
2729

2830
const { isReady, regions, currencies, pricePreferences } =

packages/admin/dashboard/src/routes/product-variants/product-variant-edit/product-variant-edit.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ export const ProductVariantEdit = () => {
3333
} = useProduct(
3434
variant?.product_id!,
3535
{
36-
fields: "-variants",
36+
// TODO: Remove exclusion once we avoid including unnecessary relations by default in the query config
37+
fields: "-type,-collection,-tags,-images,-variants,-sales_channels",
3738
},
3839
{
3940
enabled: !!variant?.product_id,

packages/admin/dashboard/src/routes/products/product-attributes/product-attributes.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,16 @@ import { useParams } from "react-router-dom"
44

55
import { RouteDrawer } from "../../../components/modals"
66
import { useProduct } from "../../../hooks/api/products"
7-
import { PRODUCT_DETAIL_FIELDS } from "../product-detail/constants"
87
import { ProductAttributesForm } from "./components/product-attributes-form"
98

109
export const ProductAttributes = () => {
1110
const { id } = useParams()
1211
const { t } = useTranslation()
1312

1413
const { product, isLoading, isError, error } = useProduct(id!, {
15-
fields: PRODUCT_DETAIL_FIELDS,
14+
// TODO: Remove exclusion once we avoid including unnecessary relations by default in the query config
15+
fields:
16+
"-type,-collection,-options,-tags,-images,-variants,-sales_channels",
1617
})
1718

1819
if (isError) {

packages/admin/dashboard/src/routes/products/product-create-option/product-create-option.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ export const ProductCreateOption = () => {
99
const { id } = useParams()
1010
const { t } = useTranslation()
1111

12-
const { product, isLoading, isError, error } = useProduct(id!)
12+
const { product, isLoading, isError, error } = useProduct(id!, {
13+
// TODO: Remove exclusion once we avoid including unnecessary relations by default in the query config
14+
fields:
15+
"-type,-collection,-options,-tags,-images,-variants,-sales_channels",
16+
})
1317

1418
if (isError) {
1519
throw error

0 commit comments

Comments
 (0)