Skip to content

Commit

Permalink
fix(gatsby): workaround graphql-compose issue (#29822) (#29834)
Browse files Browse the repository at this point in the history
* fix(gatsby): workaround graphql-compose issue

* freeze after building

(cherry picked from commit 7f9bcf1)

Co-authored-by: Vladimir Razuvaev <vladimir.razuvaev@gmail.com>
  • Loading branch information
GatsbyJS Bot and vladar committed Feb 28, 2021
1 parent 32fee71 commit b8d21f8
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions packages/gatsby/src/schema/schema.js
Expand Up @@ -75,6 +75,10 @@ const buildSchema = async ({
})
// const { printSchema } = require(`graphql`)
const schema = schemaComposer.buildSchema()

// Freeze all type composers except SitePage (as we will rebuild it at a later stage)
freezeTypeComposers(schemaComposer, new Set([`SitePage`]))

// console.log(printSchema(schema))
return schema
}
Expand Down Expand Up @@ -121,6 +125,25 @@ module.exports = {
rebuildSchemaWithSitePage,
}

// Workaround for https://github.com/graphql-compose/graphql-compose/issues/319
// FIXME: remove this when fixed in graphql-compose
const freezeTypeComposers = (schemaComposer, excluded) => {
Array.from(schemaComposer.values()).forEach(tc => {
const isCompositeTC =
tc instanceof ObjectTypeComposer || tc instanceof InterfaceTypeComposer

if (isCompositeTC && !excluded.has(tc.getTypeName())) {
// typeComposer.getType() actually mutates the underlying GraphQL type
// and always re-assigns type._fields with a thunk.
// It causes continuous redundant field re-definitions when running queries
// (affects performance significantly).
// Prevent the mutation and "freeze" the type:
const type = tc.getType()
tc.getType = () => type
}
})
}

const updateSchemaComposer = async ({
schemaComposer,
types,
Expand Down

0 comments on commit b8d21f8

Please sign in to comment.