Custom discount / graphql limits

Topic summary

A developer built a discount app using a resource picker to apply discounts to selected products. The app encounters a JavaScript error (“cannot read property ‘reduce’ of undefined”) when more than 4 products are selected, though it works fine with 4 or fewer products and any variant combination.

Key Technical Details:

  • The product list saves correctly in the admin interface with 20+ products
  • Products are stored in metafields successfully
  • Error occurs specifically when the discount function retrieves the data
  • Includes GraphQL input query code snippet (appears reversed/encoded)

Diagnosis & Solution:

  • Initial suggestion: JavaScript runtime error requiring console.error debugging to identify which property is undefined
  • Likely root cause: Hitting the 10kb metafield size limit when storing more than 4 products
  • Recommended fix: Store only product/variant IDs in the metafield instead of full product information to reduce data size

The developer confirmed products remain selected in the admin after saving 20 items, and plans to test storing only variant IDs as suggested.

Summarized with AI on November 8. AI used: claude-sonnet-4-5-20250929.

I have created a discount app that uses a resource picker to only apply the discount to selected products.

The problem is that when I select more than 4 products I get an error generated from by the Shopify function.

Error parsing selectedProducts: TypeError: cannot read property 'reduce' of undefined

It works fine with any combination of variants and in the admin interface the product list saves with more than 4 products selected so the products are being stored in the metafield there’s just a problem when the discount function retrieves them when there’s more than 4.

I’ve tried to get the discount metfields from GRaphicQL but I couldn’t work it out.

here’s my input query for the discount function.

query Input {
  cart {
    lines {
      quantity
      merchandise {
        __typename
        ...on ProductVariant {
          id
        }
      }
    }
    buyerIdentity {
      email
      customer{
        metafield(namespace: "loyalty-discount", key: "loyalty_tier") {
          value
        }
      }
    }
  }
  discountNode {
    functionConfiguration: metafield(namespace: "$app:loyalty-discount", key: "function-configuration") {
      value
    }
    selectedProducts: metafield(namespace: "$app:loyalty-discount", key: "selected-products") {
      value
    }
  }
}

Thanks Dan

Hi Dan-Hrpr,

This doesn’t seem like you’re running into a limit (e.g. instruction count limit or input query limit).

Specifically, the error message “TypeError: cannot read property ‘reduce’ of undefined” makes me think it’s a JS runtime error.

If you console.error the input when your function runs, you might be able to identify the cause. Maybe selectedProducts, or one of its properties, is undefined.

Best,

Tobe

Hi Tobe,

It is undefined but only when I save more than 4 products in it.

Cheers Dan

You might be running into the 10kb metafield size limit, then. I recommend only storing the IDs of products. I don’t know if you are storing other information about products in the metafield, but it fills up fast.

I did think that but it works in the backend in the product selector.

So if I add 20 products in the product selector and then save the discount then when I edit the discount again the 20 products are still selected.

I will try and save the variant id’s only like you suggest.