@@ -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,42 @@ const addConvenienceChildrenFields = ({ schemaComposer }) => {
999997 } )
1000998}
1001999
1002- const addImplicitConvenienceChildrenFields = ( {
1003- schemaComposer,
1004- typeComposer,
1005- } ) => {
1000+ const isExplicitChild = ( { typeComposer, childTypeComposer } ) => {
1001+ if ( ! childTypeComposer . hasExtension ( `childOf` ) ) {
1002+ return false
1003+ }
1004+ const childOfExtension = childTypeComposer . getExtension ( `childOf` )
1005+ const { types : parentMimeTypes = [ ] } =
1006+ typeComposer . getExtension ( `mimeTypes` ) ?? { }
1007+
1008+ return (
1009+ childOfExtension ?. types ?. includes ( typeComposer . getTypeName ( ) ) ||
1010+ childOfExtension ?. mimeTypes ?. some ( mimeType =>
1011+ parentMimeTypes . includes ( mimeType )
1012+ )
1013+ )
1014+ }
1015+
1016+ const addInferredChildOfExtensions = ( { schemaComposer } ) => {
1017+ schemaComposer . forEach ( typeComposer => {
1018+ if (
1019+ typeComposer instanceof ObjectTypeComposer &&
1020+ typeComposer . hasInterface ( `Node` )
1021+ ) {
1022+ addInferredChildOfExtension ( {
1023+ schemaComposer,
1024+ typeComposer,
1025+ } )
1026+ }
1027+ } )
1028+ }
1029+
1030+ const addInferredChildOfExtension = ( { schemaComposer, typeComposer } ) => {
10061031 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
1032+ // In Gatsby v3, when `@dontInfer` is set, `@childOf` extension will not be
1033+ // automatically created for parent-child relations set by plugins with
10091034 // `createParentChildLink`. With `@dontInfer`, only parent-child
1010- // relations explicitly set with the `childOf` extension will be added.
1035+ // relations explicitly set with the `@ childOf` extension will be added.
10111036 // if (shouldInfer === false) return
10121037
10131038 const parentTypeName = typeComposer . getTypeName ( )
@@ -1016,39 +1041,46 @@ const addImplicitConvenienceChildrenFields = ({
10161041 const childNodesByType = groupChildNodesByType ( { nodes } )
10171042
10181043 Object . keys ( childNodesByType ) . forEach ( typeName => {
1019- // 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` )
1044+ const childTypeComposer = schemaComposer . getAnyTC ( typeName )
1045+ let childOfExtension = childTypeComposer . getExtension ( `childOf` )
10231046
1024- // Only warn when the parent-child relation has not been explicitly set with
1025- if (
1026- ! childOfExtension ||
1027- ! childOfExtension . types . includes ( parentTypeName )
1028- ) {
1029- const childField = fieldNames . convenienceChild ( typeName )
1030- const childrenField = fieldNames . convenienceChildren ( typeName )
1031- const childOfTypes = ( childOfExtension ?. types ?? [ ] )
1032- . concat ( parentTypeName )
1033- . map ( name => `"${ name } "` )
1034- . join ( `,` )
1035-
1036- report . warn (
1037- `Deprecation warning: ` +
1038- `In Gatsby v3 fields \`${ parentTypeName } .${ childField } \` and \`${ parentTypeName } .${ childrenField } \` ` +
1039- `will not be added automatically because ` +
1040- `type \`${ typeName } \` does not explicitly list type \`${ parentTypeName } \` in \`childOf\` extension.\n` +
1041- `Add the following type definition to fix this:\n\n` +
1042- ` type ${ typeName } implements Node @childOf(types: [${ childOfTypes } ]) {\n` +
1043- ` id: ID!\n` +
1044- ` }\n\n` +
1045- `https://www.gatsbyjs.com/docs/actions/#createTypes`
1046- )
1047- }
1047+ if ( isExplicitChild ( { typeComposer, childTypeComposer } ) ) {
1048+ return
10481049 }
1050+ if ( shouldInfer === false ) {
1051+ // Adding children fields to types with the `@dontInfer` extension is deprecated
1052+ // Only warn when the parent-child relation has not been explicitly set with `childOf` directive
1053+ const childField = fieldNames . convenienceChild ( typeName )
1054+ const childrenField = fieldNames . convenienceChildren ( typeName )
1055+ const childOfTypes = ( childOfExtension ?. types ?? [ ] )
1056+ . concat ( parentTypeName )
1057+ . map ( name => `"${ name } "` )
1058+ . join ( `,` )
10491059
1050- typeComposer . addFields ( createChildrenField ( typeName ) )
1051- typeComposer . addFields ( createChildField ( typeName ) )
1060+ report . warn (
1061+ `Deprecation warning: ` +
1062+ `In Gatsby v3 fields \`${ parentTypeName } .${ childField } \` and \`${ parentTypeName } .${ childrenField } \` ` +
1063+ `will not be added automatically because ` +
1064+ `type \`${ typeName } \` does not explicitly list type \`${ parentTypeName } \` in \`childOf\` extension.\n` +
1065+ `Add the following type definition to fix this:\n\n` +
1066+ ` type ${ typeName } implements Node @childOf(types: [${ childOfTypes } ]) {\n` +
1067+ ` id: ID!\n` +
1068+ ` }\n\n` +
1069+ `https://www.gatsbyjs.com/docs/actions/#createTypes`
1070+ )
1071+ }
1072+ // Set `@childOf` extension automatically
1073+ // This will cause convenience children fields like `childImageSharp`
1074+ // to be added in `addConvenienceChildrenFields` method.
1075+ // Also required for proper printing of the `@childOf` directive in the snapshot plugin
1076+ if ( ! childOfExtension ) {
1077+ childOfExtension = { }
1078+ }
1079+ if ( ! childOfExtension . types ) {
1080+ childOfExtension . types = [ ]
1081+ }
1082+ childOfExtension . types . push ( parentTypeName )
1083+ childTypeComposer . setExtension ( `childOf` , childOfExtension )
10521084 } )
10531085}
10541086
0 commit comments