Skip to content
This repository was archived by the owner on May 6, 2023. It is now read-only.
This repository was archived by the owner on May 6, 2023. It is now read-only.

Cyclical references #206

@ehwarren

Description

@ehwarren

I'm not too sure this is an issue with the code, however, with no examples I just cant seem to get this to work properly.

Example would be an object called "Button", which contains text and a reference to sanity document types

button.ts

import { s } from 'sanity-typed-schema-builder'
import Page from 'schemas/documents/page'

const Button = s.objectNamed({
  name: 'button',
  fields: [
    {
      name: 'text',
      title: 'Text',
      type: s.string(),
    },
    {
      name: 'link',
      title: 'Link',
      type: s.reference({ to: [Page] }),
    },
  ],
})

export default Button

Then we have a reference to this button within the page document as well

page.ts

import Button from 'schemas/objects/page`
const Page = s.document({
  name: 'page',
  title: 'Pages',
  icon: DocumentIcon,
  fields: [
    {
      name: 'title',
      title: 'Title',
      type: s.string(),
    },
    {
      name: 'someButton',
      title: 'Button',
      type: Button.ref()
    },
  })

The Button is a named object because it is used in portable text blocks elsewhere aswell.

The only "solution" i could find right now was to hack the reference within the Button declaration as follows. This eliminates the need to reference the Page document, which is causing the cyclical reference.

const Button = s.objectNamed({
  name: 'button',
  fields: [
    {
      name: 'text',
      title: 'Text',
      type: s.string(),
    },
    {
      name: 'link',
      title: 'Link',
      /*@ts-ignore Importing page would give a cyclic reference*/
      type: s.reference({ to: [{ name: 'page' }] }),
    },
  ],
})

Is there any other way around this that I'm just missing?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions