Skip to content

Commit

Permalink
Merge pull request #77 from flipside/master
Browse files Browse the repository at this point in the history
added context param to mutation resolve
  • Loading branch information
wincent committed Apr 15, 2016
2 parents 977cbae + 0f54efb commit 29863d0
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 8 deletions.
54 changes: 53 additions & 1 deletion src/mutation/__tests__/mutation.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,24 @@ var simplePromiseMutation = mutationWithClientMutationId({
mutateAndGetPayload: () => Promise.resolve({result: 1})
});

var simpleRootValueMutation = mutationWithClientMutationId({
name: 'SimpleRootValueMutation',
inputFields: {},
outputFields: {
result: {
type: GraphQLInt
}
},
mutateAndGetPayload: (params, context, {rootValue}) => (rootValue)
});

var mutation = new GraphQLObjectType({
name: 'Mutation',
fields: {
simpleMutation: simpleMutation,
simpleMutationWithThunkFields: simpleMutationWithThunkFields,
simplePromiseMutation: simplePromiseMutation
simplePromiseMutation: simplePromiseMutation,
simpleRootValueMutation: simpleRootValueMutation
}
});

Expand Down Expand Up @@ -145,6 +157,26 @@ describe('mutationWithClientMutationId()', () => {
return expect(graphql(schema, query)).to.become(expected);
});

it('can access rootValue', () => {
var query = `
mutation M {
simpleRootValueMutation(input: {clientMutationId: "abc"}) {
result
clientMutationId
}
}
`;
var expected = {
data: {
simpleRootValueMutation: {
result: 1,
clientMutationId: 'abc'
}
}
};
return expect(graphql(schema, query, {result: 1})).to.become(expected);
});

describe('introspection', () => {
it('contains correct input', () => {
var query = `{
Expand Down Expand Up @@ -325,6 +357,26 @@ describe('mutationWithClientMutationId()', () => {
kind: 'OBJECT',
}
},
{
name: 'simpleRootValueMutation',
args: [
{
name: 'input',
type: {
name: null,
kind: 'NON_NULL',
ofType: {
name: 'SimpleRootValueMutationInput',
kind: 'INPUT_OBJECT'
}
},
}
],
type: {
name: 'SimpleRootValueMutationPayload',
kind: 'OBJECT',
}
},
]
}
}
Expand Down
16 changes: 9 additions & 7 deletions src/mutation/mutation.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ import type {
GraphQLResolveInfo
} from 'graphql';

type mutationFn = (object: Object, info: GraphQLResolveInfo) => Object |
(object: Object, info: GraphQLResolveInfo) => Promise<Object>;
type mutationFn =
(object: Object, ctx: Object, info: GraphQLResolveInfo) => Object |
(object: Object, ctx: Object, info: GraphQLResolveInfo) => Promise<Object>;

function resolveMaybeThunk<T>(thingOrThunk: T | () => T): T {
return typeof thingOrThunk === 'function' ? thingOrThunk() : thingOrThunk;
Expand Down Expand Up @@ -86,11 +87,12 @@ export function mutationWithClientMutationId(
args: {
input: {type: new GraphQLNonNull(inputType)}
},
resolve: (_, {input}, info) => {
return Promise.resolve(mutateAndGetPayload(input, info)).then(payload => {
payload.clientMutationId = input.clientMutationId;
return payload;
});
resolve: (_, {input}, context, info) => {
return Promise.resolve(mutateAndGetPayload(input, context, info))
.then(payload => {
payload.clientMutationId = input.clientMutationId;
return payload;
});
}
};
}

0 comments on commit 29863d0

Please sign in to comment.