Skip to content

Reverse Mapping #16736

@mbwatson

Description

@mbwatson

Summary

I'm using gatsby-transformer-yaml to manage people and teams. I'm trying to link these so that GraphQL queries for teams return member details, and vice versa. It's a many-to-many situation, and I'm not sure if it's possible or if (more likely) I'm doing something silly

Returning person detail in a query for teams is straight-forward enough by adding. However, the reverse--returning the groups of which people are members--is not quite working for me.

Relevant information

YAML files in the src/data/people directory look like this:

id: username123
name: John Doe
email: johndoe@example.net
title: CEO

and those in src/data/teams look like this

id: awesome
name: Team Awesome
description: Lorem ipsum dolor sit amet, consectetur adipisicing elit. Sint, ipsum!
members:
  - username123
  - otheruser
  - anotherone

Returning detail about people in a query for teams is accomplished by adding

mapping: {
    "TeamsYaml.members": `PeopleYaml`,
},

to my gatsby-config.js, so that the query

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

returns

{
  "data": {
    "allTeamsYaml": {
      "edges": [
        {
          "node": {
            "name": "Team Awesome",
            "members": [
              {
                "id": "username123",
                "name": "John Doe"
              },
              {
                "id": "otheruser",
                "name": "Jane Doe"
              },
              {
                "id": "anotherone",
                "name": "Jo Doe"
              }
            ]
          }
        }
      ]
    }
  }
}

as desired.

However, the reverse--returning the groups of which people are members--is not as clear.

I've tried two things:

  1. Another Mapping
mapping: {
    "TeamsYaml.members": `PeopleYaml`,
    "PeopleYaml.teams": `TeamsYaml`, // NEW
},

That new addition throws "Cannot query field "teams" on type "PeopleYaml"".

  1. Schema Customization

Per https://www.gatsbyjs.org/docs/schema-customization/, I thought this gatsby-node.js

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

would link the ids of people to those found in the members array in the team YAML file, but this intuition was incorrect here, however I do get something closer to the desired result.

The query

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

returns nulls:

{
  "data": {
    "allPeopleYaml": {
      "edges": [
        {
          "node": {
            "name": "John Doe
            "teams": null
          }
        },
        {
          "node": {
            "name": "Jane Doe"
            "teams": null
          }
        },
        {
          "node": {
            "name": "Jo Doe",
            "teams": null
          }
        }
      ]
    }
  }
}

However, this must be close because in the Explorer, the team fields are all available:

Screenshot from 2019-08-22 13-58-57

I don't see anything quite like this in the docs or in other existing issues, and any help would be appreciated in getting this reverse mapping working.

File contents (if changed)

gatsby-config.js: Various, as shown above.
package.json: N/A
gatsby-node.js: Various, as shown above.
gatsby-browser.js: N/A
gatsby-ssr.js: N/A

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions