Is it possible to set a weak reference through a mutation in Sanity.io?

2 replies
Last updated: Feb 2, 2024
Is it possible to set a weak reference through a mutation? My current mutation is this:

mutation = {
            mutations: [
                {
                    create: {
                        _id: crypto.randomUUID(),
                        _type: "team",
                        title: team.name,
                        badgeUrl: team.logo,
                        footballApiId: team.id,
                        isNationalTeam: team.national,
                        country: {
                            _type: 'reference',
                            weak: true,
                            _ref: sanityCountry._id
                        },
                        slug: {
                            type: '_slug',
                            current: slugify(`${team.id}-${team.name}`)
                        }
                    }
                }
            ]
Yet, when I add the
weak: true
to the country reference, it returns this error code when this prop is included:
ERROR TypeError: Cannot read properties of undefined (reading '0')

So my question is, is this possible?
AI Update

Yes, it's definitely possible to set a weak reference through a mutation! The issue you're encountering is that you're using the wrong property name.

When defining weak references in your schema, you use weak: true. However, when creating weak references in mutations (through the API/client), you need to use _weak: true (with an underscore) instead.

Here's your corrected mutation:

mutation = {
    mutations: [
        {
            create: {
                _id: crypto.randomUUID(),
                _type: "team",
                title: team.name,
                badgeUrl: team.logo,
                footballApiId: team.id,
                isNationalTeam: team.national,
                country: {
                    _type: 'reference',
                    _weak: true,  // Changed from 'weak' to '_weak'
                    _ref: sanityCountry._id
                },
                slug: {
                    type: '_slug',
                    current: slugify(`${team.id}-${team.name}`)
                }
            }
        }
    ]
}

This naming convention difference is documented in the Sanity weak references documentation. The _weak property tells Sanity that this reference doesn't require the referenced document to exist and allows linking to unpublished/draft documents, which can be really helpful for flexible content workflows.

The underscore prefix follows Sanity's convention for system-level properties in the actual document data, while the schema uses the more readable weak property name for configuration. So remember: weak: true in schemas, _weak: true in mutations and document data!

I think you need to use
_weak
instead.
This resolved it, thank you!

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?