Skip to content

Commit

Permalink
fix(gatsby): Remove GATSBY_BUILD_STAGE & warn against ___NODE (#3…
Browse files Browse the repository at this point in the history
…3501)

Co-authored-by: Vladimir Razuvaev <vladimir.razuvaev@gmail.com>
Co-authored-by: Ward Peeters <ward@coding-tech.com>
  • Loading branch information
3 people committed Oct 13, 2021
1 parent 4658b80 commit 775289f
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 14 deletions.
3 changes: 1 addition & 2 deletions packages/babel-preset-gatsby/src/dependencies.ts
Expand Up @@ -18,8 +18,7 @@ export default (_?: unknown, options: IPresetOptions = {}) => {
require.resolve(`@babel/runtime/package.json`)
)

// TODO(v3): Remove process.env.GATSBY_BUILD_STAGE, needs to be passed as an option
const stage = options.stage || process.env.GATSBY_BUILD_STAGE || `test`
const stage = options.stage || `test`
const pluginBabelConfig = loadCachedConfig()
const targets = pluginBabelConfig.browserslist

Expand Down
18 changes: 16 additions & 2 deletions packages/gatsby/src/schema/infer/add-inferred-fields.js
Expand Up @@ -7,6 +7,7 @@ const report = require(`gatsby-cli/lib/reporter`)
import { isFile } from "./is-file"
import { isDate } from "../types/date"
import { addDerivedType } from "../types/derived-types"
import { warnOnce } from "../../utils/warn-once"
import { is32BitInteger } from "../../utils/is-32-bit-integer"
const { getDataStore } = require(`../../datastore`)

Expand All @@ -32,6 +33,14 @@ const addInferredFields = ({
typeMapping,
config,
})

if (deprecatedNodeKeys.size > 0) {
warnOnce(
`The ___NODE convention is deprecated. Please use the @link directive instead.\nType: ${typeComposer.getTypeName()}, Keys: ${Array.from(
deprecatedNodeKeys
).join(`, `)}\nMigration: https://gatsby.dev/node-convention-deprecation`
)
}
}

module.exports = {
Expand Down Expand Up @@ -98,6 +107,8 @@ const addInferredFieldsImpl = ({
return typeComposer
}

const deprecatedNodeKeys = new Set()

const getFieldConfig = ({
schemaComposer,
typeComposer,
Expand Down Expand Up @@ -125,12 +136,17 @@ const getFieldConfig = ({
// i.e. does the config contain sanitized field names?
fieldConfig = getFieldConfigFromMapping({ typeMapping, selector })
} else if (unsanitizedKey.includes(`___NODE`)) {
// TODO(v5): Remove ability to use foreign keys like this (e.g. author___NODE___contact___email)
// and recommend using schema customization instead

fieldConfig = getFieldConfigFromFieldNameConvention({
schemaComposer,
value: exampleValue,
key: unsanitizedKey,
})
arrays = arrays + (value.multiple ? 1 : 0)

deprecatedNodeKeys.add(unsanitizedKey)
} else {
fieldConfig = getSimpleFieldConfig({
schemaComposer,
Expand Down Expand Up @@ -214,8 +230,6 @@ const getFieldConfigFromFieldNameConvention = ({
const linkedTypesSet = new Set()

if (foreignKey) {
// TODO: deprecate foreign keys like this (e.g. author___NODE___contact___email)
// and recommend using schema customization instead
const linkedValues = new Set(value.linkedNodes)
getDataStore()
.iterateNodes()
Expand Down
2 changes: 0 additions & 2 deletions packages/gatsby/src/schema/types/filter.ts
Expand Up @@ -254,8 +254,6 @@ export const getFilterInput = ({
)
const inputTypeComposer = typeComposer.getInputTypeComposer()

// TODO: In Gatsby v2, the NodeInput.id field is of type String, not ID.
// Remove this workaround for v3.
if (
inputTypeComposer?.hasField(`id`) &&
getNamedType(inputTypeComposer.getFieldType(`id`)).name === `ID`
Expand Down
3 changes: 1 addition & 2 deletions packages/gatsby/src/schema/types/node-interface.ts
Expand Up @@ -64,8 +64,7 @@ const getOrCreateNodeInterface = <TSource, TArgs>(
},
internal: internalTC.getTypeNonNull(),
})
// TODO: In Gatsby v2, the NodeInput.id field is of type String, not ID.
// Remove this workaround for v3.

const nodeInputTC = tc.getInputTypeComposer()
nodeInputTC.extendField(`id`, { type: `String` })
})
Expand Down
1 change: 0 additions & 1 deletion packages/gatsby/src/utils/__tests__/webpack.config.js
Expand Up @@ -104,7 +104,6 @@ describe(`environment variables`, () => {
[
`__PATH_PREFIX__`,
`process.env.BUILD_STAGE`,
`process.env.GATSBY_BUILD_STAGE`,
`process.env.NODE_ENV`,
`process.env.PUBLIC_DIR`,
].reduce((merged, key) => {
Expand Down
3 changes: 2 additions & 1 deletion packages/gatsby/src/utils/warn-once.ts
@@ -1,10 +1,11 @@
import reporter from "gatsby-cli/lib/reporter"
import { isWorker } from "gatsby-worker"

const displayedWarnings = new Set<string>()

export const warnOnce = (message: string, key?: string): void => {
const messageId = key ?? message
if (!displayedWarnings.has(messageId)) {
if (!displayedWarnings.has(messageId) && !isWorker) {
displayedWarnings.add(messageId)
reporter.warn(message)
}
Expand Down
4 changes: 0 additions & 4 deletions packages/gatsby/src/utils/webpack.config.js
Expand Up @@ -47,10 +47,6 @@ module.exports = async (
const modulesThatUseGatsby = await getGatsbyDependents()
const directoryPath = withBasePath(directory)

// we will converge to build-html later on but for now this was the fastest way to get SSR to work
// TODO remove in v4 - we deprecated this in v3
process.env.GATSBY_BUILD_STAGE = suppliedStage

// We combine develop & develop-html stages for purposes of generating the
// webpack config.
const stage = suppliedStage
Expand Down

0 comments on commit 775289f

Please sign in to comment.