Skip to content
This repository was archived by the owner on Dec 26, 2023. It is now read-only.

Commit 589e2ab

Browse files
committed
Add delete mutation
1 parent 856e37d commit 589e2ab

File tree

4 files changed

+41
-11
lines changed

4 files changed

+41
-11
lines changed

src/generateGraphQLSchema.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import queryForType from './query/field';
88
import typeForPointer from './types/typeForPointer';
99
import create from './mutation/create/field';
1010
import update from './mutation/update/field'
11+
import deleteField from './mutation/delete/field';
1112

1213
const schemaArrayToObject = reduce((obj, data) => ({
1314
...obj,
@@ -53,6 +54,13 @@ export default function generateGraphqlSchema(parseSchema) {
5354
fields,
5455
displayName: mapClassName(className),
5556
}, types[className]()),
57+
58+
[mutationField('delete', className)]:
59+
deleteField({
60+
className,
61+
fields,
62+
displayName: mapClassName(className)
63+
}, types[className]())
5664
}), {});
5765

5866
const query = new GraphQLObjectType({

src/mutation/create/field.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
import Parse from 'parse/node';
22
import JSONObject from '../../types/JSONObject'
3-
import createInputType from './inputType'
3+
import inputType from './inputType'
44

55
export default ({ className, displayName, fields }, Type) => ({
66
type: Type,
7+
description: `Create a new ${displayName}`,
78
args: {
89
data: {
9-
type: createInputType({ className, displayName, fields }),
10+
type: inputType({ className, displayName, fields }),
1011
},
11-
newProperties: {
12+
newAttributes: {
1213
type: JSONObject,
14+
description: "Allows saving attributes that don't exist yet"
1315
}
1416
},
1517
resolve(value, args, { sessionToken }) {

src/mutation/delete/field.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import Parse from 'parse/node';
2+
import { GraphQLNonNull, GraphQLString } from 'graphql';
3+
4+
export default ({ className, displayName }, Type) => ({
5+
type: Type,
6+
description: `Delete a ${displayName}. The deleted object is returned.`,
7+
args: {
8+
objectId: {
9+
type: GraphQLNonNull(GraphQLString),
10+
description: `Object ID of the ${displayName} to delete`,
11+
}
12+
},
13+
resolve(value, { objectId }, { sessionToken }) {
14+
const object = new Parse.Object(className, { objectId });
15+
return object.destroy({ sessionToken });
16+
}
17+
});

src/mutation/update/field.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,28 @@
11
import Parse from 'parse/node';
2+
import { GraphQLNonNull } from 'graphql';
23
import JSONObject from '../../types/JSONObject';
3-
import updateInputType from './inputType';
4+
import inputType from './inputType';
45

56
export default ({ className, displayName, fields }, Type) => ({
67
type: Type,
8+
description: `Update an existing ${displayName}`,
79
args: {
8-
data: {
9-
type: updateInputType({ className, displayName, fields }),
10+
input: {
11+
type: GraphQLNonNull(inputType({ className, displayName, fields })),
1012
},
11-
newProperties: {
13+
newAttributes: {
1214
type: JSONObject,
15+
description: "Allows saving attributes that don't exist yet"
1316
},
1417
},
15-
async resolve(value, { data, newProperties }, { sessionToken }, info) {
16-
const existing = await new Parse.Query(className).get(data.objectId);
18+
async resolve(value, { input, newAttributes }, { sessionToken }) {
19+
const existing = await new Parse.Query(className).get(input.objectId);
1720
if (!existing) {
1821
throw new Error('Item not found');
1922
}
2023
const object = new Parse.Object(className, {
21-
...data,
22-
...newProperties,
24+
...input,
25+
...newAttributes,
2326
});
2427
return object.save({ sessionToken });
2528
},

0 commit comments

Comments
 (0)