File tree Expand file tree Collapse file tree 2 files changed +45
-3
lines changed Expand file tree Collapse file tree 2 files changed +45
-3
lines changed Original file line number Diff line number Diff line change @@ -195,9 +195,43 @@ describe('mutationWithClientMutationId()', () => {
195
195
}
196
196
` ;
197
197
198
+ expect ( graphqlSync ( { schema, source } ) ) . to . deep . equal ( {
199
+ data : { someMutation : null } ,
200
+ } ) ;
201
+ } ) ;
202
+
203
+ it ( 'supports mutations returning custom classes' , ( ) => {
204
+ class SomeClass {
205
+ getSomeGeneratedData ( ) {
206
+ return 1 ;
207
+ }
208
+ }
209
+
210
+ const someMutation = mutationWithClientMutationId ( {
211
+ name : 'SomeMutation' ,
212
+ inputFields : { } ,
213
+ outputFields : {
214
+ result : {
215
+ type : GraphQLInt ,
216
+ resolve : ( obj ) => obj . getSomeGeneratedData ( ) ,
217
+ } ,
218
+ } ,
219
+ mutateAndGetPayload : ( ) => new SomeClass ( ) ,
220
+ } ) ;
221
+ const schema = wrapInSchema ( { someMutation } ) ;
222
+
223
+ const source = `
224
+ mutation {
225
+ someMutation(input: {clientMutationId: "abc"}) {
226
+ result
227
+ clientMutationId
228
+ }
229
+ }
230
+ ` ;
231
+
198
232
expect ( graphqlSync ( { schema, source } ) ) . to . deep . equal ( {
199
233
data : {
200
- someMutation : { result : null , clientMutationId : 'abc' } ,
234
+ someMutation : { result : 1 , clientMutationId : 'abc' } ,
201
235
} ,
202
236
} ) ;
203
237
} ) ;
Original file line number Diff line number Diff line change @@ -91,10 +91,18 @@ export function mutationWithClientMutationId(
91
91
const { clientMutationId } = input ;
92
92
const payload = mutateAndGetPayload ( input , context , info ) ;
93
93
if ( isPromise ( payload ) ) {
94
- return payload . then ( ( data ) => ( { ... data , clientMutationId } ) ) ;
94
+ return payload . then ( injectClientMutationId ) ;
95
95
}
96
+ return injectClientMutationId ( payload ) ;
96
97
97
- return { ...payload , clientMutationId } ;
98
+ function injectClientMutationId ( data : mixed ) {
99
+ if ( typeof data === 'object' && data !== null ) {
100
+ // $FlowFixMe[cannot-write] It's bad idea to mutate data but we need to pass clientMutationId somehow. Maybe in future we figure out better solution satisfying all our test cases.
101
+ data . clientMutationId = clientMutationId ;
102
+ }
103
+
104
+ return data ;
105
+ }
98
106
} ,
99
107
} ;
100
108
}
You can’t perform that action at this time.
0 commit comments