Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: filter out variant media from product media query #6165

Merged
merged 8 commits into from
Apr 3, 2020
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