"Solving 'related posts' attached to page data at build time in Gatsby with Sanity"
13 replies
Last updated: Oct 20, 2021
J
š Trying to solve ārelated postsā attached to page data at build time.
(Weāre already generating category pages, showing posts referencing the category⦠but really struggling from the other direction.)
The problem is a reference only contains theĀ
https://nimblewebdeveloper.com/blog/gatsby-generate-related-posts-at-build-time
Also posted in the issue queue https://github.com/sanity-io/gatsby-source-sanity/issues/81#issuecomment-930521036
UPDATE:
(Weāre already generating category pages, showing posts referencing the category⦠but really struggling from the other direction.)
The problem is a reference only contains theĀ
_key,Ā
_ref, andĀ
_typeĀ ...not the
_idThis write-up ALMOST works, but must be outdatedā¦
https://nimblewebdeveloper.com/blog/gatsby-generate-related-posts-at-build-time
Also posted in the issue queue https://github.com/sanity-io/gatsby-source-sanity/issues/81#issuecomment-930521036
UPDATE:
_rawDataCategoriesis the magic detail.
Sep 29, 2021, 9:42 PM
Hi (again) Josh! I might just be misunderstanding you so perhaps a bit more detail would help, but when you say āa reference only contains theĀ
_key,Ā
_ref, andĀ
_typeĀ ...not theĀ
_id,ā I want to clarify that the
_refvalue is the
_idof the target document.
Sep 29, 2021, 10:12 PM
J
Not the case for me. š The
Example: tag id
Iāve tried using both
_reffield is even a signed hash.
Example: tag id
f9701e5b-3434-4c8d-9280-52b2a1dfea87comes back as
_ref: '-468e50a2-1a86-5b51-b5a6-78e87ea4fc32'
Iāve tried using both
createSchemaCustomizationand
createResolversto attach the query.
Sep 29, 2021, 10:41 PM
J
gatsby-source-sanity7.0.2
Sep 29, 2021, 10:44 PM
J
(GraphQL)
Sep 29, 2021, 10:51 PM
J
šŖāØ You must use
_rawDataCategoriesto get the
idvia
_refotherwise itās that reference hash instead.
Sep 30, 2021, 12:52 AM
J
I should probably make a snippet out of this.
Sep 30, 2021, 12:52 AM
A
hey
user Q
I was looking into the same article you shared above and didnāt manage to make it work. keep getting an empty array for related. I also triple check that I dont have any
_rawDataCategoriesto get the
idvia
ref. Any pointer? ššāāļø
Oct 18, 2021, 3:09 PM
J
exports.createSchemaCustomization = ({ actions, schema }) => { actions.createTypes([ schema.buildObjectType({ interfaces: ["Node"], name: "SanityPost", fields: { relatedPosts: { type: "[SanityPost]", resolve: async (source, args, context, info) => { const categories = source._rawDataCategories ? source._rawDataCategories.map((c) => c._ref) : []; if (!categories.length) return []; const posts = await context.nodeModel.runQuery({ query: { filter: { categories: { elemMatch: { _id: { in: categories }, }, }, // exclude current node _id: { ne: source._id }, }, // no way to limit results in runQuery // see: <https://github.com/gatsbyjs/gatsby/issues/15453> }, type: "SanityPost", }); return posts && posts.length > 0 ? posts : []; }, }, }, }) ]); };
Oct 18, 2021, 5:31 PM
J
Then be sure to put
relatedPostsinto your page query as if it was another field (with subfields of course).
Oct 18, 2021, 5:33 PM
J
For clarity, that goes in
gatsby-node.jsand youāll probably want this tooā¦
sort: { fields: ["publishedAt"], order: ["DESC"], },
Oct 18, 2021, 8:50 PM
J
For clarity, that goes in
gatsby-node.jsand youāll probably want this tooā¦
sort: { fields: ["publishedAt"], order: ["DESC"], },
Oct 18, 2021, 8:50 PM
A
THANK YOU SO MUCH THIS WORKS LIKE A CHARM!
user Q
Oct 20, 2021, 3:30 PM
J
Added as a snippet⦠https://www.sanity.io/schemas/related-content-9079f69e
Oct 20, 2021, 6:34 PM
Sanityā build remarkable experiences at scale
The Sanity Composable Content Cloud is the headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.