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

@@ -357,21 +357,17 @@ const FILE_INFO = new GraphQLObjectType({
const FILE_INPUT = new GraphQLInputObjectType({
name: 'FileInput',
description:
'If this field is set to null the file will be unlinked (the file will not be deleted on cloud storage).',
fields: {
file: {
description:
'A File Scalar can be an url or a FileInfo object. If this field is set to null the file will be unlinked.',
description: 'A File Scalar can be an url or a FileInfo object.',
type: FILE,
},
upload: {
description: 'Use this field if you want to create a new file.',
type: GraphQLUpload,
},
unlink: {
description:
'Use this field if you want to unlink the file (the file will not be deleted on cloud storage)',
type: GraphQLBoolean,
},
},
});

View File

@@ -10,6 +10,14 @@ import { ParseGraphQLClassConfig } from '../../Controllers/ParseGraphQLControlle
import { transformClassNameToGraphQL } from '../transformers/className';
import { transformTypes } from '../transformers/mutation';
const filterDeletedFields = fields =>
Object.keys(fields).reduce((acc, key) => {
if (typeof fields[key] === 'object' && fields[key]?.__op === 'Delete') {
acc[key] = null;
}
return acc;
}, fields);
const getOnlyRequiredFields = (
updatedFields,
selectedFieldsString,
@@ -131,7 +139,7 @@ const load = function (parseGraphQLSchema, parseClass, parseClassConfig: ?ParseG
[getGraphQLQueryName]: {
...createdObject,
updatedAt: createdObject.createdAt,
...parseFields,
...filterDeletedFields(parseFields),
...optimizedObject,
},
};
@@ -240,7 +248,7 @@ const load = function (parseGraphQLSchema, parseClass, parseClassConfig: ?ParseG
[getGraphQLQueryName]: {
objectId: id,
...updatedObject,
...parseFields,
...filterDeletedFields(parseFields),
...optimizedObject,
},
};

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' };