Skip to content

Instantly share code, notes, and snippets.

@coreyward
Created August 6, 2020 20:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save coreyward/f09163635341995e4f51e72f5f7b7e59 to your computer and use it in GitHub Desktop.
Save coreyward/f09163635341995e4f51e72f5f7b7e59 to your computer and use it in GitHub Desktop.
GatsbyJS with Sanity: Example of adding additional `_raw` fields where needed
const sourceRawField = fieldName => ({
type: "JSON",
resolve: (source, args, context, info) => {
const data = source[fieldName]
// Resolve top-level references in Portable Text
if (data && Array.isArray(data)) {
return data.map(block =>
block._type === "reference"
? {
_key: block._key,
...context.nodeModel.getNodeById({ id: block._ref }),
}
: block
)
}
return data
},
})
exports.createResolvers = ({ createResolvers }) => {
createResolvers({
// Repeat this for each type needed
SanityBlogPost: {
_rawContent: sourceRawField("content"),
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment