Skip to content

Commit 4d7f4b3

Browse files
committed
fix(gatsby): print childOf directive for implicit child fields
1 parent 3727947 commit 4d7f4b3

File tree

3 files changed

+61
-20
lines changed

3 files changed

+61
-20
lines changed

packages/gatsby/src/schema/__tests__/__snapshots__/print.js.snap

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,10 @@ input Baz {
120120
qux: Boolean
121121
}
122122
123+
type FooChild implements Node @childOf(types: [\\"Test\\"]) @dontInfer {
124+
bar: String
125+
}
126+
123127
type Test implements Node @dontInfer {
124128
foo: Int
125129
}"
@@ -266,6 +270,10 @@ type OneMoreTest implements Node @dontInfer {
266270
267271
union ThisOrThat = AnotherTest | OneMoreTest
268272
273+
type FooChild implements Node @childOf(types: [\\"Test\\"]) @dontInfer {
274+
bar: String
275+
}
276+
269277
type Test implements Node @dontInfer {
270278
foo: Int
271279
}"
@@ -423,6 +431,10 @@ input Baz {
423431
qux: Boolean
424432
}
425433
434+
type FooChild implements Node @childOf(types: [\\"Test\\"]) @dontInfer {
435+
bar: String
436+
}
437+
426438
type Test implements Node @dontInfer {
427439
foo: Int
428440
}"

packages/gatsby/src/schema/__tests__/print.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ const { build } = require(`..`)
22
import { buildObjectType } from "../types/type-builders"
33
const { store } = require(`../../redux`)
44
const { actions } = require(`../../redux/actions/restricted`)
5+
const { actions: publicActions } = require(`../../redux/actions/public`)
6+
const { createParentChildLink } = publicActions
57
const { printTypeDefinitions } = actions
68

79
jest.mock(`fs-extra`)
@@ -41,14 +43,25 @@ jest.spyOn(global.Date.prototype, `toISOString`).mockReturnValue(`2019-01-01`)
4143
describe(`Print type definitions`, () => {
4244
beforeEach(() => {
4345
store.dispatch({ type: `DELETE_CACHE` })
44-
const node = {
46+
const node1 = {
4547
id: `test1`,
4648
internal: {
4749
type: `Test`,
4850
},
51+
children: [],
4952
foo: 26,
5053
}
51-
store.dispatch({ type: `CREATE_NODE`, payload: { ...node } })
54+
const node2 = {
55+
id: `test2`,
56+
parent: `test1`,
57+
internal: {
58+
type: `FooChild`,
59+
},
60+
bar: `bar`,
61+
}
62+
store.dispatch({ type: `CREATE_NODE`, payload: { ...node1 } })
63+
store.dispatch({ type: `CREATE_NODE`, payload: { ...node2 } })
64+
createParentChildLink({ parent: node1, child: node2 })
5265
const typeDefs = []
5366
typeDefs.push(`
5467
type AnotherTest implements Node & ITest {

packages/gatsby/src/schema/schema.js

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)