fix: setting a field to null does not delete it via GraphQL API (#7649)

BREAKING CHANGE: To delete a field via the GraphQL API, the field value has to be set to `null`. Previously, setting a field value to `null` would save a null value in the database, which was not according to the [GraphQL specs](https://spec.graphql.org/June2018/#sec-Null-Value). To delete a file field use `file: null`, the previous way of using `file: { file: null }` has become obsolete.
This commit is contained in:
Antoine Cormouls
2021-10-27 01:33:48 +02:00
committed by GitHub
parent 4c29d4d23b
commit 626fad2e71
5 changed files with 193 additions and 14 deletions

View File

@@ -30,9 +30,17 @@ const transformTypes = async (
if (inputTypeField) {
switch (true) {
case inputTypeField.type === defaultGraphQLTypes.GEO_POINT_INPUT:
if (fields[field] === null) {
fields[field] = { __op: 'Delete' };
break;
}
fields[field] = transformers.geoPoint(fields[field]);
break;
case inputTypeField.type === defaultGraphQLTypes.POLYGON_INPUT:
if (fields[field] === null) {
fields[field] = { __op: 'Delete' };
break;
}
fields[field] = transformers.polygon(fields[field]);
break;
case inputTypeField.type === defaultGraphQLTypes.FILE_INPUT:
@@ -48,6 +56,10 @@ const transformTypes = async (
);
break;
case parseClass.fields[field].type === 'Pointer':
if (fields[field] === null) {
fields[field] = { __op: 'Delete' };
break;
}
fields[field] = await transformers.pointer(
parseClass.fields[field].targetClass,
field,
@@ -56,6 +68,12 @@ const transformTypes = async (
req
);
break;
default:
if (fields[field] === null) {
fields[field] = { __op: 'Delete' };
return;
}
break;
}
}
});
@@ -66,10 +84,11 @@ const transformTypes = async (
};
const transformers = {
file: async ({ file, upload }, { config }) => {
if (file === null && !upload) {
return null;
file: async (input, { config }) => {
if (input === null) {
return { __op: 'Delete' };
}
const { file, upload } = input;
if (upload) {
const { fileInfo } = await handleUpload(upload, config);
return { ...fileInfo, __type: 'File' };