Skip to content

Commit 1294175

Browse files
committed
Merge branch 'skevy-mutation-thunk'
2 parents 32ef280 + 6b1d5b9 commit 1294175

File tree

2 files changed

+66
-6
lines changed

2 files changed

+66
-6
lines changed

src/mutation/__tests__/mutation.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,21 @@ var simpleMutation = mutationWithClientMutationId({
3131
mutateAndGetPayload: () => ({result: 1})
3232
});
3333

34+
var simpleMutationWithThunkFields = mutationWithClientMutationId({
35+
name: 'SimpleMutationWithThunkFields',
36+
inputFields: () => ({
37+
inputData: {
38+
type: GraphQLInt
39+
}
40+
}),
41+
outputFields: () => ({
42+
result: {
43+
type: GraphQLInt
44+
}
45+
}),
46+
mutateAndGetPayload: ({ inputData }) => ({result: inputData})
47+
});
48+
3449
var simplePromiseMutation = mutationWithClientMutationId({
3550
name: 'SimplePromiseMutation',
3651
inputFields: {},
@@ -46,6 +61,7 @@ var mutation = new GraphQLObjectType({
4661
name: 'Mutation',
4762
fields: {
4863
simpleMutation: simpleMutation,
64+
simpleMutationWithThunkFields: simpleMutationWithThunkFields,
4965
simplePromiseMutation: simplePromiseMutation
5066
}
5167
});
@@ -89,6 +105,26 @@ describe('mutationWithClientMutationId()', () => {
89105
return expect(graphql(schema, query)).to.become(expected);
90106
});
91107

108+
it('Supports thunks as input and output fields', () => {
109+
var query = `
110+
mutation M {
111+
simpleMutationWithThunkFields(input: {inputData: 1234, clientMutationId: "abc"}) {
112+
result
113+
clientMutationId
114+
}
115+
}
116+
`;
117+
var expected = {
118+
data: {
119+
simpleMutationWithThunkFields: {
120+
result: 1234,
121+
clientMutationId: 'abc'
122+
}
123+
}
124+
};
125+
return expect(graphql(schema, query)).to.become(expected);
126+
});
127+
92128
it('supports promise mutations', () => {
93129
var query = `
94130
mutation M {
@@ -249,6 +285,26 @@ describe('mutationWithClientMutationId()', () => {
249285
kind: 'OBJECT',
250286
}
251287
},
288+
{
289+
name: 'simpleMutationWithThunkFields',
290+
args: [
291+
{
292+
name: 'input',
293+
type: {
294+
name: null,
295+
kind: 'NON_NULL',
296+
ofType: {
297+
name: 'SimpleMutationWithThunkFieldsInput',
298+
kind: 'INPUT_OBJECT'
299+
}
300+
},
301+
}
302+
],
303+
type: {
304+
name: 'SimpleMutationWithThunkFieldsPayload',
305+
kind: 'OBJECT',
306+
}
307+
},
252308
{
253309
name: 'simplePromiseMutation',
254310
args: [

src/mutation/mutation.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ import type {
2525
type mutationFn = (object: Object, info: GraphQLResolveInfo) => Object |
2626
(object: Object, info: GraphQLResolveInfo) => Promise<Object>;
2727

28+
function resolveMaybeThunk<T>(thingOrThunk: T | () => T): T {
29+
return typeof thingOrThunk === 'function' ? thingOrThunk() : thingOrThunk;
30+
}
31+
2832
/**
2933
* A description of a mutation consumable by mutationWithClientMutationId
3034
* to create a GraphQLFieldConfig for that mutation.
@@ -54,18 +58,18 @@ export function mutationWithClientMutationId(
5458
config: MutationConfig
5559
): GraphQLFieldConfig {
5660
var {name, inputFields, outputFields, mutateAndGetPayload} = config;
57-
var augmentedInputFields = {
58-
...inputFields,
61+
var augmentedInputFields = () => ({
62+
...resolveMaybeThunk(inputFields),
5963
clientMutationId: {
6064
type: new GraphQLNonNull(GraphQLString)
6165
}
62-
};
63-
var augmentedOutputFields = {
64-
...outputFields,
66+
});
67+
var augmentedOutputFields = () => ({
68+
...resolveMaybeThunk(outputFields),
6569
clientMutationId: {
6670
type: new GraphQLNonNull(GraphQLString)
6771
}
68-
};
72+
});
6973

7074
var outputType = new GraphQLObjectType({
7175
name: name + 'Payload',

0 commit comments

Comments
 (0)