-
Notifications
You must be signed in to change notification settings - Fork 10.3k
fix(gatsby): print childOf directive for implicit child fields #28483
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
Changes from 2 commits
4d7f4b3
2448399
3ebfae2
e139486
cd92d1b
6a7a7b2
a66e13e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -146,6 +146,9 @@ const updateSchemaComposer = async ({ | |||||||||
| inferenceMetadata, | ||||||||||
| parentSpan: activity.span, | ||||||||||
| }) | ||||||||||
| addInferredChildOfExtensions({ | ||||||||||
| schemaComposer, | ||||||||||
| }) | ||||||||||
| activity.end() | ||||||||||
|
|
||||||||||
| activity = report.phantomActivity(`Processing types`, { | ||||||||||
|
|
@@ -202,11 +205,6 @@ const processTypeComposer = async ({ | |||||||||
|
|
||||||||||
| if (typeComposer.hasInterface(`Node`)) { | ||||||||||
| await addNodeInterfaceFields({ schemaComposer, typeComposer, parentSpan }) | ||||||||||
| await addImplicitConvenienceChildrenFields({ | ||||||||||
| schemaComposer, | ||||||||||
| typeComposer, | ||||||||||
| parentSpan, | ||||||||||
| }) | ||||||||||
| } | ||||||||||
| await determineSearchableFields({ | ||||||||||
| schemaComposer, | ||||||||||
|
|
@@ -999,15 +997,26 @@ const addConvenienceChildrenFields = ({ schemaComposer }) => { | |||||||||
| }) | ||||||||||
| } | ||||||||||
|
|
||||||||||
| const addImplicitConvenienceChildrenFields = ({ | ||||||||||
| schemaComposer, | ||||||||||
| typeComposer, | ||||||||||
| }) => { | ||||||||||
| const addInferredChildOfExtensions = ({ schemaComposer }) => { | ||||||||||
| schemaComposer.forEach(typeComposer => { | ||||||||||
| if ( | ||||||||||
| typeComposer instanceof ObjectTypeComposer && | ||||||||||
| typeComposer.hasInterface(`Node`) | ||||||||||
| ) { | ||||||||||
| addInferredChildOfExtension({ | ||||||||||
| schemaComposer, | ||||||||||
| typeComposer, | ||||||||||
| }) | ||||||||||
| } | ||||||||||
| }) | ||||||||||
| } | ||||||||||
|
|
||||||||||
| const addInferredChildOfExtension = ({ schemaComposer, typeComposer }) => { | ||||||||||
| const shouldInfer = typeComposer.getExtension(`infer`) | ||||||||||
| // In Gatsby v3, when `@dontInfer` is set, children fields will not be | ||||||||||
| // created for parent-child relations set by plugins with | ||||||||||
| // In Gatsby v3, when `@dontInfer` is set, `@childOf` extension will not be | ||||||||||
| // automatically created for parent-child relations set by plugins with | ||||||||||
| // `createParentChildLink`. With `@dontInfer`, only parent-child | ||||||||||
| // relations explicitly set with the `childOf` extension will be added. | ||||||||||
| // relations explicitly set with the `@childOf` extension will be added. | ||||||||||
| // if (shouldInfer === false) return | ||||||||||
|
|
||||||||||
| const parentTypeName = typeComposer.getTypeName() | ||||||||||
|
|
@@ -1016,12 +1025,12 @@ const addImplicitConvenienceChildrenFields = ({ | |||||||||
| const childNodesByType = groupChildNodesByType({ nodes }) | ||||||||||
|
|
||||||||||
| Object.keys(childNodesByType).forEach(typeName => { | ||||||||||
| // Adding children fields to types with the `@dontInfer` extension is deprecated | ||||||||||
| if (shouldInfer === false) { | ||||||||||
| const childTypeComposer = schemaComposer.getAnyTC(typeName) | ||||||||||
| const childOfExtension = childTypeComposer.getExtension(`childOf`) | ||||||||||
| const childTypeComposer = schemaComposer.getAnyTC(typeName) | ||||||||||
| let childOfExtension = childTypeComposer.getExtension(`childOf`) | ||||||||||
|
|
||||||||||
| // Only warn when the parent-child relation has not been explicitly set with | ||||||||||
| if (shouldInfer === false) { | ||||||||||
| // Adding children fields to types with the `@dontInfer` extension is deprecated | ||||||||||
| // Only warn when the parent-child relation has not been explicitly set with `childOf` directive | ||||||||||
| if ( | ||||||||||
| !childOfExtension || | ||||||||||
| !childOfExtension.types.includes(parentTypeName) | ||||||||||
|
|
@@ -1046,9 +1055,15 @@ const addImplicitConvenienceChildrenFields = ({ | |||||||||
| ) | ||||||||||
| } | ||||||||||
| } | ||||||||||
|
|
||||||||||
| typeComposer.addFields(createChildrenField(typeName)) | ||||||||||
| typeComposer.addFields(createChildField(typeName)) | ||||||||||
| // Set `@childOf` extension automatically | ||||||||||
| // This will cause convenience children fields like `childImageSharp` | ||||||||||
| // to be added in `addConvenienceChildrenFields` method. | ||||||||||
| // Also required for proper printing of the `@childOf` directive in the snapshot plugin | ||||||||||
| if (!childOfExtension?.types) { | ||||||||||
| childOfExtension = { types: [] } | ||||||||||
| } | ||||||||||
| childOfExtension.types.push(parentTypeName) | ||||||||||
| childTypeComposer.setExtension(`childOf`, childOfExtension) | ||||||||||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The essence of this PR: instead of adding fields directly we add the gatsby/packages/gatsby/src/schema/schema.js Lines 972 to 975 in 76a3b57
|
||||||||||
| }) | ||||||||||
| } | ||||||||||
|
|
||||||||||
|
|
||||||||||
Uh oh!
There was an error while loading. Please reload this page.