Skip to content

Commit 29863d0

Browse files
committed
Merge pull request #77 from flipside/master
added context param to mutation resolve
2 parents 977cbae + 0f54efb commit 29863d0

File tree

2 files changed

+62
-8
lines changed

2 files changed

+62
-8
lines changed

src/mutation/__tests__/mutation.js

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,24 @@ var simplePromiseMutation = mutationWithClientMutationId({
5757
mutateAndGetPayload: () => Promise.resolve({result: 1})
5858
});
5959

60+
var simpleRootValueMutation = mutationWithClientMutationId({
61+
name: 'SimpleRootValueMutation',
62+
inputFields: {},
63+
outputFields: {
64+
result: {
65+
type: GraphQLInt
66+
}
67+
},
68+
mutateAndGetPayload: (params, context, {rootValue}) => (rootValue)
69+
});
70+
6071
var mutation = new GraphQLObjectType({
6172
name: 'Mutation',
6273
fields: {
6374
simpleMutation: simpleMutation,
6475
simpleMutationWithThunkFields: simpleMutationWithThunkFields,
65-
simplePromiseMutation: simplePromiseMutation
76+
simplePromiseMutation: simplePromiseMutation,
77+
simpleRootValueMutation: simpleRootValueMutation
6678
}
6779
});
6880

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

160+
it('can access rootValue', () => {
161+
var query = `
162+
mutation M {
163+
simpleRootValueMutation(input: {clientMutationId: "abc"}) {
164+
result
165+
clientMutationId
166+
}
167+
}
168+
`;
169+
var expected = {
170+
data: {
171+
simpleRootValueMutation: {
172+
result: 1,
173+
clientMutationId: 'abc'
174+
}
175+
}
176+
};
177+
return expect(graphql(schema, query, {result: 1})).to.become(expected);
178+
});
179+
148180
describe('introspection', () => {
149181
it('contains correct input', () => {
150182
var query = `{
@@ -325,6 +357,26 @@ describe('mutationWithClientMutationId()', () => {
325357
kind: 'OBJECT',
326358
}
327359
},
360+
{
361+
name: 'simpleRootValueMutation',
362+
args: [
363+
{
364+
name: 'input',
365+
type: {
366+
name: null,
367+
kind: 'NON_NULL',
368+
ofType: {
369+
name: 'SimpleRootValueMutationInput',
370+
kind: 'INPUT_OBJECT'
371+
}
372+
},
373+
}
374+
],
375+
type: {
376+
name: 'SimpleRootValueMutationPayload',
377+
kind: 'OBJECT',
378+
}
379+
},
328380
]
329381
}
330382
}

src/mutation/mutation.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@ import type {
2222
GraphQLResolveInfo
2323
} from 'graphql';
2424

25-
type mutationFn = (object: Object, info: GraphQLResolveInfo) => Object |
26-
(object: Object, info: GraphQLResolveInfo) => Promise<Object>;
25+
type mutationFn =
26+
(object: Object, ctx: Object, info: GraphQLResolveInfo) => Object |
27+
(object: Object, ctx: Object, info: GraphQLResolveInfo) => Promise<Object>;
2728

2829
function resolveMaybeThunk<T>(thingOrThunk: T | () => T): T {
2930
return typeof thingOrThunk === 'function' ? thingOrThunk() : thingOrThunk;
@@ -86,11 +87,12 @@ export function mutationWithClientMutationId(
8687
args: {
8788
input: {type: new GraphQLNonNull(inputType)}
8889
},
89-
resolve: (_, {input}, info) => {
90-
return Promise.resolve(mutateAndGetPayload(input, info)).then(payload => {
91-
payload.clientMutationId = input.clientMutationId;
92-
return payload;
93-
});
90+
resolve: (_, {input}, context, info) => {
91+
return Promise.resolve(mutateAndGetPayload(input, context, info))
92+
.then(payload => {
93+
payload.clientMutationId = input.clientMutationId;
94+
return payload;
95+
});
9496
}
9597
};
9698
}

0 commit comments

Comments
 (0)