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:
@@ -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,
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
@@ -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,
|
||||
},
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user