Skip to content

Commit

Permalink
fix(gatsby): update nested input types when rebuilding SitePage (#30426)
Browse files Browse the repository at this point in the history
  • Loading branch information
vladar authored Mar 23, 2021
1 parent c240813 commit 54d4721
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
18 changes: 18 additions & 0 deletions packages/gatsby/src/schema/__tests__/rebuild-sitepage-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,24 @@ describe(`build and update schema for SitePage`, () => {
await testNestedFields()
})

it(`updates nested input types on rebuild`, async () => {
// sanity-check
const inputFields = Object.keys(
schema.getType(`SitePageFieldsFilterInput`).getFields()
)
expect(inputFields.length).toBe(1)
expect(inputFields).toEqual([`oldKey`])

// Rebuild
const page = firstPage()
page.fields = {}
store.dispatch({ type: `CREATE_NODE`, payload: page })
await rebuildWithSitePage({})
schema = store.getState().schema

expect(schema.getType(`SitePageFieldsFilterInput`)).toBeUndefined()
})

it(`respects @dontInfer on SitePage`, async () => {
const typeDefs = `
type SitePage implements Node @dontInfer {
Expand Down
21 changes: 18 additions & 3 deletions packages/gatsby/src/schema/types/derived-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,15 @@ export const deleteFieldsOfDerivedTypes = ({ typeComposer }): void => {
})
}

const removeTypeFromSchemaComposer = ({
schemaComposer,
typeComposer,
}): void => {
schemaComposer.delete(typeComposer.getTypeName())
schemaComposer.delete((typeComposer as any)._gqType)
schemaComposer.delete(typeComposer)
}

export const clearDerivedTypes = ({
schemaComposer,
typeComposer,
Expand All @@ -77,15 +86,21 @@ export const clearDerivedTypes = ({
for (const typeName of derivedTypes.values()) {
const derivedTypeComposer = schemaComposer.getAnyTC(typeName)
clearDerivedTypes({ schemaComposer, typeComposer: derivedTypeComposer })
schemaComposer.delete(typeName)
schemaComposer.delete((derivedTypeComposer as any)._gqType)
schemaComposer.delete(derivedTypeComposer)
removeTypeFromSchemaComposer({
schemaComposer,
typeComposer: derivedTypeComposer,
})
}

if (
typeComposer instanceof ObjectTypeComposer ||
typeComposer instanceof InterfaceTypeComposer
) {
const inputTypeComposer = typeComposer.getInputTypeComposer()
removeTypeFromSchemaComposer({
schemaComposer,
typeComposer: inputTypeComposer,
})
typeComposer.removeInputTypeComposer()
}

Expand Down

0 comments on commit 54d4721

Please sign in to comment.