Gatsby: [docs] Add `mapping` documentation

Created on 13 Feb 2018  Â·  13Comments  Â·  Source: gatsbyjs/gatsby

good first issue documentation

Most helpful comment

All 13 comments

Hey! I'd love to work on this!

That's great, thanks!

@KyleAMathews mentioned as an example the mapping done directly between nodes in gatsbyjs.org, an author field in markdown is mapped to an authors.yaml file. What exactly can you do once it's mapped, can you give me a bit clearer explanation of what happens in the back?

Also any suggestions on what to include in the documentation? @m-allanson @fk

@fk Thank you so much for such a detailed and clear explanation. This makes everything so much understandable now.

To query between nodes, Gatsby has a mapping feature which allows you to link two different nodes by id and then you can query with GraphQL. For instance, if you have a couple of blog posts which have author id in the frontmatter:

title: A blog post
author: Kyle Mathews

And you have a list of authors and their details stored in authors.yaml, you can map between author in frontmatter to id in authors.yaml file by:

module.exports = {
  mapping: {
    "MarkdownRemark.frontmatter.author": `AuthorYaml`,
  },
}

This enables you to query data from both sources together:

query BlogPost($slug: String!) {
    markdownRemark(fields: {slug: {eq: $slug}}) {
        html
        fields {
            slug
        }
        frontmatter {
            title
            author {
                id
                fields {
                    slug
                }
            }
        }
    }
}

@KyleAMathews @fk @m-allanson I've written up a rough idea of what should be added to the documentation. Any suggestions? I'll go on to making a PR once you review it!

@ajayns Maybe it's worth mentioning that mapping doesn't have to be 1-1 relationship (single blog post has single author) and can be 1-N (single blog post can have multiple authors - then authors field in frontmatter in your example should be array of author ids)? Probably not in introduction to this feature (to not overwhelm with too much information). This can be added at later date, just keep this in mind as this:

allows you to link two different nodes by id

is only "half" of what mapping can do.

I was looking a for a simple way to explain what mapping does. Any suggestions on how I should rephrase it then? @pieh

Not sure really - I already learned that writing docs is hard :) Maybe don't include details about linking 2 nodes by id in introduction and just say that it links nodes. And then add more details for specific scenarios (1-1 and 1-N) in seperate sub sections?

Thanks for chiming in @pieh!

@ajayns I think what @pieh suggested is a good way to split up general and more detailed information. Do you want to get a PR going with what you already did and we continue the discussion over there?

Sure! I'm on it right now! @fk

Done in #4054, thanks @ajayns 🎉

Was this page helpful?
0 / 5 - 0 ratings

Related issues

KyleAMathews picture KyleAMathews  Â·  3Comments

kalinchernev picture kalinchernev  Â·  3Comments

dustinhorton picture dustinhorton  Â·  3Comments

Oppenheimer1 picture Oppenheimer1  Â·  3Comments

andykais picture andykais  Â·  3Comments