Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(gatsby): freeze the schema only after rebuilding with SitePage #30132

Merged
merged 1 commit into from
Mar 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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