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

[schema] Remove empty input fields #12380

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
56 changes: 56 additions & 0 deletions packages/gatsby/src/schema/types/__tests__/filter-input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
const { build } = require(`../..`)
const { store } = require(`../../../redux`)
require(`../../../db/__tests__/fixtures/ensure-loki`)()

const nodes = [
{
id: `parent`,
nested: {
union___NODE: [`union1`, `union2`],
},
internal: {
type: `Parent`,
contentDigest: `a`,
},
},
{
id: `union1`,
foo: `bar`,
internal: {
type: `Union1`,
contentDigest: `bar`,
},
},
{
id: `union2`,
foo: `baz`,
internal: {
type: `Union2`,
contentDigest: `baz`,
},
},
]

describe(`Filter input`, () => {
beforeEach(async () => {
store.dispatch({ type: `DELETE_CACHE` })
nodes.forEach(node =>
store.dispatch({ type: `CREATE_NODE`, payload: { ...node } })
)
})

it(`removes empty input filter fields`, async () => {
// This can happen when a type has only one GraphQLUnion type field,
// which will be skipped by `toInputObjectType`
const schema = await buildSchema()
const parentFilterInput = schema.getType(`ParentFilterInput`)
const fields = parentFilterInput.getFields()
expect(fields.id).toBeDefined()
expect(fields.nested).toBeUndefined()
})
})

const buildSchema = async () => {
await build({})
return store.getState().schema
}
32 changes: 31 additions & 1 deletion packages/gatsby/src/schema/types/filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,34 @@ const convert = ({
return convertedITC
}

const removeEmptyFields = (
{ schemaComposer, inputTypeComposer },
cache = new Set()
) => {
const convert = itc => {
if (cache.has(itc)) {
return itc
}
cache.add(itc)
const fields = itc.getFields()
const nonEmptyFields = {}
Object.keys(fields).forEach(fieldName => {
const fieldITC = fields[fieldName]
if (fieldITC instanceof schemaComposer.InputTypeComposer) {
const convertedITC = convert(fieldITC)
if (convertedITC.getFieldNames().length) {
nonEmptyFields[fieldName] = convertedITC
}
} else {
nonEmptyFields[fieldName] = fieldITC
}
})
itc.setFields(nonEmptyFields)
return itc
}
return convert(inputTypeComposer)
}

const getFilterInput = ({ schemaComposer, typeComposer }) => {
const typeName = typeComposer.getTypeName()
const filterInputComposer = schemaComposer.getOrCreateITC(
Expand All @@ -88,11 +116,13 @@ const getFilterInput = ({ schemaComposer, typeComposer }) => {
inputTypeComposer.extendField(`id`, { type: `String` })
}

return convert({
const filterInputTC = convert({
schemaComposer,
inputTypeComposer,
filterInputComposer,
})

return removeEmptyFields({ schemaComposer, inputTypeComposer: filterInputTC })
}

module.exports = { getFilterInput }
Expand Down