Re: fileCreate GraphQL API doesn't behave as expected

Solved

fileCreate GraphQL API doesn't behave as expected

theflofly
Visitor
2 0 2

Recently the fileCreate GraphQL endpoint was released. It allows to upload files on the store that will end up under "Settings" > "Files".

My goal is to get the file url in order to use it in a product description once the upload is done.

The only way to get a file url is through the file.preview.image attribute on the file, nonetheless when I insert the file, the file.preview.image returned is null.

My GraphQL body is the following:

query = f""" mutation {{
                fileCreate(files: [{{
                    originalSource: "{dropbox_url}"
                    contentType: IMAGE
                    alt: "PLOP"
                }}]) {{
                    files {{
                        alt
                        createdAt
                        fileStatus
                        preview {{
                            status
                            image {{
                                altText
                                height
                                id
                                originalSrc
                                transformedSrc
                                width
                            }}
                        }}
                    }}
                    userErrors
                    {{
                        code
                        field
                        message
                    }}
                }}
            }}
            """

 

After some time if I read the files from the shop (through GraphQL), then the preview is returned correctly. My guess is that the preview is computed and take some time so the fileCreate returns an empty image at first.

Because the File does not have and id, I cannot simply wait a little bit and retrieve the image url. I would need to loop through all the files and match the file I just inserted with the right file using the created date timestamp (sub optimal).

Is this expected ?

Do you see another way to get the file url ?

Accepted Solution (1)
Vinnydude
Shopify Partner
21 1 5

This is an accepted solution.

Replies 4 (4)

Vinnydude
Shopify Partner
21 1 5

Unfortunately I don't have the answer for you, but I am experiencing exactly the same issue.

I'm using fileCreate to upload a file, but I can't then get the url I need because image is always null!

Vinnydude
Shopify Partner
21 1 5

This is an accepted solution.

I've got it! Run a second query on files and query the filename!

See my answer here:

https://community.shopify.com/c/Technical-Q-A/GraphQL-fileCreate-return-src/m-p/1300001/highlight/fa...

theflofly
Visitor
2 0 2

Thanks for the workaround, indeed it works.

The issue in my case is that I create the file then read it right away so I have to loop and sleep while waiting for the file to be ready.

Vinnydude
Shopify Partner
21 1 5

I've had to do the same as well! It's pretty annoying and very clunky!

I've got mine wrapped in a do / while loop with a sleep of 10 seconds. It resolves on the third cycle usually.

Not ideal, but it works!