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:
@@ -101,7 +101,7 @@ ___
|
|||||||
# [Unreleased (master branch)](https://github.com/parse-community/parse-server/compare/5.0.0-alpha.1...master)
|
# [Unreleased (master branch)](https://github.com/parse-community/parse-server/compare/5.0.0-alpha.1...master)
|
||||||
|
|
||||||
## Breaking Changes
|
## Breaking Changes
|
||||||
- (none)
|
- feat: `null` value on field during graphql mutation now unset the value from the database, file unset changed
|
||||||
## Features
|
## Features
|
||||||
- (none)
|
- (none)
|
||||||
## Bug Fixes
|
## Bug Fixes
|
||||||
|
|||||||
@@ -6606,6 +6606,162 @@ describe('ParseGraphQLServer', () => {
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should unset fields when null used on update/create', async () => {
|
||||||
|
const customerSchema = new Parse.Schema('Customer');
|
||||||
|
customerSchema.addString('aString');
|
||||||
|
customerSchema.addBoolean('aBoolean');
|
||||||
|
customerSchema.addDate('aDate');
|
||||||
|
customerSchema.addArray('aArray');
|
||||||
|
customerSchema.addGeoPoint('aGeoPoint');
|
||||||
|
customerSchema.addPointer('aPointer', 'Customer');
|
||||||
|
customerSchema.addObject('aObject');
|
||||||
|
customerSchema.addPolygon('aPolygon');
|
||||||
|
await customerSchema.save();
|
||||||
|
|
||||||
|
await parseGraphQLServer.parseGraphQLSchema.schemaCache.clear();
|
||||||
|
|
||||||
|
const cus = new Parse.Object('Customer');
|
||||||
|
await cus.save({ aString: 'hello' });
|
||||||
|
|
||||||
|
const fields = {
|
||||||
|
aString: "i'm string",
|
||||||
|
aBoolean: true,
|
||||||
|
aDate: new Date().toISOString(),
|
||||||
|
aArray: ['hello', 1],
|
||||||
|
aGeoPoint: { latitude: 30, longitude: 30 },
|
||||||
|
aPointer: { link: cus.id },
|
||||||
|
aObject: { prop: { subprop: 1 }, prop2: 'test' },
|
||||||
|
aPolygon: [
|
||||||
|
{ latitude: 30, longitude: 30 },
|
||||||
|
{ latitude: 31, longitude: 31 },
|
||||||
|
{ latitude: 32, longitude: 32 },
|
||||||
|
{ latitude: 30, longitude: 30 },
|
||||||
|
],
|
||||||
|
};
|
||||||
|
const nullFields = Object.keys(fields).reduce((acc, k) => ({ ...acc, [k]: null }), {});
|
||||||
|
const result = await apolloClient.mutate({
|
||||||
|
mutation: gql`
|
||||||
|
mutation CreateCustomer($input: CreateCustomerInput!) {
|
||||||
|
createCustomer(input: $input) {
|
||||||
|
customer {
|
||||||
|
id
|
||||||
|
aString
|
||||||
|
aBoolean
|
||||||
|
aDate
|
||||||
|
aArray {
|
||||||
|
... on Element {
|
||||||
|
value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
aGeoPoint {
|
||||||
|
longitude
|
||||||
|
latitude
|
||||||
|
}
|
||||||
|
aPointer {
|
||||||
|
objectId
|
||||||
|
}
|
||||||
|
aObject
|
||||||
|
aPolygon {
|
||||||
|
longitude
|
||||||
|
latitude
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
`,
|
||||||
|
variables: {
|
||||||
|
input: { fields },
|
||||||
|
},
|
||||||
|
});
|
||||||
|
const {
|
||||||
|
data: {
|
||||||
|
createCustomer: {
|
||||||
|
customer: { aPointer, aArray, id, ...otherFields },
|
||||||
|
},
|
||||||
|
},
|
||||||
|
} = result;
|
||||||
|
expect(id).toBeDefined();
|
||||||
|
delete otherFields.__typename;
|
||||||
|
delete otherFields.aGeoPoint.__typename;
|
||||||
|
otherFields.aPolygon.forEach(v => {
|
||||||
|
delete v.__typename;
|
||||||
|
});
|
||||||
|
expect({
|
||||||
|
...otherFields,
|
||||||
|
aPointer: { link: aPointer.objectId },
|
||||||
|
aArray: aArray.map(({ value }) => value),
|
||||||
|
}).toEqual(fields);
|
||||||
|
|
||||||
|
const updated = await apolloClient.mutate({
|
||||||
|
mutation: gql`
|
||||||
|
mutation UpdateCustomer($input: UpdateCustomerInput!) {
|
||||||
|
updateCustomer(input: $input) {
|
||||||
|
customer {
|
||||||
|
aString
|
||||||
|
aBoolean
|
||||||
|
aDate
|
||||||
|
aArray {
|
||||||
|
... on Element {
|
||||||
|
value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
aGeoPoint {
|
||||||
|
longitude
|
||||||
|
latitude
|
||||||
|
}
|
||||||
|
aPointer {
|
||||||
|
objectId
|
||||||
|
}
|
||||||
|
aObject
|
||||||
|
aPolygon {
|
||||||
|
longitude
|
||||||
|
latitude
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
`,
|
||||||
|
variables: {
|
||||||
|
input: { fields: nullFields, id },
|
||||||
|
},
|
||||||
|
});
|
||||||
|
const {
|
||||||
|
data: {
|
||||||
|
updateCustomer: { customer },
|
||||||
|
},
|
||||||
|
} = updated;
|
||||||
|
delete customer.__typename;
|
||||||
|
expect(Object.keys(customer).length).toEqual(8);
|
||||||
|
Object.keys(customer).forEach(k => {
|
||||||
|
expect(customer[k]).toBeNull();
|
||||||
|
});
|
||||||
|
try {
|
||||||
|
const queryResult = await apolloClient.query({
|
||||||
|
query: gql`
|
||||||
|
query getEmptyCustomer($where: CustomerWhereInput!) {
|
||||||
|
customers(where: $where) {
|
||||||
|
edges {
|
||||||
|
node {
|
||||||
|
id
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
`,
|
||||||
|
variables: {
|
||||||
|
where: Object.keys(fields).reduce(
|
||||||
|
(acc, k) => ({ ...acc, [k]: { exists: false } }),
|
||||||
|
{}
|
||||||
|
),
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(queryResult.data.customers.edges.length).toEqual(1);
|
||||||
|
} catch (e) {
|
||||||
|
console.log(JSON.stringify(e));
|
||||||
|
}
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('Files Mutations', () => {
|
describe('Files Mutations', () => {
|
||||||
@@ -9141,7 +9297,7 @@ describe('ParseGraphQLServer', () => {
|
|||||||
const mutationResult = await apolloClient.mutate({
|
const mutationResult = await apolloClient.mutate({
|
||||||
mutation: gql`
|
mutation: gql`
|
||||||
mutation UnlinkFile($id: ID!) {
|
mutation UnlinkFile($id: ID!) {
|
||||||
updateSomeClass(input: { id: $id, fields: { someField: { file: null } } }) {
|
updateSomeClass(input: { id: $id, fields: { someField: null } }) {
|
||||||
someClass {
|
someClass {
|
||||||
someField {
|
someField {
|
||||||
name
|
name
|
||||||
|
|||||||
@@ -357,21 +357,17 @@ const FILE_INFO = new GraphQLObjectType({
|
|||||||
|
|
||||||
const FILE_INPUT = new GraphQLInputObjectType({
|
const FILE_INPUT = new GraphQLInputObjectType({
|
||||||
name: 'FileInput',
|
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: {
|
fields: {
|
||||||
file: {
|
file: {
|
||||||
description:
|
description: 'A File Scalar can be an url or a FileInfo object.',
|
||||||
'A File Scalar can be an url or a FileInfo object. If this field is set to null the file will be unlinked.',
|
|
||||||
type: FILE,
|
type: FILE,
|
||||||
},
|
},
|
||||||
upload: {
|
upload: {
|
||||||
description: 'Use this field if you want to create a new file.',
|
description: 'Use this field if you want to create a new file.',
|
||||||
type: GraphQLUpload,
|
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 { transformClassNameToGraphQL } from '../transformers/className';
|
||||||
import { transformTypes } from '../transformers/mutation';
|
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 = (
|
const getOnlyRequiredFields = (
|
||||||
updatedFields,
|
updatedFields,
|
||||||
selectedFieldsString,
|
selectedFieldsString,
|
||||||
@@ -131,7 +139,7 @@ const load = function (parseGraphQLSchema, parseClass, parseClassConfig: ?ParseG
|
|||||||
[getGraphQLQueryName]: {
|
[getGraphQLQueryName]: {
|
||||||
...createdObject,
|
...createdObject,
|
||||||
updatedAt: createdObject.createdAt,
|
updatedAt: createdObject.createdAt,
|
||||||
...parseFields,
|
...filterDeletedFields(parseFields),
|
||||||
...optimizedObject,
|
...optimizedObject,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
@@ -240,7 +248,7 @@ const load = function (parseGraphQLSchema, parseClass, parseClassConfig: ?ParseG
|
|||||||
[getGraphQLQueryName]: {
|
[getGraphQLQueryName]: {
|
||||||
objectId: id,
|
objectId: id,
|
||||||
...updatedObject,
|
...updatedObject,
|
||||||
...parseFields,
|
...filterDeletedFields(parseFields),
|
||||||
...optimizedObject,
|
...optimizedObject,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -30,9 +30,17 @@ const transformTypes = async (
|
|||||||
if (inputTypeField) {
|
if (inputTypeField) {
|
||||||
switch (true) {
|
switch (true) {
|
||||||
case inputTypeField.type === defaultGraphQLTypes.GEO_POINT_INPUT:
|
case inputTypeField.type === defaultGraphQLTypes.GEO_POINT_INPUT:
|
||||||
|
if (fields[field] === null) {
|
||||||
|
fields[field] = { __op: 'Delete' };
|
||||||
|
break;
|
||||||
|
}
|
||||||
fields[field] = transformers.geoPoint(fields[field]);
|
fields[field] = transformers.geoPoint(fields[field]);
|
||||||
break;
|
break;
|
||||||
case inputTypeField.type === defaultGraphQLTypes.POLYGON_INPUT:
|
case inputTypeField.type === defaultGraphQLTypes.POLYGON_INPUT:
|
||||||
|
if (fields[field] === null) {
|
||||||
|
fields[field] = { __op: 'Delete' };
|
||||||
|
break;
|
||||||
|
}
|
||||||
fields[field] = transformers.polygon(fields[field]);
|
fields[field] = transformers.polygon(fields[field]);
|
||||||
break;
|
break;
|
||||||
case inputTypeField.type === defaultGraphQLTypes.FILE_INPUT:
|
case inputTypeField.type === defaultGraphQLTypes.FILE_INPUT:
|
||||||
@@ -48,6 +56,10 @@ const transformTypes = async (
|
|||||||
);
|
);
|
||||||
break;
|
break;
|
||||||
case parseClass.fields[field].type === 'Pointer':
|
case parseClass.fields[field].type === 'Pointer':
|
||||||
|
if (fields[field] === null) {
|
||||||
|
fields[field] = { __op: 'Delete' };
|
||||||
|
break;
|
||||||
|
}
|
||||||
fields[field] = await transformers.pointer(
|
fields[field] = await transformers.pointer(
|
||||||
parseClass.fields[field].targetClass,
|
parseClass.fields[field].targetClass,
|
||||||
field,
|
field,
|
||||||
@@ -56,6 +68,12 @@ const transformTypes = async (
|
|||||||
req
|
req
|
||||||
);
|
);
|
||||||
break;
|
break;
|
||||||
|
default:
|
||||||
|
if (fields[field] === null) {
|
||||||
|
fields[field] = { __op: 'Delete' };
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -66,10 +84,11 @@ const transformTypes = async (
|
|||||||
};
|
};
|
||||||
|
|
||||||
const transformers = {
|
const transformers = {
|
||||||
file: async ({ file, upload }, { config }) => {
|
file: async (input, { config }) => {
|
||||||
if (file === null && !upload) {
|
if (input === null) {
|
||||||
return null;
|
return { __op: 'Delete' };
|
||||||
}
|
}
|
||||||
|
const { file, upload } = input;
|
||||||
if (upload) {
|
if (upload) {
|
||||||
const { fileInfo } = await handleUpload(upload, config);
|
const { fileInfo } = await handleUpload(upload, config);
|
||||||
return { ...fileInfo, __type: 'File' };
|
return { ...fileInfo, __type: 'File' };
|
||||||
|
|||||||
Reference in New Issue
Block a user