Skip to content

Commit 31e754f

Browse files
GatsbyJS Botvladar
andauthored
fix(gatsby): freeze the schema only after rebuilding with SitePage (#30132) (#30137)
(cherry picked from commit 4fc4248) Co-authored-by: Vladimir Razuvaev <vladimir.razuvaev@gmail.com>
1 parent bc0eca5 commit 31e754f

2 files changed

Lines changed: 27 additions & 6 deletions

File tree

packages/gatsby/src/schema/__tests__/rebuild-sitepage-type.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const { store } = require(`../../redux`)
2+
const { graphql } = require(`../../../graphql`)
23
const { build, rebuildWithSitePage } = require(`..`)
34

45
jest.mock(`gatsby-cli/lib/reporter`, () => {
@@ -115,7 +116,7 @@ describe(`build and update schema for SitePage`, () => {
115116
expect(sortFieldsEnum.getValue(`context___key`)).toBeDefined()
116117
})
117118

118-
it(`updates nested types on rebuild`, async () => {
119+
const testNestedFields = async () => {
119120
let fields
120121
let inputFields
121122

@@ -149,6 +150,25 @@ describe(`build and update schema for SitePage`, () => {
149150
.map(value => value.name)
150151
expect(fieldsEnum.includes(`fields___oldKey`)).toBeTruthy()
151152
expect(fieldsEnum.includes(`fields___key`)).toBeTruthy()
153+
}
154+
155+
it(`updates nested types on rebuild`, testNestedFields)
156+
157+
it(`updates nested types on rebuild (with query executed before rebuilding)`, async () => {
158+
// Set a stage for the same test as above but with graphql query executed before updating schema
159+
// See https://github.com/gatsbyjs/gatsby/issues/30107
160+
const result = await graphql(
161+
schema,
162+
`
163+
{
164+
__typename
165+
}
166+
`,
167+
null,
168+
{}
169+
)
170+
expect(result).toEqual({ data: { __typename: `Query` } })
171+
await testNestedFields()
152172
})
153173

154174
it(`respects @dontInfer on SitePage`, async () => {

packages/gatsby/src/schema/schema.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,6 @@ const buildSchema = async ({
7676
// const { printSchema } = require(`graphql`)
7777
const schema = schemaComposer.buildSchema()
7878

79-
// Freeze all type composers except SitePage (as we will rebuild it at a later stage)
80-
freezeTypeComposers(schemaComposer, new Set([`SitePage`]))
81-
8279
// console.log(printSchema(schema))
8380
return schema
8481
}
@@ -117,7 +114,11 @@ const rebuildSchemaWithSitePage = async ({
117114
fieldExtensions,
118115
parentSpan,
119116
})
120-
return schemaComposer.buildSchema()
117+
const schema = schemaComposer.buildSchema()
118+
119+
freezeTypeComposers(schemaComposer)
120+
121+
return schema
121122
}
122123

123124
module.exports = {
@@ -127,7 +128,7 @@ module.exports = {
127128

128129
// Workaround for https://github.com/graphql-compose/graphql-compose/issues/319
129130
// FIXME: remove this when fixed in graphql-compose
130-
const freezeTypeComposers = (schemaComposer, excluded) => {
131+
const freezeTypeComposers = (schemaComposer, excluded = new Set()) => {
131132
Array.from(schemaComposer.values()).forEach(tc => {
132133
const isCompositeTC =
133134
tc instanceof ObjectTypeComposer || tc instanceof InterfaceTypeComposer

0 commit comments

Comments
 (0)