I'm having an issue inserting an image into a metaobject using the Shopify API

I am building a custom app for a client that lets me upload Google Sheet data into a Shopify Metaobject. Among all the fields I am populating, I need to attach an image to each metaobject.

Here is an example of a GraphQL mutation that I wrote to do this:

const query = `
        mutation UpsertMetaobject($handle:MetaobjectHandleInput!, $metaobject: MetaobjectUpsertInput!){
            metaobjectUpsert(handle: $handle, metaobject: $metaobject){
                metaobject{
                    type
                    title: field(key: "title"){
                        value
                    }
                    qualities: field(key: "qualities"){
                        value
                    }          
                }
                userErrors {
                    field
                    message
                    code
                }
            }
        }
    `;

    const variables = {
        "handle": {
            "handle": handle,
            "type": "hive_five_certification"
        },
        "metaobject": {
            fields: [
                {
                    "key": "title",
                    "value": `${title}`
                },
                {
                    "key": "icon",
                    "value": `gid://shopify/ImageSource/32543195857032`
                },
                {
                    "key": "qualities",
                    "value": `${qualities}`
                }
            ]
        }

As you can see from my mutation, there is a field with the key “icon” and I’m trying to use the gid of an image from my clien’t Shopify Store.

When I ran the query, I got this error:

message: 'Value must be a file reference string.'

After researching the Shopify API docs, I found an object type called GenericFile. I think I’m supposed to use the id from that object type since it seems like it is probably the “file reference” that the error message is talking about.

Is that true? And if it is, how can I incorporate GenericFile into the mutation I have above?

And if it is not true, what should I do?

Hey @Patrick_Pierre :waving_hand: - usually we’d be looking for something like “gid://shopify/MediaImage/id-here” as the input value when attaching pre-uploaded images to metaobjects.

If you first run a fileCreate mutation (more info here) to upload the image file (just be sure to include the id value in your mutation output), we’d create the MediaImage object that you can then use in your original mutation. I just did a quick test using that method and it worked for me.

Let me know if I can clarify anything or help out further - hope this helps!

2 Likes

@Alan_G Thanks for your response. I did not see the info about file queries and mutations in the Shopify Documentation. I need to attach images that have already been uploaded to the metaobjects.

But I can see from the documentation that I can use the MediaImage GraphQL object.
I’ve just tested it and it works for me. Thank you!

2 Likes