Skip to content

Commit

Permalink
fix(gatsby): Don't show deprecation warning when adding childr… (#16559)
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanprobst authored and sidharthachatterjee committed Aug 12, 2019
1 parent 8b39af2 commit 5c7a3e1
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ describe(`Define parent-child relationships with field extensions`, () => {
type: `Child`,
contentDigest: `Child1`,
},
parent: [`parent1`],
parent: `parent1`,
children: [],
name: `Child 1`,
},
Expand All @@ -68,7 +68,7 @@ describe(`Define parent-child relationships with field extensions`, () => {
type: `Child`,
contentDigest: `Child2`,
},
parent: [`parent2`],
parent: `parent2`,
children: [],
name: `Child 2`,
},
Expand All @@ -78,7 +78,7 @@ describe(`Define parent-child relationships with field extensions`, () => {
type: `AnotherChild`,
contentDigest: `AnotherChild1`,
},
parent: [`parent1`],
parent: `parent1`,
children: [],
name: `Another Child 1`,
},
Expand All @@ -88,7 +88,7 @@ describe(`Define parent-child relationships with field extensions`, () => {
type: `AnotherChild`,
contentDigest: `AnotherChild2`,
},
parent: [`parent1`],
parent: `parent1`,
children: [],
name: `Another Child 2`,
},
Expand Down Expand Up @@ -160,9 +160,13 @@ describe(`Define parent-child relationships with field extensions`, () => {
type Child implements Node {
id: ID!
}
type AnotherChild implements Node @childOf(types: ["Parent"], many: true) {
id: ID!
}
`)
)
await buildSchema()
expect(report.warn).toBeCalledTimes(1)
expect(report.warn).toBeCalledWith(
`On types with the \`@dontInfer\` directive, or with the \`infer\` ` +
`extension set to \`false\`, automatically adding fields for ` +
Expand Down
6 changes: 3 additions & 3 deletions packages/gatsby/src/schema/extensions/__tests__/interfaces.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ describe(`Queryable Node interfaces`, () => {
date: {
type: `Date`,
extensions: {
dateformat: true,
dateformat: {},
},
},
},
Expand All @@ -154,7 +154,7 @@ describe(`Queryable Node interfaces`, () => {
date: {
type: `Date`,
extensions: {
dateformat: true,
dateformat: {},
},
},
},
Expand All @@ -167,7 +167,7 @@ describe(`Queryable Node interfaces`, () => {
date: {
type: `Date`,
extensions: {
dateformat: true,
dateformat: {},
},
},
},
Expand Down
24 changes: 17 additions & 7 deletions packages/gatsby/src/schema/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -811,7 +811,8 @@ const addImplicitConvenienceChildrenFields = ({
// relations explicitly set with the `childOf` extension will be added.
// if (shouldInfer === false) return

const nodes = nodeStore.getNodesByType(typeComposer.getTypeName())
const parentTypeName = typeComposer.getTypeName()
const nodes = nodeStore.getNodesByType(parentTypeName)

const childNodesByType = groupChildNodesByType({ nodeStore, nodes })

Expand All @@ -824,17 +825,26 @@ const addImplicitConvenienceChildrenFields = ({

// Adding children fields to types with the `@dontInfer` extension is deprecated
if (shouldInfer === false) {
const fieldName = _.camelCase(
`${maxChildCount > 1 ? `children` : `child`} ${typeName}`
)
if (!typeComposer.hasField(fieldName)) {
const childTypeComposer = schemaComposer.getAnyTC(typeName)
const childOfExtension = childTypeComposer.getExtension(`childOf`)
const many = maxChildCount > 1

// Only warn when the parent-child relation has not been explicitly set with
if (
!childOfExtension ||
!childOfExtension.types.includes(parentTypeName) ||
!childOfExtension.many === many
) {
const fieldName = _.camelCase(
`${many ? `children` : `child`} ${typeName}`
)
report.warn(
`On types with the \`@dontInfer\` directive, or with the \`infer\` ` +
`extension set to \`false\`, automatically adding fields for ` +
`children types is deprecated.\n` +
`In Gatsby v3, only children fields explicitly set with the ` +
`\`childOf\` extension will be added.\n` +
`For example, in Gatsby v3, \`${typeComposer.getTypeName()}\` will ` +
`For example, in Gatsby v3, \`${parentTypeName}\` will ` +
`not get a \`${fieldName}\` field.`
)
}
Expand Down Expand Up @@ -931,7 +941,7 @@ const addTypeToRootQuery = ({ schemaComposer, typeComposer }) => {
},
resolve: findManyPaginated(typeName),
},
}).makeFieldNonNull([queryNamePlural])
}).makeFieldNonNull(queryNamePlural)
}

const parseTypes = ({
Expand Down

0 comments on commit 5c7a3e1

Please sign in to comment.