Skip to content

Commit

Permalink
fix(gatsby): freeze the schema only after rebuilding with SitePage (#…
Browse files Browse the repository at this point in the history
…30132) (#30137)

(cherry picked from commit 4fc4248)

Co-authored-by: Vladimir Razuvaev <vladimir.razuvaev@gmail.com>
  • Loading branch information
GatsbyJS Bot and vladar committed Mar 9, 2021
1 parent bc0eca5 commit 31e754f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
22 changes: 21 additions & 1 deletion packages/gatsby/src/schema/__tests__/rebuild-sitepage-type.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const { store } = require(`../../redux`)
const { graphql } = require(`../../../graphql`)
const { build, rebuildWithSitePage } = require(`..`)

jest.mock(`gatsby-cli/lib/reporter`, () => {
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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 () => {
Expand Down
11 changes: 6 additions & 5 deletions packages/gatsby/src/schema/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -117,7 +114,11 @@ const rebuildSchemaWithSitePage = async ({
fieldExtensions,
parentSpan,
})
return schemaComposer.buildSchema()
const schema = schemaComposer.buildSchema()

freezeTypeComposers(schemaComposer)

return schema
}

module.exports = {
Expand All @@ -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
Expand Down

0 comments on commit 31e754f

Please sign in to comment.