Product variants being returned with incorrect position value when queried with GraphQL

I’m currently experiencing an issue with a GraphQL query I’m attempting using the Python requests library. For one particular product with 40 variants, the variants after 3 and 6 are returned with the same position again (so two threes and two sixes), and every position value after both of these occurrences is reduced by 1. However, if I export the data for this product using Matrixify, the positions correctly run from 1 to 40 without any duplicates. This only seems to affect this one particular product, what’s going on here?

Query I’m using:

Results of Query (csv):

Results of Matrixify export (csv):

Is there anything unusual about this product, eg: an app managing/changing anything related to variants?

I don’t believe so. The only changes I make to variants with an app using the API alter the tags if a product is new, and this product hasn’t been affected by this app.

Is there anything else I can check on my end?

@DYN_27 Out of curiosity, what if you go to the Shopify admin and temporarily switch the positions of the variants with position 3 (HA03073 and HA03069) and then switch them right back.

Then run the query, are the position values still repeated?

1 Like

I’ve switched the order in which the options appear and that seems to have fixed the issue, but still a bit concerned as to how the issue cropped up to begin with.

I was interested so I looked into this some more and here’s my guess at what’s happening: Shopify uses the position field to sort variants, but it doesn’t guarantee that the positions start at 1 and increase sequentially for each variant. Matrixify orders the variants in the export in the same order as Shopify, but starts the numbering at one, increasing with each variant.

Why does Matrixify do this?

When I looked at different products in GraphQL you’ll occasionally see gaps in the positions, or the first variant’s position is ‘2’. If you were to export your products and see that the first variant’s position is ‘2’ it would be confusing and Matrixify probably avoids a lot of support tickets by just starting the position at 1 each time.

Why does Shopify do this?

When the variants are displayed they’re probably doing something like the SQL equivalent of ORDER BY position, id. This doesn’t require the position to actually start at 1 and not contain duplicates. Most API calls, or reordering variants in the admin, will cause Shopify to recalculate the positions starting at 1 but my guess is there’s a few specific API calls, or race conditions, that could cause the sequence of positions on Shopify become different than you would expect.

What’s the best way to handle this?

If you need to sort the variants like Shopify, sort them according to position, and then variant ID. If you need to display the position of a variant, keep an incrementing counter as you loop through the variants and show that instead (similar to what Matrixify does)

2 Likes

That’s really useful to know, thanks for your thorough response!

1 Like