Skip to content

Commit e35ffdd

Browse files
committed
fix(gatsby): workaround graphql-compose issue (#29822)
* fix(gatsby): workaround graphql-compose issue * freeze after building (cherry picked from commit 7f9bcf1)
1 parent 32fee71 commit e35ffdd

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

packages/gatsby/src/schema/schema.js

+23
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ const buildSchema = async ({
7575
})
7676
// const { printSchema } = require(`graphql`)
7777
const schema = schemaComposer.buildSchema()
78+
79+
// Freeze all type composers except SitePage (as we will rebuild it at a later stage)
80+
freezeTypeComposers(schemaComposer, new Set([`SitePage`]))
81+
7882
// console.log(printSchema(schema))
7983
return schema
8084
}
@@ -121,6 +125,25 @@ module.exports = {
121125
rebuildSchemaWithSitePage,
122126
}
123127

128+
// Workaround for https://github.com/graphql-compose/graphql-compose/issues/319
129+
// FIXME: remove this when fixed in graphql-compose
130+
const freezeTypeComposers = (schemaComposer, excluded) => {
131+
Array.from(schemaComposer.values()).forEach(tc => {
132+
const isCompositeTC =
133+
tc instanceof ObjectTypeComposer || tc instanceof InterfaceTypeComposer
134+
135+
if (isCompositeTC && !excluded.has(tc.getTypeName())) {
136+
// typeComposer.getType() actually mutates the underlying GraphQL type
137+
// and always re-assigns type._fields with a thunk.
138+
// It causes continuous redundant field re-definitions when running queries
139+
// (affects performance significantly).
140+
// Prevent the mutation and "freeze" the type:
141+
const type = tc.getType()
142+
tc.getType = () => type
143+
}
144+
})
145+
}
146+
124147
const updateSchemaComposer = async ({
125148
schemaComposer,
126149
types,

0 commit comments

Comments
 (0)