Files API - Get file url

florinanghel
Shopify Partner
132 17 24

Trying to use the new files api to list the files uploaded in the shopify admin Files page (or through the files api).

That works fine but it looks like the file name or url cant be obtained from the api.

florinanghel_0-1625755699123.png

If the file is an image then the url can be obtained from the preview image but it doesnt work for other file types.

0 Likes
florinanghel
Shopify Partner
132 17 24

I was not familiar with graphql inline fragments, the way to get the file url is by using inline fragments on the returned concrete types.

files(first: 20, sortKey: CREATED_AT, reverse: true) {
                pageInfo {
                    hasNextPage
                    hasPreviousPage
                }
                edges {
                    cursor
                    node {
                        alt
                        createdAt
                        fileStatus
                        ... on GenericFile {
                            url
                            id
                        }
                        ... on MediaImage {
                            id
                            image {
                                originalSrc
                            }
                        }
                        preview {
                            image {
                                height
                                transformedSrc(maxWidth: 300, maxHeight: 300)
                                width
                            }
                        }
                    }
                }
            }