diff --git a/src/field/field.js b/src/field/field.js index 4cdc338..664a2dc 100644 --- a/src/field/field.js +++ b/src/field/field.js @@ -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 { @@ -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] = { @@ -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); + } + }) }; } diff --git a/src/field/field.spec.js b/src/field/field.spec.js index 9b2de6b..f33ffd1 100644 --- a/src/field/field.spec.js +++ b/src/field/field.spec.js @@ -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: { @@ -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