Skip to content

Commit

Permalink
Merge branch 'skevy-mutation-thunk'
Browse files Browse the repository at this point in the history
  • Loading branch information
dschafer committed Dec 9, 2015
2 parents 32ef280 + 6b1d5b9 commit 1294175
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 6 deletions.
56 changes: 56 additions & 0 deletions src/mutation/__tests__/mutation.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {},
Expand All @@ -46,6 +61,7 @@ var mutation = new GraphQLObjectType({
name: 'Mutation',
fields: {
simpleMutation: simpleMutation,
simpleMutationWithThunkFields: simpleMutationWithThunkFields,
simplePromiseMutation: simplePromiseMutation
}
});
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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: [
Expand Down
16 changes: 10 additions & 6 deletions src/mutation/mutation.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ import type {
type mutationFn = (object: Object, info: GraphQLResolveInfo) => Object |
(object: Object, info: GraphQLResolveInfo) => Promise<Object>;

function resolveMaybeThunk<T>(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.
Expand Down Expand Up @@ -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',
Expand Down

0 comments on commit 1294175

Please sign in to comment.