-
Notifications
You must be signed in to change notification settings - Fork 10.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(gatsby): always add both childField
and childrenField
in GraphQL
#28656
Conversation
} | ||
children.forEach(child => { | ||
typeComposer.addFields(createChildrenField(child.getTypeName())) | ||
typeComposer.addFields(createChildField(child.getTypeName())) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's basically the essence of this PR. Other changed files are just tests/docs updates.
} | ||
children.forEach(child => { | ||
typeComposer.addFields(createChildrenField(child.getTypeName())) | ||
typeComposer.addFields(createChildField(child.getTypeName())) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same change, just in the different codepath
typeComposer.addFields(createChildField(typeName)) | ||
} | ||
typeComposer.addFields(createChildrenField(typeName)) | ||
typeComposer.addFields(createChildField(typeName)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And one more entry. Other changes are mostly chores and don't have any effect on behavior.
Just to leave papar trail for the future from our offline chat:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you!
…hQL (gatsbyjs#28656) * fix(gatsby): always add both `childField` and `childrenField` in GraphQL * Update docs * fix null reference * Update tests * TODO item to remove `many` arg in `v3` * Update snapshot * add description to convenience child fields * update description * Revert docs for now
Description
Before this PR we create only one of the child fields depending on the number of elements in the node
children
array.E.g. when
children
array contains 1 item we create this field:But when
children
array contains 2+ items, we create this field:The problem is that it relies on inference. And if at some point in time you had a single child node and then suddenly added a second child node - your schema will change from
childBar
tochildrenBar
and your queries will break.With this PR we always add both fields instead:
In this case,
childBar
will always return the first child from the array of node children. And then it is up to users to pick the most appropriate field for their queries.This also deprecates the
many
argument of thechildOf
directive 1This is a non-breaking change. Also required to unblock #28483
Credits for this idea go to @pieh
Documentation
Related Issues
#28483
Footnotes
https://www.gatsbyjs.com/docs/reference/graphql-data-layer/schema-customization/#defining-child-relations ↩ ↩2
https://www.gatsbyjs.com/docs/schema-inference/#childparent-fields ↩
https://www.gatsbyjs.com/docs/schema-generation/#4-parent--children-relationships ↩