@@ -114,7 +114,7 @@ describe('middleware test', () => {
114114 } ) ;
115115 } ) ;
116116
117- it ( 'rewrites mutations and responses too' , async ( ) => {
117+ const setupMutationApp = ( ) => {
118118 const app = express ( ) ;
119119
120120 app . use (
@@ -141,7 +141,11 @@ describe('middleware test', () => {
141141 rootValue
142142 } )
143143 ) ;
144+ return app ;
145+ } ;
144146
147+ it ( 'rewrites mutations and responses too' , async ( ) => {
148+ const app = setupMutationApp ( ) ;
145149 // in the past, we didn't use input or output types correctly
146150 // so we need to rewrite the query to this old query will work still
147151 const deprecatedQuery = `
@@ -185,4 +189,190 @@ describe('middleware test', () => {
185189 }
186190 } ) ;
187191 } ) ;
192+
193+ it ( 'works with no variables' , async ( ) => {
194+ const app = setupMutationApp ( ) ;
195+
196+ // in the past, we didn't use input or output types correctly
197+ // so we need to rewrite the query to this old query will work still
198+ const deprecatedQuery = `
199+ mutation {
200+ makePokemon(name: "Squirtle") {
201+ id
202+ name
203+ }
204+ }
205+ ` ;
206+
207+ const deprecatedRes = await request ( app )
208+ . post ( '/graphql' )
209+ . send ( { query : deprecatedQuery } ) ;
210+ expect ( deprecatedRes . body . errors ) . toBe ( undefined ) ;
211+ expect ( deprecatedRes . body . data . makePokemon ) . toEqual ( {
212+ id : '17' ,
213+ name : 'Squirtle'
214+ } ) ;
215+
216+ // the new version of the query should still work with no problem though
217+ const newQuery = `
218+ mutation {
219+ makePokemon(input: { name: "Squirtle" }) {
220+ pokemon {
221+ id
222+ name
223+ }
224+ }
225+ }
226+ ` ;
227+
228+ const newRes = await request ( app )
229+ . post ( '/graphql' )
230+ . send ( { query : newQuery } ) ;
231+ expect ( newRes . body . errors ) . toBe ( undefined ) ;
232+ expect ( newRes . body . data . makePokemon ) . toEqual ( {
233+ pokemon : {
234+ id : '17' ,
235+ name : 'Squirtle'
236+ }
237+ } ) ;
238+ } ) ;
239+
240+ it ( 'doesnt transform the response if theres no _rewriteHandler' , async ( ) => {
241+ const app = express ( ) ;
242+
243+ app . use (
244+ '/graphql' ,
245+ graphqlRewriterMiddleware ( {
246+ rewriters : [
247+ new FieldArgsToInputTypeRewriter ( {
248+ fieldName : 'makePokemon' ,
249+ argNames : [ 'name' ]
250+ } ) ,
251+ new NestFieldOutputsRewriter ( {
252+ fieldName : 'makePokemon' ,
253+ newOutputName : 'pokemon' ,
254+ outputsToNest : [ 'id' , 'name' ]
255+ } )
256+ ]
257+ } )
258+ ) ;
259+
260+ app . use ( '/graphql' , ( req , res ) => {
261+ ( req as any ) . _rewriteHandler = null ;
262+ res . json ( { data : { random : true } } ) ;
263+ } ) ;
264+
265+ const deprecatedQuery = `
266+ mutation {
267+ makePokemon(name: "Squirtle") {
268+ id
269+ name
270+ }
271+ }
272+ ` ;
273+
274+ const deprecatedRes = await request ( app )
275+ . post ( '/graphql' )
276+ . send ( { query : deprecatedQuery } ) ;
277+ expect ( deprecatedRes . body . data ) . toEqual ( { random : true } ) ;
278+ } ) ;
279+
280+ it ( 'ignores missing queries or invalid graphql' , async ( ) => {
281+ const app = express ( ) ;
282+
283+ app . use (
284+ '/graphql' ,
285+ graphqlRewriterMiddleware ( {
286+ rewriters : [
287+ new FieldArgsToInputTypeRewriter ( {
288+ fieldName : 'makePokemon' ,
289+ argNames : [ 'name' ]
290+ } ) ,
291+ new NestFieldOutputsRewriter ( {
292+ fieldName : 'makePokemon' ,
293+ newOutputName : 'pokemon' ,
294+ outputsToNest : [ 'id' , 'name' ]
295+ } )
296+ ]
297+ } )
298+ ) ;
299+
300+ app . use ( '/graphql' , ( req , res ) => {
301+ res . json ( { data : { random : true } } ) ;
302+ } ) ;
303+
304+ const missingQueryRes = await request ( app ) . post ( '/graphql' ) ;
305+ expect ( missingQueryRes . body . data ) . toEqual ( { random : true } ) ;
306+
307+ const invalidQueryRes = await request ( app )
308+ . post ( '/graphql' )
309+ . send ( { query : '123 peekaboo!' } ) ;
310+ expect ( invalidQueryRes . body . data ) . toEqual ( { random : true } ) ;
311+ } ) ;
312+
313+ it ( 'ignores invalid json responses' , async ( ) => {
314+ const app = express ( ) ;
315+
316+ app . use (
317+ '/graphql' ,
318+ graphqlRewriterMiddleware ( {
319+ rewriters : [
320+ new FieldArgsToInputTypeRewriter ( {
321+ fieldName : 'makePokemon' ,
322+ argNames : [ 'name' ]
323+ } ) ,
324+ new NestFieldOutputsRewriter ( {
325+ fieldName : 'makePokemon' ,
326+ newOutputName : 'pokemon' ,
327+ outputsToNest : [ 'id' , 'name' ]
328+ } )
329+ ]
330+ } )
331+ ) ;
332+
333+ app . use ( '/graphql' , ( req , res ) => {
334+ res . json ( 'jimmy' ) ;
335+ } ) ;
336+
337+ const deprecatedQuery = `
338+ mutation {
339+ makePokemon(name: "Squirtle") {
340+ id
341+ name
342+ }
343+ }
344+ ` ;
345+
346+ const invalidQueryRes = await request ( app )
347+ . post ( '/graphql' )
348+ . send ( { query : deprecatedQuery } ) ;
349+ expect ( invalidQueryRes . body ) . toEqual ( 'jimmy' ) ;
350+ } ) ;
351+
352+ it ( 'throws on invalid graphql if ignoreParsingErrors === false' , async ( ) => {
353+ const app = express ( ) ;
354+
355+ app . use (
356+ '/graphql' ,
357+ graphqlRewriterMiddleware ( {
358+ ignoreParsingErrors : false ,
359+ rewriters : [
360+ new FieldArgsToInputTypeRewriter ( {
361+ fieldName : 'makePokemon' ,
362+ argNames : [ 'name' ]
363+ } )
364+ ]
365+ } )
366+ ) ;
367+
368+ app . use ( '/graphql' , ( req , res ) => {
369+ res . json ( { data : { random : true } } ) ;
370+ } ) ;
371+
372+ const errorRes = await request ( app )
373+ . post ( '/graphql' )
374+ . send ( { query : '123 peekaboo!' } ) ;
375+ expect ( errorRes . status ) . toBe ( 500 ) ;
376+ expect ( errorRes . body ) . toEqual ( { } ) ;
377+ } ) ;
188378} ) ;
0 commit comments