Files API - Get file url

florinanghel
Shopify Partner
136 17 30

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.

Replies 3 (3)
florinanghel
Shopify Partner
136 17 30

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
                            }
                        }
                    }
                }
            }
devdude
Excursionist
23 1 19

This was really useful, thank you! But how would I go about looking up just ONE file by id?

 

Thanks!

https://www.merchbot.net
mmarienko
Shopify Partner
6 0 1

fileCreate

mutation fileCreate($files: [FileCreateInput!]!) {
    fileCreate(files: $files) {
        userErrors {
            field
            message
        }
        files {
            fileStatus
            createdAt
            fileStatus
            ... on GenericFile {
                url
                id
            }
            ... on MediaImage {
                id
                image {
                    originalSrc
                }
            }
        }
    }
}
{
  "files": {
    "originalSource": "https://shopify.s3.amazonaws.com/tmp/55561781388/files/c7518ddddde-9e2081664da7/dddd.mp4",
    "alt": "test",
    "contentType": "FILE"
  }
}


getFileUrl

query getFileUrl($id: ID!) {
    node(id: $id) {
        ... on GenericFile {
            id
            url
        }
        ... on MediaImage {
            id
            image {
                originalSrc
            }
        }
    }
}
{
  "id": "gid://shopify/GenericFile/111111111111111"
}