Return a list of products with media files.

BeamJokerFavor
Explorer
51 3 6

Maybe you can help me write a query in GraphQL.

I want a query so if a product has a media file (glb) I want to make it active. If not, I want it archived. How would I do this? I am thinking a query for all products with GLBs, and another query for all products without GLBs, and use those lists of GIDs to activate or archive respectively. Is there an easier way? How do I check for existence of a media file?

Replies 3 (3)
Luke_K
Shopify Staff
Shopify Staff
402 65 92

Hey @BeamJokerFavor 

You could use the Media connection on the Product to do this - check out the post here for further details and be sure to see the GraphQL reference docs here - thanks!

 

| Shopify |
- Was my reply helpful? Click Like to let me know!
- Was your question answered? Mark it as an Accepted Solution!
BeamJokerFavor
Explorer
51 3 6

This requires a gid for a single product. Would I have to loop through all my products one at a time?

Luke_K
Shopify Staff
Shopify Staff
402 65 92

Hey @BeamJokerFavor 

Yes, it does appear, that you would have to loop through/handle this programatically. 

With GQL to do it without a gid would require something more complex like this below. Not necessarily great, but would return the Model 3D GLB.

 

{
  products(first:5){
    edges {
      node {
        title
        productType
           media(first:5) {
            edges {
               node {
                mediaContentType
                __typename
          ... fieldsForMediaTypes
            }
          }
        }
      }
    }
  }
}

fragment fieldsForMediaTypes on Media {
  alt
  mediaContentType
  status
  ... on Model3d {
    sources {
      format
      mimeType
      url
    }
    originalSource {
      format
      mimeType
      url
    }
  }
 }

 

Perhaps filtering via some custom logic on the result of the dataset that is returned from the above could be an option considering your use case.

All this aside, I think this is an excellent piece of feedback - it feels to me like this could be easier honestly -  and I've raised this as a feature request with the Product team for their consideration. Hope this helps explain more at least!

 

| Shopify |
- Was my reply helpful? Click Like to let me know!
- Was your question answered? Mark it as an Accepted Solution!