Solved

502 Bad Gateway error when trying to create a product with 30+ variants using GraphQL.

chescko29
Shopify Partner
8 2 3

Hi,

I'm getting 502 Bad Gateway error when trying to create a product with 30+ variants using GraphQL. This is not the case if I create a product with fewer variants.

Looking in other cases it looks like it is a common problem, but I didn't found any way to solve this.

Any idea/recommendation?

Thanks.

Accepted Solution (1)
chescko29
Shopify Partner
8 2 3

This is an accepted solution.

Hi.

After a while doing some test and researching, we finally had to adjust or code to:

1. First, create the product with his variants using productCreate mutation.
2. Then, upload all media files (images) to the product and assign images to each variant.

After that, we have not experienced 502 Bad Gateway errors anymore.

It looks like Shopify have troubles handling mutations with too many variants/images.

View solution in original post

Replies 5 (5)

Luke_K
Shopify Staff
402 66 98

Hey @chescko29 

By any chance would you have the request_id from that failing request? I can take a look into it for you. Thanks!

| Shopify |
- Was my reply helpful? Click Like to let me know!
- Was your question answered? Mark it as an Accepted Solution!
chescko29
Shopify Partner
8 2 3

This is an accepted solution.

Hi.

After a while doing some test and researching, we finally had to adjust or code to:

1. First, create the product with his variants using productCreate mutation.
2. Then, upload all media files (images) to the product and assign images to each variant.

After that, we have not experienced 502 Bad Gateway errors anymore.

It looks like Shopify have troubles handling mutations with too many variants/images.

ASLuis
Shopify Partner
6 0 2

Hello thanks for sharing, right now I am experiencing the same error, could you help me with an example of the code that you used to solve this problem?

chescko29
Shopify Partner
8 2 3

Hi.

 

This is a common issue. When you try to create a product including all images and all variants in a single productCreate mutation it looks like the size of the query is too big, so you get the 502 error.

 

So, I solved this by separating the product creation into multiple steps programmatically, which requieres more code but is more efficient.

 

I can't share examples of code but I can give you some steps to follow.

 

1. First, create the product with his variants using productCreate mutation. (Just create the product and variants with no images.) https://shopify.dev/docs/api/admin-graphql/2022-10/mutations/productcreate

2. Then, upload all media files (images) to the product. You should use stagedUploadsCreate mutation https://shopify.dev/docs/api/admin-graphql/2023-04/mutations/stageduploadscreate and productAppendImages https://shopify.dev/docs/api/admin-graphql/2023-04/mutations/productAppendImages

 

3. Finally assign images to variants using productvariantupdate mutation https://shopify.dev/docs/api/admin-graphql/2023-04/mutations/productvariantupdate

 

This is the way Shopify recommends to upload media. You can check this link for mor info https://shopify.dev/docs/apps/online-store/media/products

 

 

ASLuis
Shopify Partner
6 0 2

Thank you very much for the information, I will put it into practice!