😎 Discover cool tips and tricks for customization in our next Developer Deep Dive virtual event - sign up now!

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

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?