From b8d21f8fdb650bbed945ab136240924b8e6ac6e8 Mon Sep 17 00:00:00 2001 From: GatsbyJS Bot Date: Sun, 28 Feb 2021 12:32:44 -0500 Subject: [PATCH] fix(gatsby): workaround graphql-compose issue (#29822) (#29834) * fix(gatsby): workaround graphql-compose issue * freeze after building (cherry picked from commit 7f9bcf10797f0e1ba1583c6f1a6417ffe91f1b5e) Co-authored-by: Vladimir Razuvaev --- packages/gatsby/src/schema/schema.js | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/packages/gatsby/src/schema/schema.js b/packages/gatsby/src/schema/schema.js index 4a9fe950de06f..87e20119acb3f 100644 --- a/packages/gatsby/src/schema/schema.js +++ b/packages/gatsby/src/schema/schema.js @@ -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 } @@ -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,