@@ -146,6 +146,9 @@ const updateSchemaComposer = async ({
146146 inferenceMetadata,
147147 parentSpan : activity . span ,
148148 } )
149+ addInferredChildOfExtensions ( {
150+ schemaComposer,
151+ } )
149152 activity . end ( )
150153
151154 activity = report . phantomActivity ( `Processing types` , {
@@ -202,11 +205,6 @@ const processTypeComposer = async ({
202205
203206 if ( typeComposer . hasInterface ( `Node` ) ) {
204207 await addNodeInterfaceFields ( { schemaComposer, typeComposer, parentSpan } )
205- await addImplicitConvenienceChildrenFields ( {
206- schemaComposer,
207- typeComposer,
208- parentSpan,
209- } )
210208 }
211209 await determineSearchableFields ( {
212210 schemaComposer,
@@ -999,15 +997,26 @@ const addConvenienceChildrenFields = ({ schemaComposer }) => {
999997 } )
1000998}
1001999
1002- const addImplicitConvenienceChildrenFields = ( {
1003- schemaComposer,
1004- typeComposer,
1005- } ) => {
1000+ const addInferredChildOfExtensions = ( { schemaComposer } ) => {
1001+ schemaComposer . forEach ( typeComposer => {
1002+ if (
1003+ typeComposer instanceof ObjectTypeComposer &&
1004+ typeComposer . hasInterface ( `Node` )
1005+ ) {
1006+ addInferredChildOfExtension ( {
1007+ schemaComposer,
1008+ typeComposer,
1009+ } )
1010+ }
1011+ } )
1012+ }
1013+
1014+ const addInferredChildOfExtension = ( { schemaComposer, typeComposer } ) => {
10061015 const shouldInfer = typeComposer . getExtension ( `infer` )
1007- // In Gatsby v3, when `@dontInfer` is set, children fields will not be
1008- // created for parent-child relations set by plugins with
1016+ // In Gatsby v3, when `@dontInfer` is set, `@childOf` extension will not be
1017+ // automatically created for parent-child relations set by plugins with
10091018 // `createParentChildLink`. With `@dontInfer`, only parent-child
1010- // relations explicitly set with the `childOf` extension will be added.
1019+ // relations explicitly set with the `@ childOf` extension will be added.
10111020 // if (shouldInfer === false) return
10121021
10131022 const parentTypeName = typeComposer . getTypeName ( )
@@ -1017,10 +1026,11 @@ const addImplicitConvenienceChildrenFields = ({
10171026
10181027 Object . keys ( childNodesByType ) . forEach ( typeName => {
10191028 // Adding children fields to types with the `@dontInfer` extension is deprecated
1020- if ( shouldInfer === false ) {
1021- const childTypeComposer = schemaComposer . getAnyTC ( typeName )
1022- const childOfExtension = childTypeComposer . getExtension ( `childOf` )
1029+ const childTypeComposer = schemaComposer . getAnyTC ( typeName )
1030+ let childOfExtension = childTypeComposer . getExtension ( `childOf` )
10231031
1032+ if ( shouldInfer === false ) {
1033+ // Adding children fields to types with the `@dontInfer` extension is deprecated
10241034 // Only warn when the parent-child relation has not been explicitly set with
10251035 if (
10261036 ! childOfExtension ||
@@ -1046,9 +1056,15 @@ const addImplicitConvenienceChildrenFields = ({
10461056 )
10471057 }
10481058 }
1049-
1050- typeComposer . addFields ( createChildrenField ( typeName ) )
1051- typeComposer . addFields ( createChildField ( typeName ) )
1059+ // Set `@childOf` extension automatically
1060+ // This will cause convenience children fields like `childImageSharp`
1061+ // to be added in `addConvenienceChildrenFields` method.
1062+ // Also required for proper printing of the `@childOf` directive in the snapshot plugin
1063+ if ( ! childOfExtension ?. types ) {
1064+ childOfExtension = { types : [ ] }
1065+ }
1066+ childOfExtension . types . push ( parentTypeName )
1067+ childTypeComposer . setExtension ( `childOf` , childOfExtension )
10521068 } )
10531069}
10541070
0 commit comments