Created
August 6, 2020 20:21
-
-
Save coreyward/f09163635341995e4f51e72f5f7b7e59 to your computer and use it in GitHub Desktop.
GatsbyJS with Sanity: Example of adding additional `_raw` fields where needed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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