Trying to assign a media file to a product fails

Hello. I’m trying to assign an existing image to multiple products in a Shopify store following recommendations from this thread: Attach Existing Media to a Product via GraphQL API

This is my code:

const query = `
	mutation fileUpdate($files: [FileUpdateInput!]!) {
		fileUpdate(files: $files) {
			files {
				id
			}
			userErrors {
				message
			}
		}
	}
`

const result = await shopify.graphql(query, {
	files: [
		{
			id: `gid://shopify/MediaImage/${mediaId}`,
			referencesToRemove: [],
			referencesToAdd: [`gid://shopify/Product/${productId}`],
		}
	]
})

And I’m getting this error: “Variable $files of type [FileUpdateInput!]! was provided invalid value for 0.referencesToRemove (Field is not defined on FileUpdateInput), 0.referencesToAdd (Field is not defined on FileUpdateInput)”

Both of the fields are described in the documentation here: FileUpdateInput - GraphQL Admin

What am I doing wrong?

1 Like

Hey @MB-EQN :waving_hand: - thanks for flagging this. I did a bit of testing on my end, and I think I might have a solution here! Could you try formatting the API call like this?

mutation fileUpdate($files: [FileUpdateInput!]!) {
fileUpdate(files: $files) {
files { id updatedAt }
userErrors { field message code }
}
}

Variables:

{
"files": [
{
"id": "gid://shopify/MediaImage/image-id",
"referencesToAdd": ["gid://shopify/Product/product-id"]
}
]
}

I’d just make sure you’re adding the quotation marks for the input fields under the files object. I think this should resolve the issue, but let me know if the error persists and I can definitely take a closer look. Hope this helps.

Hey @MB-EQN, wanted to follow up to see if the above helped/if we can mark this as solved?

Hi Alan,

No, that doesn’t fix the issue. Maybe I should’ve specified that but in my original post I used TypeScript syntax which doesn’t require quotation marks around object keys. The library that I’m using is shopify-api-node and it takes care of converting TypeScript objects into valid JSON strings before sending. Furthemore, the error that I’m getting references the fields by keys, confirming that JSON was parsed properly by Shopify servers.

Thanks for clarifying @MB-EQN - I’m unable to replicate the issue on my end here when I use my example above in an API client (Postman), the file is updated as expected and attached to the right product.

Would you be able to share an X-Request-ID from the API response headers we send out for an example call where you’re seeing the error message? I can take a look in our logs to see exactly what’s being passed over to us. Hope to hear from you soon!

Hi Alan,

While setting up logging for headers – I decided to try something else and it fixed the problem. shopify-api-node was the culprit: one of its configuration options is apiVersion which, according to its documentation, is optional and “defaults to the oldest supported stable version”. I’m not sure which specific version that is but it’s apparently not one of the versions that Shopify official documentation still lists. Explicitly setting it to 2025-07 fixed the issue I was getting.

Hey @MB-EQN - glad to hear the problem is resolved! I can’t say for sure why they’ve set the default to the older supported stable version since that looks to be a third party library. If you wanted to migrate to using our official library, that would be my recommendation, but I definitely get why using the Monei library works in some some cases:

Let me know if I can help with anything else on my end here :slight_smile: