Skip to content

Commit

Permalink
fix(gatsby): move require out of hot function (#36253)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
KyleAMathews committed Jul 28, 2022
1 parent 94a3264 commit 32ad041
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions packages/gatsby/src/schema/node-model.js
Expand Up @@ -846,7 +846,7 @@ async function resolveRecursive(

return _.pickBy(resolvedFields, (value, key) => queryFields[key])
}

let withResolverContext
function resolveField(
nodeModel,
schemaComposer,
Expand All @@ -858,7 +858,13 @@ function resolveField(
if (!gqlField?.resolve) {
return node[fieldName]
}
const withResolverContext = require(`./context`)

// We require this inline as there's a circular dependency from context back to this file.
// https://github.com/gatsbyjs/gatsby/blob/9d33b107d167e3e9e2aa282924a0c409f6afd5a0/packages/gatsby/src/schema/context.ts#L5
if (!withResolverContext) {
withResolverContext = require(`./context`)
}

return gqlField.resolve(
node,
gqlField.args.reduce((acc, arg) => {
Expand Down

0 comments on commit 32ad041

Please sign in to comment.