🔮 Sanity Create is here. Writing is reinvented. Try now, no developer setup

Help with resolving references in an array of references in a Sanity schema

40 replies
Last updated: Oct 2, 2020
I'm having some trouble working with an array of references and would love some help. Details in the thread.
Oct 2, 2020, 2:18 PM
I have a schema with fields like this
fields: [
		{
			name: 'discipline',
			title: 'Discipline',
			type: 'reference',
			to: {
				type: 'discipline'
			}
		},
		{
			name: 'services',
			title: 'Services',
			type: 'array',
			of: [
				{
					type: 'reference',
					to: [{ type: 'service' }]
				}
			]
		}
	],
(Each "discipline" and "service" just has a title & slug field)
Oct 2, 2020, 2:19 PM
This is the query I have so far
*[_type == 'home'] {
  "ds": {
    "discipline": disciplines_services[].discipline->{
    	title,
      "slug": slug.current
    },
    "services": disciplines_services[].services,
	}
}
That correctly drills into the reference for the discipline but I can't figure out how to get at fields for each service item.
Oct 2, 2020, 2:20 PM
I've tried
"services": disciplines_services[].services->
and
"services": disciplines_services[].services[]->
but that just returns null
Oct 2, 2020, 2:21 PM
I also tried
"services": disciplines_services[].services[]->{title},
but that returns an empty object.
Oct 2, 2020, 2:21 PM
I have a really similar schema and I think this is mine translated:
disciplines_services[].services[]{service->}
Oct 2, 2020, 4:06 PM
That gives me back an empty item for each service
Oct 2, 2020, 4:08 PM
Weird, does
disciplines_services[].services[]{service->{…}}
give you anything?
Oct 2, 2020, 4:14 PM
Which is weird because the original query shows the individual reference items
"services": disciplines_services[].services, 
Oct 2, 2020, 4:14 PM
Trying your second suggestion now
Oct 2, 2020, 4:16 PM
Still gives an empty array 😞
Oct 2, 2020, 4:17 PM
Can you post the output of
disciplines_services[].services
?
Oct 2, 2020, 4:18 PM
[
  [
    {
      "_key": "2ca0512392aa",
      "_ref": "5df9db11-183c-4cd3-a778-85a51fdb18f8",
      "_type": "reference"
    },
    {
      "_key": "cda9ad18d073",
      "_ref": "ffc9ea0a-fb77-4734-8cfc-98fcc1305477",
      "_type": "reference"
    },
    {
      "_key": "295a8e145aeb",
      "_ref": "8191b9e1-4c26-4a1c-8105-403c7213a688",
      "_type": "reference"
    },
    {
      "_key": "378c191f220a",
      "_ref": "22bcf04e-0eee-4baa-8564-b02c48339a3f",
      "_type": "reference"
    }
  ],
  [
    {
      "_key": "c1c9f86b47bd",
      "_ref": "963864f0-72ef-4891-9b23-7bc4df19bc6a",
      "_type": "reference"
    }
  ]
]
Oct 2, 2020, 4:58 PM
Ah, you’re getting back an array of arrays that contain references, not just an array of service references which I expected. I tried to replicate your schema to get the same response shape and I can’t seem to match it. Sanity gives me an error if I try to nest arrays 🤷‍♂️
However, with a schema like this:

home
(
document
) ->
disciplines_services
(
object
) w/ the fields from your snippet
This query returns what I’d expect, _and I think what you want_:

*[_type == "home"]{
  "ds": {
    "discipline": discipline_services.discipline->{name},
    "services": discipline_services.services[]->{name}
  }
}
Which is:

[
  {
    "ds": {
      "discipline": {
        "name": "Discipline Test 1"
      },
      "services": [
        {
          "name": "Service Test 1"
        },
        {
          "name": "Service Test 2"
        }
      ]
    }
  }
]

