Skip to content

Commit

Permalink
refactor(mutations): use mutationWithClientMutationId from the graphq…
Browse files Browse the repository at this point in the history
…l-relay module
  • Loading branch information
tothandras committed Oct 13, 2015
1 parent 73ec052 commit 7776a27
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 82 deletions.
104 changes: 26 additions & 78 deletions src/field/field.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import {
GraphQLID,
GraphQLObjectType,
GraphQLSchema,
GraphQLScalarType,
GraphQLInputObjectType
GraphQLScalarType
} from 'graphql';
import {mutationWithClientMutationId} from 'graphql-relay';
import {getModels} from './../model';
import {getTypes, nodeInterface} from './../type';
import {
Expand Down Expand Up @@ -64,8 +64,8 @@ function getQueryField(graffitiModel, type) {
function getMutationField(graffitiModel, type) {
const {name} = type;

const allField = type._typeConfig.fields();
const args = reduce(allField, (args, field) => {
const allFields = type._typeConfig.fields();
const args = reduce(allFields, (args, field) => {
if (field.type instanceof GraphQLObjectType) {
if (field.type.name.endsWith('Connection')) {
args[field.name] = {
Expand All @@ -84,84 +84,32 @@ function getMutationField(graffitiModel, type) {
return args;
}, {});

const addInputType = new GraphQLNonNull(new GraphQLInputObjectType({
name: `${name}AddInput`,
fields: () => ({
...args,
clientMutationId: {
name: 'clientMutationId',
type: new GraphQLNonNull(GraphQLID)
}
})
}));

const updateInputType = new GraphQLNonNull(new GraphQLInputObjectType({
name: `${name}UpdateInput`,
fields: () => ({
...args,
clientMutationId: {
name: 'clientMutationId',
type: new GraphQLNonNull(GraphQLID)
},
id: {
type: new GraphQLNonNull(GraphQLID),
description: `The ID of a ${name}`
}
})
}));

const outputType = new GraphQLObjectType({
name: `${name}Payload`,
fields: () => ({
...allField,
clientMutationId: {
name: 'clientMutationId',
type: new GraphQLNonNull(GraphQLID)
}
})
});
const addName = `add${name}`;
const updateName = `update${name}`;

return {
[`add${name}`]: {
type: outputType,
args: {
input: {
name: 'input',
type: addInputType
}
},
resolve: (root, args, info) => {
const clientMutationId = args.input.clientMutationId;
delete args.input.clientMutationId;
return getAddOneResolver(graffitiModel)(root, args.input, info).then((result) => {
return {
clientMutationId,
...result
};
});
},
resolveType: outputType
},
[`update${name}`]: {
type: outputType,
args: {
input: {
name: 'input',
type: updateInputType
[addName]: mutationWithClientMutationId({
name: addName,
inputFields: args,
outputFields: allFields,
mutateAndGetPayload: (args) => {
return getAddOneResolver(graffitiModel)(null, args);
}
}),
[updateName]: mutationWithClientMutationId({
name: updateName,
inputFields: {
...args,
id: {
type: new GraphQLNonNull(GraphQLID),
description: `The ID of a ${name}`
}
},
resolve: (root, args, info) => {
const clientMutationId = args.input.clientMutationId;
delete args.input.clientMutationId;
return getUpdateOneResolver(graffitiModel)(root, {id: args.id, ...args.input}, info).then((result) => {
return {
clientMutationId,
...result
};
});
},
resolveType: outputType
}
outputFields: allFields,
mutateAndGetPayload: (args) => {
return getUpdateOneResolver(graffitiModel)(null, args);
}
})
};
}

Expand Down
6 changes: 2 additions & 4 deletions src/field/field.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,7 @@ describe('field', () => {
const graphQLType = types.Qux;
const fields = getMutationField({Qux: {model: {}}}, graphQLType);
const args = {
input: {
name: 'input'
}
input: {}
};
expect(fields).to.containSubset({
addQux: {
Expand All @@ -82,7 +80,7 @@ describe('field', () => {
args
}
});
expect(fields.addQux.args.input.type.ofType._typeConfig.fields()).to.containSubset({
expect(fields.addQux.args.input.type.ofType._typeConfig.fields).to.containSubset({
bar: {
name: 'bar',
type: GraphQLString
Expand Down

0 comments on commit 7776a27

Please sign in to comment.