From bc715ea457a9b4e1562cce2cb95b6fada047e0e8 Mon Sep 17 00:00:00 2001 From: Vladimir Razuvaev Date: Tue, 9 Mar 2021 20:03:29 +0700 Subject: [PATCH] fix(gatsby): freeze the schema only after rebuilding with SitePage (#30132) (cherry picked from commit 4fc424868fa72b04a8152b2e8f0dceeb4955b15f) --- .../schema/__tests__/rebuild-sitepage-type.js | 22 ++++++++++++++++++- packages/gatsby/src/schema/schema.js | 11 +++++----- 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/packages/gatsby/src/schema/__tests__/rebuild-sitepage-type.js b/packages/gatsby/src/schema/__tests__/rebuild-sitepage-type.js index 2a7342fe130cb..d032dfa071374 100644 --- a/packages/gatsby/src/schema/__tests__/rebuild-sitepage-type.js +++ b/packages/gatsby/src/schema/__tests__/rebuild-sitepage-type.js @@ -1,4 +1,5 @@ const { store } = require(`../../redux`) +const { graphql } = require(`../../../graphql`) const { build, rebuildWithSitePage } = require(`..`) jest.mock(`gatsby-cli/lib/reporter`, () => { @@ -115,7 +116,7 @@ describe(`build and update schema for SitePage`, () => { expect(sortFieldsEnum.getValue(`context___key`)).toBeDefined() }) - it(`updates nested types on rebuild`, async () => { + const testNestedFields = async () => { let fields let inputFields @@ -149,6 +150,25 @@ describe(`build and update schema for SitePage`, () => { .map(value => value.name) expect(fieldsEnum.includes(`fields___oldKey`)).toBeTruthy() expect(fieldsEnum.includes(`fields___key`)).toBeTruthy() + } + + it(`updates nested types on rebuild`, testNestedFields) + + it(`updates nested types on rebuild (with query executed before rebuilding)`, async () => { + // Set a stage for the same test as above but with graphql query executed before updating schema + // See https://github.com/gatsbyjs/gatsby/issues/30107 + const result = await graphql( + schema, + ` + { + __typename + } + `, + null, + {} + ) + expect(result).toEqual({ data: { __typename: `Query` } }) + await testNestedFields() }) it(`respects @dontInfer on SitePage`, async () => { diff --git a/packages/gatsby/src/schema/schema.js b/packages/gatsby/src/schema/schema.js index 87e20119acb3f..b4e30533d0d5b 100644 --- a/packages/gatsby/src/schema/schema.js +++ b/packages/gatsby/src/schema/schema.js @@ -76,9 +76,6 @@ 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 } @@ -117,7 +114,11 @@ const rebuildSchemaWithSitePage = async ({ fieldExtensions, parentSpan, }) - return schemaComposer.buildSchema() + const schema = schemaComposer.buildSchema() + + freezeTypeComposers(schemaComposer) + + return schema } module.exports = { @@ -127,7 +128,7 @@ module.exports = { // Workaround for https://github.com/graphql-compose/graphql-compose/issues/319 // FIXME: remove this when fixed in graphql-compose -const freezeTypeComposers = (schemaComposer, excluded) => { +const freezeTypeComposers = (schemaComposer, excluded = new Set()) => { Array.from(schemaComposer.values()).forEach(tc => { const isCompositeTC = tc instanceof ObjectTypeComposer || tc instanceof InterfaceTypeComposer