Watch a live product demo đź‘€ See how Sanity powers richer commerce experiences

Moving to GraphQL in Gatsby - Reference Returns Null

5 replies
Last updated: Jul 22, 2022
Hi, I am new to Sanity and I have some issues with a reference type. I have a job schema where I have a skill field that references jobskill. Now when I move to GraphQL in Gatsby, the result for the skills is always null. Basically I think gatsby thinks that the skills field is just an arrray of strings and not of objects.This is my schema:

export default {
    name: 'job',
    title: 'Jobs',
    type: 'document',
    fields: [
        {
            name: 'title',
            title: 'Jobtitel',
            type: 'string',
        },
        {
            name: 'slug',
            title: 'Link',
            type: 'slug',
            description: 'Eindeutiger Link, welcher zum spezifischen Job fĂĽhrt. (z.B. projekt-manager)',
            options: {
                source: 'title',
                maxLength: 96,
            },
        },
        {
            name: 'datum',
            title: 'Datum',
            type: 'date',
            options: {
                dateFormat: 'YYYY-MM-DD',
                calendarTodayLabel: 'Today'
            },
            initialValue: (new Date()).toLocaleDateString('en-CA')
        },
        {
            name: 'skills',
            title: 'Skills',
            type: 'array',
            of: [{
                    type: 'reference',
                    to: [{type: 'jobskill'}]
                }]
        },
        {
            name: 'benefits',
            title: 'Vorteile',
            type: 'array',
            of: [{type: "string"}]
        },
        {
            name: 'konditionen',
            title: 'Konditionen',
            type: 'array',
            of: [{type: "string"}]
        }
    ],
}
And my jobskill schema looks like this:


const colors = [
    { "title": "Schwarz", "value": "000000"},
    { "title": "Orange", "value": "ff603b"},
    { "title": "GrĂĽn", "value": "b7b783"},
]

export default {
    name: 'jobskill',
    title: 'Job Skills',
    type: 'document',
    fields: [
        {
            name: 'title',
            title: 'Fähigkeit Bezeichnung',
            type: 'string',
        },
        {
            name: 'color',
            title: 'Farbe der Fähigkeit',
            type: 'string',
            options: {
                list: colors,
                layout: "dropdown"
            },
        }
    ]
}
And this is my request:


query ($slug: String) {
  sanityJob(slug: {current: {eq: $slug}}) {
    title
    benefits
    konditionen
    skills
  }
}
and that is the result I get:


{
  "data": {
    "sanityJob": {
      "title": "Projekt Manager:in M/W/D",
      "benefits": [
        "Familäres Umfeld",
        "Nette Kolleg:innen"
      ],
      "konditionen": [
        "ab € 5.300 brutto / monatlich",
        "25 Urlaubstage / Jahr",
        "Jahresprovision",
        "interne Schulungen"
      ],
      "skills": [
        null
      ]
    }
  },
  "extensions": {}
}
And in my Sanity Studio I have one skill added, and if I add two, there would be two null fields.

Thanks in advance!
Jul 22, 2022, 4:17 PM
Ok, it looks like my GatsbyJS GrapQL has an old version of the Sanity Schema. Do you know how to fix this?
Jul 22, 2022, 5:32 PM
Hey
user Q
! The GraphQL API needs to be redeployed any time you make a change to your schema.
Jul 22, 2022, 6:05 PM
You can do so with
sanity graphql deploy
Jul 22, 2022, 6:06 PM
Oh perfect! thanks a lot :)
Jul 22, 2022, 6:25 PM
You're welcome!
Jul 22, 2022, 6:57 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