From adb856f0ab6a400fa7b4ed5803def5764edeffb0 Mon Sep 17 00:00:00 2001 From: Adam Miskiewicz Date: Thu, 10 Sep 2015 19:31:44 -0400 Subject: [PATCH 1/3] mutation input and output fields should support thunks --- src/mutation/mutation.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/mutation/mutation.js b/src/mutation/mutation.js index 9eae43b..f2b4fcb 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. @@ -55,13 +59,13 @@ export function mutationWithClientMutationId( ): GraphQLFieldConfig { var {name, inputFields, outputFields, mutateAndGetPayload} = config; var augmentedInputFields = { - ...inputFields, + ...resolveMaybeThunk(inputFields), clientMutationId: { type: new GraphQLNonNull(GraphQLString) } }; var augmentedOutputFields = { - ...outputFields, + ...resolveMaybeThunk(outputFields), clientMutationId: { type: new GraphQLNonNull(GraphQLString) } From a9765f18121c64f0f8a30698ae55caaaa807e94e Mon Sep 17 00:00:00 2001 From: Adam Miskiewicz Date: Thu, 10 Sep 2015 19:43:29 -0400 Subject: [PATCH 2/3] the augmented fields need to be thunks as well --- src/mutation/mutation.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/mutation/mutation.js b/src/mutation/mutation.js index f2b4fcb..1ff08ca 100644 --- a/src/mutation/mutation.js +++ b/src/mutation/mutation.js @@ -58,18 +58,18 @@ export function mutationWithClientMutationId( config: MutationConfig ): GraphQLFieldConfig { var {name, inputFields, outputFields, mutateAndGetPayload} = config; - var augmentedInputFields = { + var augmentedInputFields = () => ({ ...resolveMaybeThunk(inputFields), clientMutationId: { type: new GraphQLNonNull(GraphQLString) } - }; - var augmentedOutputFields = { + }); + var augmentedOutputFields = () => ({ ...resolveMaybeThunk(outputFields), clientMutationId: { type: new GraphQLNonNull(GraphQLString) } - }; + }); var outputType = new GraphQLObjectType({ name: name + 'Payload', From 8c9c898f1e1f6eaad35c2979b77fbdc0686c131d Mon Sep 17 00:00:00 2001 From: Adam Miskiewicz Date: Thu, 10 Sep 2015 20:00:59 -0400 Subject: [PATCH 3/3] adding test case for thunk fields on mutation inputFields and outputFields --- src/mutation/__tests__/mutation.js | 56 ++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/src/mutation/__tests__/mutation.js b/src/mutation/__tests__/mutation.js index 444c96e..8b1e6f4 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: [