How to use internal links in sanity.io and gatsby with gatsby-source-sanity?

Viewed 748

I have created an internalLink type following the sanity documentation and as per the gotcha note about using internalLinks with a graphql api, I have created it as a separate type as follows:

export default {
    name: 'internalLink',
    type: 'object',
    title: 'Internal link',
    fields: [
      {
        name: 'reference',
        type: 'reference',
        title: 'Reference',
        to: [
          { type: 'helpTopic' },
          // other types you may want to link to
        ]
      }
    ]
  }

I have added it schema.js as follows:

import internalLink from './internalLink'

export default createSchema({
  name: 'default',
  types: schemaTypes.concat([
    internalLink,
    helpTopic,
    blockContent,
    figure,
    slideshow,
  ])
})

and added it to annotations as follows:

annotations: [
          {
            type: 'internalLink'
          }
        ]

When I use graphiql in Gatsby, if I do not use resolveReferences, then I get the unresolved references properly:

"markDefs": [
                {
                  "_key": "00a07e239d3d",
                  "_type": "internalLink",
                  "reference": {
                    "_ref": "7c635eee-0d98-5335-a376-4101922ca4b7",
                    "_type": "reference"
                  }
                }
              ]

however, when I use _rawBody(resolveReferences:{maxDepth:1000}), the reference does not get resolved and returns as null:

"markDefs": [
                {
                  "_key": "00a07e239d3d",
                  "_type": "internalLink",
                  "reference": null
                }
              ]

Is this a bug or is there something wrong that I am doing. I have tried going through the docs but cannot figure this out and the example gatsby sanity combo project does not have an internalLinks example.

Thanks for the help in advance.

1 Answers

As of now annotations cannot accept schema types like that. You will have to put the entire schema block in there. A bit of a weird limitation. I think someone already put in a ticket/feature request regarding this issue on Sanity.io's Github repo.

Related