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

@@ -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', () => {
@@ -9141,7 +9297,7 @@ describe('ParseGraphQLServer', () => {
const mutationResult = await apolloClient.mutate({
mutation: gql`
mutation UnlinkFile($id: ID!) {
updateSomeClass(input: { id: $id, fields: { someField: { file: null } } }) {
updateSomeClass(input: { id: $id, fields: { someField: null } }) {
someClass {
someField {
name