Skip to content

Using createSchemaCustomization in gatsby-node to define reverse mapping defined in gatsby-config #25077

@mbwatson

Description

@mbwatson

Summary

I opened issue #16736 last year, and now I've come back to this project. The solution I apparently found back then (illustrated above) doesn't seem to work now. I wonder if createSchemaCustomization has changed since then, or (more likely) what simple thing I've missed.

I've created a minimal example to illustrate this issue: https://github.com/mbwatson/gatsby-links.

Relevant information

This example has two data types--people and teams--sourced from YAML files in the ./data directory. To summarize, the aim is to see the members of a team when a team is queried, and (2) to see the teams to which a person belongs when a person is queried.

People files look like the following:

id: alice-albertson
name: Alice Albertson
bio: >
  Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
  tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
  quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
  consequat.

Teams YAML files look like this:

id: team-awesome
name: Team Awesome
description: >
  Lorem ipsum dolor sit amet, consectetur adipisicing elit.
  Temporibus ipsam provident quasi soluta ex fugit, asperiores
  itaque voluptas. Cupiditate nisi enim eius rem tempore
  debitis eum impedit fugiat nostrum magnam.
members:
  - alice-albertson
  - carla-cranston

Mapping

Teams

Using mapping in gatsby-config.js I can see members as people nodes when querying teams:

Query

{
  allTeamsYaml {
    edges {
      node {
        id
        name
        members {
          id
          name
        }
      }
    }
  }
}

Result

{
  "data": {
    "allTeamsYaml": {
      "edges": [
        {
          "node": {
            "id": "team-awesome",
            "name": "Team Awesome",
            "members": [
              {
                "id": "alice-albertson",
                "name": "Alice Albertson"
              },
              {
                "id": "carla-cranston",
                "name": "Carla Cranston"
              }
            ]
          }
        },
        {
          "node": {
            "id": "team-mediocre",
            "name": "Team Mediocre",
            "members": [
              {
                "id": "alice-albertson",
                "name": "Alice Albertson"
              },
              {
                "id": "bob-bobberson",
                "name": "Robert Bobberson"
              }
            ]
          }
        }
      ]
    }
  }
}

People

However, I can't seem to see the teams when querying a person. My thinking was to add

exports.createSchemaCustomization = ({ actions }) => {
    const { createTypes } = actions
    const typeDefs = `
        type PeopleYaml implements Node {
            teams: [TeamsYaml] @link(by: "members.id", from: "id")
        }
    `
    createTypes(typeDefs)
}

to gatsby-node.js, but the results have null for teams.

Query

{
  allPeopleYaml {
    edges {
      node {
        id
        name
        teams {
          id
        }
      }
    }
  }
}

Result

{
  "data": {
    "allPeopleYaml": {
      "edges": [
        {
          "node": {
            "id": "alice-albertson",
            "name": "Alice Albertson",
            "teams": null
          }
        },
        {
          "node": {
            "id": "bob-bobberson",
            "name": "Robert Bobberson",
            "teams": null
          }
        },
        {
          "node": {
            "id": "carla-cranston",
            "name": "Carla Cranston",
            "teams": null
          }
        }
      ]
    }
  }
}

Am I doing something silly here? Any help resolving this would be greatly appreciated!

Relevant file contents

gatsby-config.js:

module.exports = {
  siteMetadata: {
    title: `Gatsby Default Starter`,
    description: `Kick off your next, great Gatsby project with this default starter. This barebones starter ships with the main Gatsby configuration files you might need.`,
    author: `@gatsbyjs`,
  },
  plugins: [
    `gatsby-plugin-react-helmet`,
    {
      resolve: `gatsby-source-filesystem`,
      options: {
        name: `images`,
        path: `${__dirname}/src/images`,
      },
    },
    `gatsby-transformer-sharp`,
    `gatsby-plugin-sharp`,
    {
      resolve: `gatsby-plugin-manifest`,
      options: {
        name: `gatsby-starter-default`,
        short_name: `starter`,
        start_url: `/`,
        background_color: `#663399`,
        theme_color: `#663399`,
        display: `minimal-ui`,
        icon: `src/images/gatsby-icon.png`, // This path is relative to the root of the site.
      },
    },
    `gatsby-transformer-yaml`,
    {
        resolve: `gatsby-source-filesystem`,
        options: {
            path: `./src/data/`,
        },
    },
    // this (optional) plugin enables Progressive Web App + Offline functionality
    // To learn more, visit: https://gatsby.dev/offline
    // `gatsby-plugin-offline`,
  ],
  mapping: {
      "TeamsYaml.members": "PeopleYaml",
  },
}

gatsby-node.js:

exports.createSchemaCustomization = ({ actions }) => {
    const { createTypes } = actions
    const typeDefs = `
        type PeopleYaml implements Node {
            teams: [TeamsYaml] @link(by: "members.id", from: "id")
        }
    `
    createTypes(typeDefs)
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions