How to build GraphQL types on top of gatsby-transformer-remark?May 5, 2020 at 1:19pm (Edited 2 months ago)
I am looking to manipulate the frontmatter of
MarkdownRemarknodes and create contextualized GraphQL types from them. My goal is to inherit the work gatsby-transformer-remark and associated plugins have performed on nodes, and then build on top of them. One example might be to process markdown within frontmatter properties. An added benefit of this is that the fields available in frontmatter would be specific to a set of markdown files, rather than having one generic
I currently have a working example here, but it feels problematic for two reasons:
- The processing that gatsby-transformer-remark does after the
onNodeCreatehook is not available on the object. For example, the
htmlfield won't exist on my inherited type.
- I can overcome the point above by setting the
mediaTypeto that of the sourced file node, but then I can only get to the
childMarkdownRemark. That's not a huge problem, but it's indicative of a bigger problem, which is that I've just create another MarkdownRemark node on top of the node that was already processed, leading to duplicated nodes, and that feels like a major performance issue.
I've considered another approach that is to explicitly define the fields I want to add in the GraphQL schema. The problem with this approach is that I would have to know the type name for every object I want to manipulate, and that feels like it could be brittle. e.g. a frontmatter path to something like
sidebar.content.featureswould be of type
MarkdownRemarkFrontmatterSidebarContentFeature(or something like that). It feels like I'd be rewriting logic that already exists with Gatsby.
I've tried implicitly adding these fields by hooking into
MarkdownRemarknodes, and adding properties to the node. That works when the server first boots up (and on build), but it doesn't work when changing markdown files on the fly (which is a requirement for me).
I'm curious what the community would consider best practice in approaching this problem?