Skip to content

Commit

Permalink
feat(mutations): support delete mutations
Browse files Browse the repository at this point in the history
  • Loading branch information
tothandras committed Oct 13, 2015
1 parent 7776a27 commit 63e7d99
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 19 deletions.
28 changes: 28 additions & 0 deletions src/e2e.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,34 @@ describe('e2e', () => {
}
});
});

it('should delete data', async function Test10() {
let result = await graphql(schema, `
mutation addUserMutation {
addUser(input: {name: "Test User", clientMutationId: "1"}) {
_id
}
}
`);
const id = result.data.addUser._id;

result = await graphql(schema, `
mutation deleteUserMutation {
deleteUser(input: {id: "${id}", clientMutationId: "2"}) {
clientMutationId
ok
}
}
`);
expect(result).to.containSubset({
data: {
deleteUser: {
clientMutationId: '2',
ok: true
}
}
});
});
});
});
});
30 changes: 21 additions & 9 deletions src/field/field.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import {
GraphQLID,
GraphQLObjectType,
GraphQLSchema,
GraphQLScalarType
GraphQLScalarType,
GraphQLBoolean
} from 'graphql';
import {mutationWithClientMutationId} from 'graphql-relay';
import {getModels} from './../model';
Expand All @@ -14,8 +15,9 @@ import {
getIdFetcher,
getOneResolver,
getListResolver,
getAddOneResolver,
getUpdateOneResolver
getAddOneMutateHandler,
getUpdateOneMutateHandler,
getDeleteOneMutateHandler
} from './../query';

function getQueryField(graffitiModel, type) {
Expand Down Expand Up @@ -86,15 +88,14 @@ function getMutationField(graffitiModel, type) {

const addName = `add${name}`;
const updateName = `update${name}`;
const deleteName = `delete${name}`;

return {
[addName]: mutationWithClientMutationId({
name: addName,
inputFields: args,
outputFields: allFields,
mutateAndGetPayload: (args) => {
return getAddOneResolver(graffitiModel)(null, args);
}
mutateAndGetPayload: getAddOneMutateHandler(graffitiModel)
}),
[updateName]: mutationWithClientMutationId({
name: updateName,
Expand All @@ -106,9 +107,20 @@ function getMutationField(graffitiModel, type) {
}
},
outputFields: allFields,
mutateAndGetPayload: (args) => {
return getUpdateOneResolver(graffitiModel)(null, args);
}
mutateAndGetPayload: getUpdateOneMutateHandler(graffitiModel)
}),
[deleteName]: mutationWithClientMutationId({
name: deleteName,
inputFields: {
id: {
type: new GraphQLNonNull(GraphQLID),
description: `The ID of a ${name}`
}
},
outputFields: {
ok: {type: GraphQLBoolean}
},
mutateAndGetPayload: getDeleteOneMutateHandler(graffitiModel)
})
};
}
Expand Down
4 changes: 2 additions & 2 deletions src/field/field.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ describe('field', () => {

describe('getMutationField', () => {
it('should return an addXyz and an updateXyz field', function getMutationFieldTest() {
this.sandbox.stub(query, 'getAddOneResolver').returns(() => {});
this.sandbox.stub(query, 'getUpdateOneResolver').returns(() => {});
this.sandbox.stub(query, 'getAddOneMutateHandler').returns(() => {});
this.sandbox.stub(query, 'getUpdateOneMutateHandler').returns(() => {});
const graphQLType = types.Qux;
const fields = getMutationField({Qux: {model: {}}}, graphQLType);
const args = {
Expand Down
37 changes: 29 additions & 8 deletions src/query/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,15 @@ function updateOne(Collection, args, info) {
});
}

function deleteOne(Collection, args) {
const _id = processId(args);
delete args.id;
delete args._id;
return Collection.remove({_id}).then(({result}) => {
return {ok: !!result.ok};
});
}

function getList(Collection, selector, options = {}, info = null) {
if (selector && (Array.isArray(selector.id) || Array.isArray(selector._id))) {
const {id, _id = id} = selector;
Expand Down Expand Up @@ -91,22 +100,33 @@ function getOneResolver(graffitiModel) {
};
}

function getAddOneResolver(graffitiModel) {
return (root, args, info) => {
function getAddOneMutateHandler(graffitiModel) {
return (args) => {
const Collection = graffitiModel.model;
if (Collection) {
return addOne(Collection, args, info);
return addOne(Collection, args);
}

return null;
};
}

function getUpdateOneResolver(graffitiModel) {
return (root, args, info) => {
function getUpdateOneMutateHandler(graffitiModel) {
return (args) => {
const Collection = graffitiModel.model;
if (Collection) {
return updateOne(Collection, args);
}

return null;
};
}

function getDeleteOneMutateHandler(graffitiModel) {
return (args) => {
const Collection = graffitiModel.model;
if (Collection) {
return updateOne(Collection, args, info);
return deleteOne(Collection, args);
}

return null;
Expand Down Expand Up @@ -270,8 +290,9 @@ export default {
_idToCursor: idToCursor,
getIdFetcher,
getOneResolver,
getAddOneResolver,
getUpdateOneResolver,
getAddOneMutateHandler,
getUpdateOneMutateHandler,
getDeleteOneMutateHandler,
getListResolver,
connectionFromModel
};

0 comments on commit 63e7d99

Please sign in to comment.