Watch a live product demo 👀 See how Sanity powers richer commerce experiences

Is it possible to have a `type: array`, of more than one entry for `type: reference` ? I kept getting an error that pointed to <https://www.sanity.io/help/schema-array-of-invalid>...

12 replies
Last updated: Dec 31, 2020
Is it possible to have a
type: array
, of more than one entry for
type: reference
? I kept getting an error that pointed to https://www.sanity.io/help/schema-array-of-invalid when I tried to make multiple
{type: 'reference', to: [a single type]}
. I tried uniquely `name`ing them on both the
to
values, and the reference type itself, but it's hung up on the fact I have dozens of
reference
types in the
type: 'array'
.
Currently reverted back to just a single
type: 'reference'
with a lot of
to
values, but it's problematic because of the many types I have, many of them have hundreds of results, making it very difficult for me to say I want "this document" from "this type"
The generated groq from that on dash is quite huge too since it has to match on a ton of fields on each type
😐
Dec 29, 2020, 9:47 PM
Do you have it set up like this?

fields: [
  ...,
  {
    name: "involved",
    title: "People Involved",
    type: 'array',
    of: [
      {
        type: "reference",
        to: [{ type: "managers" }, { type: "staff" }],
      },
    ],
  },
...
}

Dec 29, 2020, 10:03 PM
thats what i currently have it set up as, but I would like to have multiple
type: 'reference'
inside it so I can have more targeted search by types
Dec 29, 2020, 10:03 PM
Or some means of being able to select what type that was given to it in the reference link dialog. Right now the choice is search all the `to`s, which is like dumping the entire dataset every search
Dec 29, 2020, 10:05 PM
You can’t have multiple ones like you describe unfortunately, but would it help with a filter? https://www.sanity.io/docs/reference-type#filter-ebd7a95f9dc6
Dec 30, 2020, 8:36 AM
Right now the choice is search all the `to`s, which is like dumping the entire dataset every search
This shouldn't be a concern, it's basically the same as any query to the dataset, but I get why the user experience might not be the best if you don't what to search for.
The solution here is to add
name
 to the array fields so that the studio knows how to differ between them.
{
      name: 'multipleReferences',
      type: 'array',
      of: [
        {
          name: 'referenceA',
          type: 'reference',
          to: [{type: 'a'}],
          title: 'Make reference to A docs'
        },
        {
          name: 'referenceB',
          type: 'reference',
          to: [{type: 'b'}],
          title: 'Make reference to B docs'
        }
      ]
    },

Dec 30, 2020, 9:39 AM
This will change the array items` 
_type
 from
reference
 to
referenceA
and
referenceB
Dec 30, 2020, 9:40 AM
Wish it didn't change the _type (current auto-dereferencing code I wrote doesn't understand that) but I guess I can match on _type
^reference(_\w*)?
and pray everyone follows convention.
Dec 31, 2020, 3:42 PM
You can look for the
_ref
key?
Dec 31, 2020, 3:43 PM
That’s the best guarantee. I see your point though!
Dec 31, 2020, 3:43 PM
Probably, but is that _ref key protected like _type is (as in, I can't make a field
name: '__ref')
? (you're drunk slack, get some coffee)
Dec 31, 2020, 3:43 PM
Good to know though. A coworker recently fell into that
name
trap and neither of us could figure out what was going on (outside of "name seems to be dangerous, dont use it)". Now I can explain it lol
Dec 31, 2020, 3:45 PM
I don’t think it’s protected like that, but I have never observe anyone use it outside of the reference context. We should probably throw a warning if you do add field names that corresponds with those we use for specific things
Dec 31, 2020, 5:44 PM

Sanity– build remarkable experiences at scale

The Sanity Composable Content Cloud is the modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Related answers

Get more help in the community Slack

TopicCategoriesFeaturedRepliesLast Updated
After adding the subtitle and running this code npm run graphql-deploy It does nothingSep 15, 2020
how to limit a reference to just one entry in Studio reference input side versus the default as-many-entries-as-you-fill-in-an-array...Sep 18, 2020
Is it possible to fetch more than one "_type" using GROQ?Nov 2, 2020
I want to add a view with the Structure builder (S.view.component) where I list similar documents based on the title. What...Sep 23, 2020
Is there a structure builder example where the format of each preview for the document list is modified?Feb 3, 2021
I have an array of references to a country schema type but it always just returns NULL values for meJan 30, 2021
Hi, I need help with a query for getting the url of an image asset. Here is what I've been trying, but I only get the _ref...Dec 1, 2020
Sanity UI looks brilliant :smiley: Is something like the current date picker possible at the moment? I’m not sure if anicon...Dec 21, 2020
Hey everyone. I have been coding and may have potentially accidentally deleted something. Does anyone know how to resolve...Dec 26, 2020
Hello everyone and happy new year :raised_hands::skin-tone-2:, I have a problem with outputting Portable Text :disappointed:...Jan 1, 2021

Related contributions

Clean Next.js + Sanity app
- Template

Official(made by Sanity team)

A clean example of Next.js with embedded Sanity ready for recomposition.

Cody Olsen
Go to Clean Next.js + Sanity app

Blog with Built-in Content Editing
- Template

Official(made by Sanity team)

A Sanity-powered blog with built-in content editing and instant previews.

Go to Blog with Built-in Content Editing