Skip to content
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

fix(gatsby): better text for missing inferred extension #28530

Merged
merged 5 commits into from
Dec 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions packages/gatsby/src/schema/infer/__tests__/infer.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ jest.mock(`gatsby-cli/lib/reporter`, () => {
const report = require(`gatsby-cli/lib/reporter`)
afterEach(() => {
report.error.mockClear()
report.warn.mockClear()
})

const makeNodes = () => [
Expand Down Expand Up @@ -1385,4 +1386,54 @@ Object {
)
})
})

describe(`missing extension warning`, () => {
it(`warns when inferred extension is missing in type definition`, async () => {
const nodes = [
{
date: `2012-11-01`,
internal: { type: `Test` },
id: `1`,
},
{
linked___NODE: `foo`,
internal: { type: `Test` },
id: `2`,
},

// linked node:
{ id: `foo`, internal: { type: `Foo` } },
]
const typeDefs = [
{
typeOrTypeDef: `
type Test implements Node {
linked: Foo
date: Date
}
`,
},
]
await buildTestSchema(nodes, {}, typeDefs)
expect(report.warn.mock.calls.length).toEqual(2)
expect(report.warn.mock.calls[0][0]).toEqual(
`Deprecation warning: adding inferred extension \`dateformat\` for field \`Test.date\`.\n` +
`In Gatsby v3, only fields with an explicit directive/extension will be resolved correctly.\n` +
`Add the following type definition to fix this:\n\n` +
` type Test implements Node {\n` +
` date: Date @dateformat\n` +
` }\n\n` +
`https://www.gatsbyjs.com/docs/actions/#createTypes`
)
expect(report.warn.mock.calls[1][0]).toEqual(
`Deprecation warning: adding inferred extension \`link\` for field \`Test.linked\`.\n` +
`In Gatsby v3, only fields with an explicit directive/extension will be resolved correctly.\n` +
`Add the following type definition to fix this:\n\n` +
` type Test implements Node {\n` +
` linked: Foo @link(by: "id", from: "linked___NODE")\n` +
` }\n\n` +
`https://www.gatsbyjs.com/docs/actions/#createTypes`
)
})
})
})
20 changes: 16 additions & 4 deletions packages/gatsby/src/schema/infer/add-inferred-fields.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { isFile } from "./is-file"
import { isDate } from "../types/date"
import { addDerivedType } from "../types/derived-types"
import { is32BitInteger } from "../../utils/is-32-bit-integer"
import { printDirectives } from "../print"
const { getNode, getNodes } = require(`../../redux/nodes`)

const addInferredFields = ({
Expand Down Expand Up @@ -120,11 +121,22 @@ const addInferredFieldsImpl = ({
.forEach(name => {
if (!typeComposer.hasFieldExtension(key, name)) {
typeComposer.setFieldExtension(key, name, extensions[name])

const typeName = typeComposer.getTypeName()
const implementsNode =
unsanitizedFieldPath.length === 1 ? `implements Node ` : ``
const extension = printDirectives(
{ [name]: extensions[name] },
schemaComposer.getDirectives()
)
report.warn(
`Deprecation warning - adding inferred resolver for field ` +
`${typeComposer.getTypeName()}.${key}. In Gatsby v3, ` +
`only fields with an explicit directive/extension will ` +
`get a resolver.`
`Deprecation warning: adding inferred extension \`${name}\` for field \`${typeName}.${key}\`.\n` +
`In Gatsby v3, only fields with an explicit directive/extension will be resolved correctly.\n` +
`Add the following type definition to fix this:\n\n` +
` type ${typeComposer.getTypeName()} ${implementsNode}{\n` +
` ${key}: ${field.type.toString()}${extension}\n` +
` }\n\n` +
`https://www.gatsbyjs.com/docs/actions/#createTypes`
)
}
})
Expand Down
1 change: 1 addition & 0 deletions packages/gatsby/src/schema/print.js
Original file line number Diff line number Diff line change
Expand Up @@ -402,4 +402,5 @@ const breakLine = (line, maxLen) => {

module.exports = {
printTypeDefinitions,
printDirectives,
}