How to determine if a File is used by a product?

How to determine if a File is used by a product?

alttextai
Shopify Partner
6 0 2

I am trying to use the File API (https://shopify.dev/docs/api/admin-graphql/2023-07/queries/files) to get a list of the files in a store. Ultimately I'm looking for files which are *not* product images -- so things like images used on blog pages, etc.  So I need to determine for each file if it is used as a product image or not. The data returned by querying the File objects does not seem to give back this info.  However, when loading a store's Content->Files page (https://admin.shopify.com/store/<store-name>/content/files?selectedView=all), it immediately shows a column called References which details what product(s) use the file.  This is the info I need to get from the API.

 

Is there any way to get this info? That admin page on the store seems to be using some other API call.

Replies 6 (6)

Liam
Shopify Staff
2873 312 819

Hi Alttextai,

 

Currently, the Shopify Admin GraphQL API does not provide a direct way to find out where a file is being used or referenced in the store, like in product images or blog posts. The information available from the `File` object is mostly related to the file itself, such as its URL, created time, file status, and error messages.

 

One possible workaround could be to fetch all the products, blog posts, pages, etc., separately and check if the file URL is present in any of these objects. This could be a very time-consuming process though, especially if stores have a lot of content.

 

Alternatively, you can use the REST API to get product images specifically , and then cross-reference these with the files you get from the GraphQL `files` query to determine which ones are not product images. You can use the `admin/api/2023-07/products/#{product_id}/images.json` endpoint to get a list of all images associated with a specific product.

 

Hope this helps!

Liam | Developer Advocate @ Shopify 
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Mark it as an Accepted Solution
 - To learn more visit Shopify.dev or the Shopify Web Design and Development Blog

alttextai
Shopify Partner
6 0 2

Hi @Liam -- I'm sure you realize all of these "workarounds" are pretty bad solutions 🙂 Is it possible to just have this information added to the API response soon?

Liam
Shopify Staff
2873 312 819

Hi again Alttexta - being able to easily return the relationship of specific files to resources from an API call would for sure be the best solution in this situation. I've passed on this feedback as a feature request to our API team! 

Liam | Developer Advocate @ Shopify 
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Mark it as an Accepted Solution
 - To learn more visit Shopify.dev or the Shopify Web Design and Development Blog

atyaglik
Shopify Partner
3 0 1

Any updates on this?
It's very important for me to make this work 😞

ShopifyDevSup
Shopify Staff
1453 238 498

Hey @alttextai and @atyaglik

 

Just testing this here, the files query does have a query filter for product_id. You can use this to determine files that are, or are not associated with a product. 

 

For example, the below will return all files that have a product ID,  

files(first: 5, query: "product_id:*")

And combining that with a NOT query will return all images without a product ID:

files(first: 5, query: "-product_id:*")

 

Here's an example of a full query for files not associated with a product, capturing the different mediaTypes 

 

{
   files(first: 5, query: "-product_id:*") {
       edges {
           node {
               createdAt
               updatedAt
               alt
               ... on GenericFile {
                   id
                   originalFileSize
                   url
               }
               ... on MediaImage {
                   id
                   image {
                       id
                       originalSrc: url
                       width
                       height
                   }
               }
               ... on Video {
                   id
                   duration
                   preview {
                       status
                       image {
                           id
                           width
                           height
                           url
                       }
                   }
                   originalSource {
                       url
                       width
                       height
                       format
                       mimeType
                   }
                   sources {
                       url
                       width
                       height
                       format
                       mimeType
                   }
               }
           }
       }
   }
}
 

Here's a link to the different queries available. The used_in query would be useful for retrieving files that are used in other places like themes https://shopify.dev/docs/api/admin-graphql/2024-04/queries/files#argument-query 

 

Hope that helps! 

 

- Kyle G.

Developer Support @ Shopify
- Was this reply helpful? Click Like to let us know!
- Was your question answered? Mark it as an Accepted Solution
- To learn more visit Shopify.dev or the Shopify Web Design and Development Blog

alttextai
Shopify Partner
6 0 2

Interesting! Will give this a try, thanks 👍