0

Is it not possible to use an object type as a reference in sanity? For example this is not working. When I go to the field nothing shows up. If I can't do this how can I access the indexPage objects that have been created under other documents?

export const indexPage = {
  title: "Index Page",
  name: "indexPage",
  type: "object",
  fields: [
    {
      title: "Subheading",
      name: "subheading",
      type: "array",
      of: [{ type: 'block' }] 
    },
    {
      title: "Content",
      name: "content",
      type: "array",
      of: [{ type: "block" }]
    },
  ]
}

// in another file

export const coolPage = {
  title: "Cool Page",
  name: "coolPage",
  type: "object",
  fields: [
    {
      title: "Reference Index Page",
      name: "refIndexPage",
      type: "reference",
      to: [{ type: 'indexPage' }] 
    }
  ]
}
Taylor Austin
  • 5,407
  • 15
  • 58
  • 103

1 Answers1

4

References can only point to other documents; not to a specific part of a document. So to achieve this, indexPage would need to be a document.

I think modelling indexPage as a document would be a viable option in your case. You mentioned "indexPage objects that have been created under other documents". Instead of creating indexPage data inside a specific document, indexPage should be its own document type. Any other document can then connect to it via a reference. This approach should be really flexible for you.

Sanity Studio recently added support for "references in place", which makes this workflow even better. The studio now allows you to create a document to reference while you are editing the document that references it—without leaving the editor. You can see a demo here (no extra work on your part is needed here, it's handled automatically by Sanity Studio).

In summary: if you have a piece of data you'd like to share between multiple documents, model it as its own document type that is referenced by every document that is related.

Ash
  • 3,136
  • 2
  • 21
  • 12