GROQ query for Mux video asset returning only _ref instead of full data

9 replies
Last updated: Sep 11, 2021
I am currently trying to integrate Mux video playback in my Sanity project with a plain react FE.
I am using the 'mux-input' plugin for uploading integration in the studio and that works perfect.
🙂
i want to use the 'sanity-mux-player' for showing the videos on client side, and i have been following this guide :
https://www.sanity.io/blog/first-class-responsive-video-support-with-the-new-mux-plugin
But when i try and query with the GROQ example i only get a _ref in the video asset. According to the guide i should be getting a lot more data.

I have also tried to query everything on the video type and i still don't get anymore data back.

What is the correct GROQ query?

Any help or tips on how to query the data with GROQ correctly would be greatly appreciated Thanks.
🙂
AI Update

The issue you're encountering is a common one when working with Mux videos in Sanity! You're getting just a _ref because you need to use the dereference operator (->) in your GROQ query to expand the reference and get the full asset data.

The blog post example you're following uses Mux video inside a Portable Text field (that's what the text[] part is about), but it sounds like you have a direct video field on your document instead.

For a direct video field on your document type, your GROQ query should look like this:

*[_type == "video"]{
  ...,
  'video': video.asset->
}

This will dereference the asset and give you all the Mux asset data including playbackId, status, data, and other metadata that the sanity-mux-player component needs.

If you want to be more explicit about what data you're fetching, you can also write it like:

*[_type == "video"]{
  ...,
  video {
    asset-> {
      playbackId,
      status,
      data
    }
  }
}

The key is that -> operator after asset - that tells GROQ to follow the reference and return the actual asset document instead of just the reference object with _ref.

Once you have the playbackId in your query results, you can pass the asset data directly to the sanity-mux-player component's assetDocument prop, and it should handle the rest! The player component expects the full asset document with all the Mux metadata, which is why dereferencing is essential here.

Show original thread
9 replies
Hi Rune. It sounds like you might need to add the dereference operator (
->
). If you’d like, feel free to paste your GROQ query and we can take a look.
Hi Geoff,
Thanks for the swift reply
🙂
heres my GROQ query:


*[_type == "video"]{
...,
text[]{
...,
_type == "muxVideo" => {
...,
asset -> {
...,

"url": "
https://stream.mux.com/ " + playbackId }
}
}
}
Thanks for that. Are you getting the
_ref
on
asset
? What does this give you?

*[_type == "video"]{
  ...,
  text[]{
  	...,
	  _type == "muxVideo" => {
  		...,
	}
}
Yes i am getting the _ref on asset.
i get this response:


[
{
"_createdAt": "2021-09-11T15
:10:36Z", "_id": "799b8aca-df7b-4c24-8463-0cd3c7a73e6d",
"_rev": "xZsaDmmUWhmXMRp153zWbH",
"_type": "video",
"_updatedAt": "2021-09-11T15
:10:36Z", "title": "video test",
"video": {
"asset": {
"_ref": "25ec7227-83bf-4ae0-80e2-d6e97683d57e"
}
}
}
]
The example code from that guide uses the video inside of a portable text editor, which is what that
text[]
bit is about. Could you try this?

*[_type == "video"]{
  ...,
  'video': video.asset->
}
There are other ways you could write this to get additional details back, but this should resolve the reference. Edit: Actually, it doesn’t look like there’s anything you’re missing on
video
, so this should be sufficient.
The example code from that guide uses the video inside of a portable text editor, which is what that
text[]
bit is about. Could you try this?

*[_type == "video"]{
  ...,
  'video': video.asset->
}
There are other ways you could write this to get additional details back, but this should resolve the reference.
ah ok i totally missed that.
The query you just gave me works perfectly
🙂 🚀
Thanks for your help 🙌
Great! Glad that’s working. Have fun building! 🙂

Sanity – Build the way you think, not the way your CMS thinks

Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.

Was this answer helpful?