Oct 2, 2020, 5:48 PM
That still doesn't work for me
*[_type == 'home'] {
  "ds": {
    "discipline": disciplines_services[].discipline->{title},
    "services": discipline_services.services[]->{title}
	}
}
gives me back an array of disciplines but doesn't give me back anything at all for services.
Oct 2, 2020, 6:05 PM
If I do
"services": disciplines_services[].services[0][0]->{title}
, I do get back the title for that specific item.
Oct 2, 2020, 6:17 PM
I was really expecting
"services": disciplines_services[].services[][]->{title}
to do the trick, but no luck there.
Oct 2, 2020, 6:17 PM
I feel like the data in there might not actually match your schema somehow, services should be an array of references right? But you have an array of arrays
Oct 2, 2020, 6:32 PM
If it helps, here's what it looks like in the admin
Oct 2, 2020, 6:35 PM
So Discipline is just a straight reference and Services is an array of references.
I realize I wasn't clear before. I then have a document that has a field that's an array of those disciplineService types.


{
			name: 'disciplines_services',
			title: 'Disciplines & Services',
			descriptions: 'Displayed on the homepage under "what we do"',
			type: 'array',
			of: [
				{
					type: 'disciplineService'
				}
			]
		}
Oct 2, 2020, 6:37 PM
So I am actually querying an array of arrays.
Sorry for the miscommunication
Oct 2, 2020, 6:37 PM
(and ignore that Invalid preview config)
Oct 2, 2020, 6:37 PM
It may not be possible to do what I want. I found this note in the docs

Combining multiple
[]
traversal operators in a single access expression currently has undefined behavior.
Oct 2, 2020, 6:50 PM
Just realized you had a typo in this one:
*[_type == 'home'] {
  "ds": {
    "discipline": disciplines_services[].discipline->{title},
    "services": discipline_services.services[]->{title}
	}
}

"services": discipline*s*_services
Oct 2, 2020, 6:50 PM
"services": disciplines_services.services[]->{title}
doesn't work because it's missing the array reference on
disciplines_services
Oct 2, 2020, 6:52 PM
But adding it also doesn't work
"services": disciplines_services[].services[]->{title}
Oct 2, 2020, 6:52 PM
What you’re trying to do is definitely possible
Oct 2, 2020, 6:53 PM
Well, it's definitely possible to get the array of references back. But I'm not seeing anyway to actually resolve those references inside the services array.
Oct 2, 2020, 6:54 PM
*[_type == "home"]{
  disciplines_services
}
is this query
Oct 2, 2020, 6:54 PM
I believe the issue is your
services
isn’t an array objects like above
Oct 2, 2020, 6:55 PM
I'm not sure I understand what you mean.
Oct 2, 2020, 6:56 PM
What do you get if you run this:
*[_type == "home"]{
  disciplines_services
}
Oct 2, 2020, 7:07 PM
I'm only going to ever have one top-level result so I've been running
*[_type == "home"]{
  disciplines_services
}[0]
Oct 2, 2020, 7:08 PM
So we can simplify the result to
Oct 2, 2020, 7:08 PM
From there, we're back to the original problem.
I can resolve the reference to the discipline of each array item.

But I can't resolve any of the references in the services array
Oct 2, 2020, 7:10 PM
I'm just going to punt and make the services an array of strings instead of references.
Thanks for the help!
Oct 2, 2020, 7:23 PM
One more thing to try if you haven’t, this works for me with the exact same response as above
Oct 2, 2020, 7:27 PM
*[_type == "home"]{
  "ds": {
    "discipline": disciplines_services[].discipline->{name},
    "services": disciplines_services[]{services[]->{name}}
  }
}
Oct 2, 2020, 7:27 PM
OMG THAT WORKED ❤️ ❤️ ❤️
Oct 2, 2020, 7:31 PM
You just never gave up hope 😝
Much appreciated
Oct 2, 2020, 7:31 PM
haha no worries, I knew it was possible
Oct 2, 2020, 7:32 PM

Sanity– build remarkable experiences at scale

Sanity is a 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.

Was this answer helpful?