diff --git a/src/mutation/__tests__/mutation.js b/src/mutation/__tests__/mutation.js index 444c96e..2aae0ce 100644 --- a/src/mutation/__tests__/mutation.js +++ b/src/mutation/__tests__/mutation.js @@ -31,6 +31,21 @@ var simpleMutation = mutationWithClientMutationId({ mutateAndGetPayload: () => ({result: 1}) }); +var simpleMutationWithThunkFields = mutationWithClientMutationId({ + name: 'SimpleMutationWithThunkFields', + inputFields: () => ({ + inputData: { + type: GraphQLInt + } + }), + outputFields: () => ({ + result: { + type: GraphQLInt + } + }), + mutateAndGetPayload: ({ inputData }) => ({result: inputData}) +}); + var simplePromiseMutation = mutationWithClientMutationId({ name: 'SimplePromiseMutation', inputFields: {}, @@ -46,6 +61,7 @@ var mutation = new GraphQLObjectType({ name: 'Mutation', fields: { simpleMutation: simpleMutation, + simpleMutationWithThunkFields: simpleMutationWithThunkFields, simplePromiseMutation: simplePromiseMutation } }); @@ -89,6 +105,26 @@ describe('mutationWithClientMutationId()', () => { return expect(graphql(schema, query)).to.become(expected); }); + it('Supports thunks as input and output fields', () => { + var query = ` + mutation M { + simpleMutationWithThunkFields(input: {inputData: 1234, clientMutationId: "abc"}) { + result + clientMutationId + } + } + `; + var expected = { + data: { + simpleMutationWithThunkFields: { + result: 1234, + clientMutationId: 'abc' + } + } + }; + return expect(graphql(schema, query)).to.become(expected); + }); + it('supports promise mutations', () => { var query = ` mutation M { @@ -249,6 +285,26 @@ describe('mutationWithClientMutationId()', () => { kind: 'OBJECT', } }, + { + name: 'simpleMutationWithThunkFields', + args: [ + { + name: 'input', + type: { + name: null, + kind: 'NON_NULL', + ofType: { + name: 'SimpleMutationWithThunkFieldsInput', + kind: 'INPUT_OBJECT' + } + }, + } + ], + type: { + name: 'SimpleMutationWithThunkFieldsPayload', + kind: 'OBJECT', + } + }, { name: 'simplePromiseMutation', args: [ diff --git a/src/mutation/mutation.js b/src/mutation/mutation.js index 9eae43b..1ff08ca 100644 --- a/src/mutation/mutation.js +++ b/src/mutation/mutation.js @@ -25,6 +25,10 @@ import type { type mutationFn = (object: Object, info: GraphQLResolveInfo) => Object | (object: Object, info: GraphQLResolveInfo) => Promise; +function resolveMaybeThunk(thingOrThunk: T | () => T): T { + return typeof thingOrThunk === 'function' ? thingOrThunk() : thingOrThunk; +} + /** * A description of a mutation consumable by mutationWithClientMutationId * to create a GraphQLFieldConfig for that mutation. @@ -54,18 +58,18 @@ export function mutationWithClientMutationId( config: MutationConfig ): GraphQLFieldConfig { var {name, inputFields, outputFields, mutateAndGetPayload} = config; - var augmentedInputFields = { - ...inputFields, + var augmentedInputFields = () => ({ + ...resolveMaybeThunk(inputFields), clientMutationId: { type: new GraphQLNonNull(GraphQLString) } - }; - var augmentedOutputFields = { - ...outputFields, + }); + var augmentedOutputFields = () => ({ + ...resolveMaybeThunk(outputFields), clientMutationId: { type: new GraphQLNonNull(GraphQLString) } - }; + }); var outputType = new GraphQLObjectType({ name: name + 'Payload',