Replies: 3 comments
-
I think you look for an many-to-many relationship Remove the members from team yaml and a new relationship yaml with items of: -
team_id: team-awesome
member_id: alice-albertson
role_type: Leader
-
team_id: team-awesome
member_id: bob-bobberson
role_type: Secretary |
Beta Was this translation helpful? Give feedback.
-
I am not sure but maybe you can do this in the team yaml with this syntax: members:
-
id: alice-albertson
role: Leader
-
id: bob-bobberson
role: Secretary |
Beta Was this translation helpful? Give feedback.
-
thanks for your reply! before i saw your response, i worked out a non-built-in solution in exports.createResolvers = ({ actions, createResolvers }) => {
// ...
const resolvers = {
TeamsYaml: {
members: {
type: ["PeopleYaml"],
resolve(source, args, context, info) {
if (!source.members) { return [] }
const memberIds = source.members.map(member => member.id)
// console.log(source.members.map(({ id, role }) => `${ id } (${ role })`))
return context.nodeModel.getNodesByIds({ ids: memberIds })
.map(node => {
const index = source.members.findIndex(member => member.id === node.id)
if (index === -1) {
return node
}
return ({ ...node, role: source.members[index].role })
})
}
},
},
// ...
}
createResolvers(resolvers)
} this gives me exactly what i need. what obvious problems might i be facing in the future if i leave it with the solution i've implemented? possibly a performance hit at build-time? |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Preliminaries
I have received excellent assistance from the Gatsby community in the past, and I'm hoping this is another situation that someone can easily resolve for me. Suppose I have two types of data--
people
andteams
--in YAML files in my'./data
directory.People files look like the following:
Teams YAML files look like this:
Then I can grab teams and their members with a query like this:
which results in data like this:
Perfect! I can also do the reverse mapping--that is, getting all teams to which people belong. These are all achieved through mapping definitions in
gatsby-config.js
andcustomSchemaCustomization
&createResolvers
ingatsby-node.js
. Details for accomplishing this can be found in this issue #25077 I asked earlier this year.Question
Now, I need to do something new: Each person on each team needs a role, e.g., "leader." People possibly have different roles on each team, thus I would like to format my
team
data files like the following:This doesn't mesh with my mapping and createschemaCustomization/createResolvers solution described above, as my goal is to have each member's role returned along with their other details. That is, I'd like the results returned from a query like this:
to give data that looks like this:
I would appreciate any guidance toward achieving this functionality. This is starting to make me wonder if I should move to a relational database, as this feels like a simple JOIN would take care of, but I bet this is a common situation people have solved with GraphQL.
Beta Was this translation helpful? Give feedback.
All reactions