Skip to content

Commit 32ad041

Browse files
authored
fix(gatsby): move require out of hot function (#36253)
* fix(gatsby): move require out of hot function This require showed up in a performance profile as taking ~600ms during a large query as `resolveField` is called a lot. We can require it out of the function to avoid this. * not sure why it needs the .default here * Update node-model.js * Update node-model.js * Fix lint
1 parent 94a3264 commit 32ad041

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

packages/gatsby/src/schema/node-model.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -846,7 +846,7 @@ async function resolveRecursive(
846846

847847
return _.pickBy(resolvedFields, (value, key) => queryFields[key])
848848
}
849-
849+
let withResolverContext
850850
function resolveField(
851851
nodeModel,
852852
schemaComposer,
@@ -858,7 +858,13 @@ function resolveField(
858858
if (!gqlField?.resolve) {
859859
return node[fieldName]
860860
}
861-
const withResolverContext = require(`./context`)
861+
862+
// We require this inline as there's a circular dependency from context back to this file.
863+
// https://github.com/gatsbyjs/gatsby/blob/9d33b107d167e3e9e2aa282924a0c409f6afd5a0/packages/gatsby/src/schema/context.ts#L5
864+
if (!withResolverContext) {
865+
withResolverContext = require(`./context`)
866+
}
867+
862868
return gqlField.resolve(
863869
node,
864870
gqlField.args.reduce((acc, arg) => {

0 commit comments

Comments
 (0)