Moving to GraphQL in Gatsby - Reference Returns Null
I can see your issue! When using Gatsby with Sanity's GraphQL, reference fields need to be queried differently than simple string arrays. The problem is that your GraphQL query is only asking for skills as a flat field, but it's actually an array of references that need to be expanded.
Here's what's happening: Gatsby's GraphQL layer sees your reference array but doesn't automatically resolve the referenced documents. You need to explicitly tell it to expand those references.
Try updating your GraphQL query like this:
query ($slug: String) {
sanityJob(slug: {current: {eq: $slug}}) {
title
benefits
konditionen
skills {
_id
title
color
}
}
}The key change is that instead of just skills, you're now accessing skills { } as an object and selecting the fields you want from the referenced jobskill documents.
If that doesn't work, there's an alternative approach using the _raw field that Gatsby provides. This is sometimes necessary when the standard GraphQL resolver has issues:
query ($slug: String) {
sanityJob(slug: {current: {eq: $slug}}) {
title
benefits
konditionen
_rawSkills
}
}The _rawSkills field will give you the raw data structure from Sanity, but you'll need to manually resolve the references in your component code using Sanity's client, or you can use a combination approach where you still get the resolved data through a proper nested query.
Best solution: Make sure your Gatsby schema is properly configured to understand the references. Sometimes you need to rebuild your Gatsby GraphQL schema. Try running:
gatsby clean
gatsby developThis clears the cache and rebuilds the schema, which often fixes reference resolution issues.
The root cause is that gatsby-source-sanity needs to properly transform your Sanity schema into Gatsby's GraphQL schema, and reference types require explicit field selection in your queries rather than treating them as primitive values. Your schema definition looks correct, so it's really just about how you're querying the data in GraphQL!
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.