From 19c5d5ba36342de14b1735fd65ac937b674ec1ed Mon Sep 17 00:00:00 2001 From: Adrien de Peretti Date: Thu, 21 Sep 2023 17:29:49 +0200 Subject: [PATCH] feat(medusa): Migrate remote query usage in store product domain to use an object (#5131) * feat(medusa): Migrate remote query usage in store product domain to use an object * fix profile level --- .../api/routes/store/products/get-product.ts | 117 ++--------------- .../src/api/routes/store/products/index.ts | 89 +++++++++++++ .../routes/store/products/list-products.ts | 124 ++---------------- 3 files changed, 105 insertions(+), 225 deletions(-) diff --git a/packages/medusa/src/api/routes/store/products/get-product.ts b/packages/medusa/src/api/routes/store/products/get-product.ts index e833ba784d..35a0b10763 100644 --- a/packages/medusa/src/api/routes/store/products/get-product.ts +++ b/packages/medusa/src/api/routes/store/products/get-product.ts @@ -10,7 +10,7 @@ import { IsOptional, IsString } from "class-validator" import { PriceSelectionParams } from "../../../../types/price-selection" import { cleanResponseData } from "../../../../utils" import IsolateProductDomain from "../../../../loaders/feature-flags/isolate-product-domain" -import { defaultStoreProductsFields } from "./index" +import { defaultStoreRemoteQueryFields } from "./index" /** * @oas [get] /store/products/{id} @@ -166,116 +166,15 @@ async function getProductWithIsolatedProductModule(req, id: string) { const remoteQuery = req.scope.resolve("remoteQuery") const variables = { id } - const commonProperties = [] - const query = ` - query ($id: String!) { - product (id: $id) { - ${defaultStoreProductsFields.join("\n")} - - images { - id - created_at - updated_at - deleted_at - url - metadata - } - - tags { - id - created_at - updated_at - deleted_at - value - } - - type { - id - created_at - updated_at - deleted_at - value - } - - collection { - title - handle - id - created_at - updated_at - deleted_at - } - - options { - id - created_at - updated_at - deleted_at - title - product_id - metadata - values { - id - created_at - updated_at - deleted_at - value - option_id - variant_id - metadata - } - } - - variants { - id - created_at - updated_at - deleted_at - title - product_id - sku - barcode - ean - upc - variant_rank - inventory_quantity - allow_backorder - manage_inventory - hs_code - origin_country - mid_code - material - weight - length - height - width - metadata - options { - id - created_at - updated_at - deleted_at - value - option_id - variant_id - metadata - } - } - - profile { - id - created_at - updated_at - deleted_at - name - type - } - } - } - ` + const query = { + product: { + __args: variables, + ...defaultStoreRemoteQueryFields, + }, + } - const [product] = await remoteQuery(query, variables) + const [product] = await remoteQuery(query) product.profile_id = product.profile?.id diff --git a/packages/medusa/src/api/routes/store/products/index.ts b/packages/medusa/src/api/routes/store/products/index.ts index d2d8ed1dde..aa61cdd122 100644 --- a/packages/medusa/src/api/routes/store/products/index.ts +++ b/packages/medusa/src/api/routes/store/products/index.ts @@ -111,6 +111,95 @@ export const allowedStoreProductsRelations = [ "sales_channels", ] +/** + * This is temporary. + */ +export const defaultStoreRemoteQueryFields = { + fields: defaultStoreProductsFields, + images: { + fields: ["id", "created_at", "updated_at", "deleted_at", "url", "metadata"], + }, + tags: { + fields: ["id", "created_at", "updated_at", "deleted_at", "value"], + }, + + type: { + fields: ["id", "created_at", "updated_at", "deleted_at", "value"], + }, + + collection: { + fields: ["title", "handle", "id", "created_at", "updated_at", "deleted_at"], + }, + + options: { + fields: [ + "id", + "created_at", + "updated_at", + "deleted_at", + "title", + "product_id", + "metadata", + ], + values: { + fields: [ + "id", + "created_at", + "updated_at", + "deleted_at", + "value", + "option_id", + "variant_id", + "metadata", + ], + }, + }, + + variants: { + fields: [ + "id", + "created_at", + "updated_at", + "deleted_at", + "title", + "product_id", + "sku", + "barcode", + "ean", + "upc", + "variant_rank", + "inventory_quantity", + "allow_backorder", + "manage_inventory", + "hs_code", + "origin_country", + "mid_code", + "material", + "weight", + "length", + "height", + "width", + "metadata", + ], + + options: { + fields: [ + "id", + "created_at", + "updated_at", + "deleted_at", + "value", + "option_id", + "variant_id", + "metadata", + ], + }, + }, + profile: { + fields: ["id", "created_at", "updated_at", "deleted_at", "name", "type"], + }, +} + export * from "./list-products" export * from "./search" diff --git a/packages/medusa/src/api/routes/store/products/list-products.ts b/packages/medusa/src/api/routes/store/products/list-products.ts index fe5dbbd245..87b76d7fbf 100644 --- a/packages/medusa/src/api/routes/store/products/list-products.ts +++ b/packages/medusa/src/api/routes/store/products/list-products.ts @@ -24,7 +24,7 @@ import { cleanResponseData } from "../../../../utils/clean-response-data" import { defaultStoreCategoryScope } from "../product-categories" import { optionalBooleanMapper } from "../../../../utils/validators/is-boolean" import IsolateProductDomain from "../../../../loaders/feature-flags/isolate-product-domain" -import { defaultStoreProductsFields } from "./index" +import { defaultStoreRemoteQueryFields } from "./index" /** * @oas [get] /store/products @@ -394,125 +394,17 @@ async function listAndCountProductWithIsolatedProductModule( take: listConfig.take, } - // prettier-ignore - const args = ` - filters: $filters, - order: $order, - skip: $skip, - take: $take - ` - - const query = ` - query ($filters: any, $order: any, $skip: Int, $take: Int) { - product (${args}) { - ${defaultStoreProductsFields.join("\n")} - - images { - id - created_at - updated_at - deleted_at - url - metadata - } - - tags { - id - created_at - updated_at - deleted_at - value - } - - type { - id - created_at - updated_at - deleted_at - value - } - - collection { - title - handle - id - created_at - updated_at - deleted_at - } - - options { - id - created_at - updated_at - deleted_at - title - product_id - metadata - values { - id - created_at - updated_at - deleted_at - value - option_id - variant_id - metadata - } - } - - variants { - id - created_at - updated_at - deleted_at - title - product_id - sku - barcode - ean - upc - variant_rank - inventory_quantity - allow_backorder - manage_inventory - hs_code - origin_country - mid_code - material - weight - length - height - width - metadata - options { - id - created_at - updated_at - deleted_at - value - option_id - variant_id - metadata - } - } - - profile { - id - created_at - updated_at - deleted_at - name - type - } - } - } - ` + const query = { + product: { + __args: variables, + ...defaultStoreRemoteQueryFields, + }, + } const { rows: products, metadata: { count }, - } = await remoteQuery(query, variables) + } = await remoteQuery(query) products.forEach((product) => { product.profile_id = product.profile?.id