diff --git a/src/core-services/product/resolvers/Product/index.js b/src/core-services/product/resolvers/Product/index.js index 6645670bda7..85bc69b5de6 100644 --- a/src/core-services/product/resolvers/Product/index.js +++ b/src/core-services/product/resolvers/Product/index.js @@ -8,7 +8,7 @@ import tags from "./tags.js"; export default { _id: (node) => encodeProductOpaqueId(node._id), - media: (node, args, context) => getProductMedia(node, context), + media: (node, args, context) => getProductMedia(node, args, context), metafields: (node) => node.metafields || [], shop: resolveShopFromShopId, slug: (node) => node.handle, diff --git a/src/core-services/product/schemas/product.graphql b/src/core-services/product/schemas/product.graphql index d818e7a4a14..b5a558753c5 100644 --- a/src/core-services/product/schemas/product.graphql +++ b/src/core-services/product/schemas/product.graphql @@ -19,7 +19,10 @@ type Product { isVisible: Boolean! "All media for a product" - media: [ImageInfo] + media( + "Determines whether variant media should be included in the product or not" + shouldIncludeVariantMedia: Boolean = true + ): [ImageInfo] "The product description to use for page `description` meta element in HTML" metaDescription: String @@ -605,6 +608,7 @@ extend type Query { "Shop ID" shopId: ID! + ): Product "Query for a list of Products" diff --git a/src/core-services/product/utils/getProductMedia.js b/src/core-services/product/utils/getProductMedia.js index d457c5402d3..be52cbdae82 100644 --- a/src/core-services/product/utils/getProductMedia.js +++ b/src/core-services/product/utils/getProductMedia.js @@ -3,18 +3,26 @@ * @method getProductMedia * @summary Get an array of ImageInfo objects by Product ID * @param {Object} product - A product of type simple + * @param {Boolean} shouldIncludeVariantMedia - Whether to return variant media or not * @param {Object} context - The per request context * @returns {Promise} Array of ImageInfo objects sorted by priority */ -export default async function getProductMedia(product, context) { +export default async function getProductMedia(product, { shouldIncludeVariantMedia = true }, context) { const { Media } = context.collections; const { _id: productId, shopId } = product; + if (!Media) return []; + let includeVariantMedia = {}; + if (!shouldIncludeVariantMedia) { + includeVariantMedia = { "metadata.variantId": null }; + } + const mediaArray = await Media.find( { "metadata.shopId": shopId, "metadata.productId": productId, + ...includeVariantMedia, "metadata.workflow": { $nin: ["archived", "unpublished"] } }, {