Skip to content

Commit

Permalink
Merge pull request #6165 from reactioncommerce/willopez-product-media…
Browse files Browse the repository at this point in the history
…-fixes

refactor: filter out variant media from product media query
  • Loading branch information
mikemurray authored Apr 3, 2020
2 parents 4890007 + 8b6eaf6 commit 9f958fc
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/core-services/product/resolvers/Product/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 5 additions & 1 deletion src/core-services/product/schemas/product.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -605,6 +608,7 @@ extend type Query {

"Shop ID"
shopId: ID!

): Product

"Query for a list of Products"
Expand Down
10 changes: 9 additions & 1 deletion src/core-services/product/utils/getProductMedia.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<Object[]>} 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"] }
},
{
Expand Down

0 comments on commit 9f958fc

Please sign in to comment.