Difficulty accessing unpublished references in Sanity.io

2 replies
Last updated: Dec 12, 2023
Hello,
Can be pulled an strengthenOnPublish?

Example in the ss.


I want to access the reference but because is _strengthenOnPublish I cannot, (is not published yet). Does anyone know if can be possible?

The query to see the properties:


*[_id == 'drafts.11a505c1-2e8c-4276-af96-9e05535f77c3' || _id == "11a505c1-2e8c-4276-af96-9e05535f77c3"] {

slug,

modules[_ref == "a118f6b8-1126-4e9b-96de-e4fb4d28be00"] {

...

}

}

When I try to use -> for the reference I cannot get it.
Example:


*[_id == 'drafts.11a505c1-2e8c-4276-af96-9e05535f77c3' || _id == "11a505c1-2e8c-4276-af96-9e05535f77c3"] {

slug,

modules[_ref == "a118f6b8-1126-4e9b-96de-e4fb4d28be00"] -> {

...

}

}

Any thoughts?
AI Update

Based on your question, it looks like you're dealing with _strengthenOnPublish which is a property Sanity adds to references that are created in draft mode. When you try to dereference these with ->, you can't access the referenced document because it's treating it as a regular reference that expects a published document.

The issue is that _strengthenOnPublish indicates the reference will be "strengthened" (converted to a strong reference) when the document is published, but while it's in draft mode, it behaves differently than a standard reference.

Here are a few approaches to work around this:

1. Use a subquery instead of the dereference operator

Instead of using -> to dereference, you can manually fetch the referenced document using a subquery:

*[_id == 'drafts.11a505c1-2e8c-4276-af96-9e05535f77c3' || _id == "11a505c1-2e8c-4276-af96-9e05535f77c3"] {
  slug,
  "resolvedModules": modules[_ref == "a118f6b8-1126-4e9b-96de-e4fb4d28be00"] {
    _ref,
    "document": *[_id == ^._ref || _id == "drafts." + ^._ref][0]
  }
}

2. Convert to weak references

If you're creating these references programmatically or through mutations, consider using weak references. Weak references are specifically designed to handle draft documents and don't have the same dereferencing limitations:

{
  _type: 'reference',
  _ref: 'document-id',
  _weak: true
}

3. Query both draft and published versions

You can explicitly check for both draft and published versions of the referenced document:

*[_id == 'drafts.11a505c1-2e8c-4276-af96-9e05535f77c3' || _id == "11a505c1-2e8c-4276-af96-9e05535f77c3"] {
  slug,
  "resolvedModules": modules[_ref == "a118f6b8-1126-4e9b-96de-e4fb4d28be00"] {
    _ref,
    "document": *[_id == "drafts." + ^._ref || _id == ^._ref][0] {
      // your fields here
    }
  }
}

The key insight is that _strengthenOnPublish references can't be dereferenced with -> in the traditional way while in draft mode. You need to either use weak references from the start (if you control the reference creation) or manually resolve the references using subqueries that account for both draft and published document IDs.

The
->
operator does not work on unpublished references. You would need to perform a subquery to get its values.
I see, thank you
user M

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?