Sanity array field with custom object type reference

43 replies
Last updated: May 15, 2020
{
      name: 'contacts',
      title: 'Contacts',
      type: 'array',
      of: [
        { type: 'contact_person' }
      ]
    }
AI Update

Your schema looks good! This defines an array field named contacts that will hold multiple items of type contact_person.

Here's what your schema does:

  • name: 'contacts' - the field identifier used in queries and code
  • title: 'Contacts' - the human-readable label shown in Sanity Studio
  • type: 'array' - makes this an orderable collection
  • of: [{ type: 'contact_person' }] - specifies that array items must be of type contact_person

The array field will automatically give you drag-and-drop reordering, and each item will get a unique _key property that Sanity uses to track items. You'll also get copy/paste functionality for free, letting editors copy individual contacts or entire arrays between documents.

Make sure you have a contact_person schema type defined elsewhere in your schema files. It could be either a document type or an object type, depending on whether you want contacts to be standalone documents (referenced) or embedded objects:

// As an embedded object (inline)
{
  name: 'contact_person',
  type: 'object',
  fields: [
    { name: 'name', type: 'string' },
    { name: 'email', type: 'string' },
    // ... other fields
  ]
}

// Or as a reference to a document
{
  name: 'contacts',
  type: 'array',
  of: [
    { 
      type: 'reference',
      to: [{ type: 'contact_person' }]
    }
  ]
}

If you're getting a validation error about invalid "of" property, double-check that contact_person is properly defined and exported in your schema.

Show original thread
43 replies
if contact_person is a document you need:

...
type: "array",
of: [{ type: "reference", to: { type: "contact_person" } }],
It’s not, it’s just embedded in the document
Do I have to make contact_person in to a document?
At least everything works in the studio, but my query doesn’t return the contacts field as part of the main document.
what is a contact_person? 🙂
Object
export default {
  name: 'contact_person',
  type: 'object',
  title: 'Contact person',
  fields: [
    {
      name: 'name',
      title: 'Name',
      type: 'string'
    },
    {
      name: 'email',
      title: 'Email',
      type: 'string'
    },
    {
      name: 'field',
      title: 'Field of expertise',
      type: 'string'
    }
  ]
};
how does your query look?
export async function preload({ params, query }) {
    const noticeQuery = `*[_type == "notice"][0]`;
    const noticeProjection = `{
      ...,
      body[]{
        ...,
        children[]{
          ...,
          "asset": asset->
        }
      }
    }`;
    
    const notice = await client.fetch(noticeQuery + noticeProjection);
did you deploy to sanity after you created the object?
sanity deploy
I’m running the studio locally (and deployed)
deployed to Netlify. I’ll try to deploy again.
Is the field not returned on the front-end side or not even in the playground after running
sanity graphql deploy
?
I’ve never run
sanity graphql deploy
, I’m deploying our studio only to Netlify and running it locally.
Can you see the values if you inspect your document within the Studio?
I could, but for some reason the schema didn’t update after deploying, so the studio offered me to unset the
contacts
I see, so the deploy might not have happened successfully. Do you see any errors in your Netlify build log?
I’ll re-build and re-deploy. Locally running I can see the
contacts
field in the studio.
Re-deployed and now it’s there
However the query still return nothing!
const noticeQuery = `*[_type == "notice"][0]`;
    const noticeProjection = `{
      contacts,
      ...,
      body[]{
        ...,
        children[]{
          ...,
          "asset": asset->
        }
      }
    }`;
It is there when I inspect the data in studio. But not when I query the data.
What happens if you try without
contacts
and leave
...,
intact?
Also, are you sure it is there on the specific single
notice
document you are querying with
[0]
?
What happens if you try without
contacts
and leave
...,
intact?
Still missing contacts. And there is only one document.
Also, are you sure it is there on the specific single
notice
document you are querying with
[0]
?
Re-deployed and now it’s there
However the query still return nothing!
const noticeQuery = `*[_type == "notice"][0]`;
    const noticeProjection = `{
      contacts,
      ...,
      body[]{
        ...,
        children[]{
          ...,
          "asset": asset->
        }
      }
    }`;
If you have the Vision plugin installed in your Studio, are you able to get the data there with the same query?
If you have the Vision plugin installed in your Studio, are you able to get the data there with the same query?
If you have the Vision plugin installed in your Studio, are you able to get the data there with the same query?
No, it’s not there with Vision
Only when I inspect the document
It is there when I inspect the data in studio. But not when I query the data.
That’s strange. Is the document published?
Yes
Do you have any repo you could share? (in DM if you prefer) - or a zip of the schema files.
Wait a sec.
It’s a private github repo. Who can I add to it?
(Resolved in DM)

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?