@@ -166,7 +166,7 @@ describe('middleware test', () => {
166
166
} ) ;
167
167
} ) ;
168
168
169
- const setupMutationApp = ( ) => {
169
+ const setupMutationApp = ( extraExpressGraphqlOpts : any = { } ) => {
170
170
const app = express ( ) ;
171
171
172
172
app . use (
@@ -190,7 +190,8 @@ describe('middleware test', () => {
190
190
'/graphql' ,
191
191
graphqlHTTP ( {
192
192
schema,
193
- rootValue
193
+ rootValue,
194
+ ...extraExpressGraphqlOpts
194
195
} )
195
196
) ;
196
197
return app ;
@@ -362,7 +363,7 @@ describe('middleware test', () => {
362
363
expect ( invalidQueryRes . body . data ) . toEqual ( { random : true } ) ;
363
364
} ) ;
364
365
365
- it ( 'ignores invalid json responses' , async ( ) => {
366
+ it ( 'ignores invalid json responses sent via response.json() ' , async ( ) => {
366
367
const app = express ( ) ;
367
368
368
369
app . use (
@@ -401,6 +402,106 @@ describe('middleware test', () => {
401
402
expect ( invalidQueryRes . body ) . toEqual ( 'jimmy' ) ;
402
403
} ) ;
403
404
405
+ it ( 'ignores invalid json responses sent via response.end()' , async ( ) => {
406
+ const app = express ( ) ;
407
+
408
+ app . use (
409
+ '/graphql' ,
410
+ graphqlRewriterMiddleware ( {
411
+ rewriters : [
412
+ new FieldArgsToInputTypeRewriter ( {
413
+ fieldName : 'makePokemon' ,
414
+ argNames : [ 'name' ]
415
+ } ) ,
416
+ new NestFieldOutputsRewriter ( {
417
+ fieldName : 'makePokemon' ,
418
+ newOutputName : 'pokemon' ,
419
+ outputsToNest : [ 'id' , 'name' ]
420
+ } )
421
+ ]
422
+ } )
423
+ ) ;
424
+
425
+ app . use ( '/graphql' , ( req , res ) => {
426
+ const messedUpRes = Buffer . from ( 'jisdhfiods{{{{' , 'utf8' ) ;
427
+ res . setHeader ( 'Content-Type' , 'application/json; charset=utf-8' ) ;
428
+ res . setHeader ( 'Content-Length' , String ( messedUpRes . length ) ) ;
429
+ res . end ( messedUpRes ) ;
430
+ } ) ;
431
+
432
+ const deprecatedQuery = `
433
+ mutation {
434
+ makePokemon(name: "Squirtle") {
435
+ id
436
+ name
437
+ }
438
+ }
439
+ ` ;
440
+
441
+ const invalidQueryRes = await request ( app )
442
+ . post ( '/graphql' )
443
+ . send ( { query : deprecatedQuery } )
444
+ // disable supertest json parsing
445
+ . buffer ( true )
446
+ . parse ( ( res , cb ) => {
447
+ let data = Buffer . from ( '' ) ;
448
+ res . on ( 'data' , chunk => {
449
+ data = Buffer . concat ( [ data , chunk ] ) ;
450
+ } ) ;
451
+ res . on ( 'end' , ( ) => {
452
+ cb ( null , data . toString ( ) ) ;
453
+ } ) ;
454
+ } ) ;
455
+
456
+ expect ( invalidQueryRes . body ) . toEqual ( 'jisdhfiods{{{{' ) ;
457
+ } ) ;
458
+
459
+ it ( 'is able to rewriter responses with pretty printing enabled on express-graphql' , async ( ) => {
460
+ const app = setupMutationApp ( { pretty : true } ) ;
461
+ // in the past, we didn't use input or output types correctly
462
+ // so we need to rewrite the query to this old query will work still
463
+ const deprecatedQuery = `
464
+ mutation makePokemonWithWrongType($name: String!) {
465
+ makePokemon(name: $name) {
466
+ id
467
+ name
468
+ }
469
+ }
470
+ ` ;
471
+
472
+ const deprecatedRes = await request ( app )
473
+ . post ( '/graphql' )
474
+ . send ( { query : deprecatedQuery , variables : { name : 'Squirtle' } } ) ;
475
+ expect ( deprecatedRes . body . errors ) . toBe ( undefined ) ;
476
+ expect ( deprecatedRes . body . data . makePokemon ) . toEqual ( {
477
+ id : '17' ,
478
+ name : 'Squirtle'
479
+ } ) ;
480
+
481
+ // the new version of the query should still work with no problem though
482
+ const newQuery = `
483
+ mutation makePokemon($input: MakePokemonInput!) {
484
+ makePokemon(input: $input) {
485
+ pokemon {
486
+ id
487
+ name
488
+ }
489
+ }
490
+ }
491
+ ` ;
492
+
493
+ const newRes = await request ( app )
494
+ . post ( '/graphql' )
495
+ . send ( { query : newQuery , variables : { input : { name : 'Squirtle' } } } ) ;
496
+ expect ( newRes . body . errors ) . toBe ( undefined ) ;
497
+ expect ( newRes . body . data . makePokemon ) . toEqual ( {
498
+ pokemon : {
499
+ id : '17' ,
500
+ name : 'Squirtle'
501
+ }
502
+ } ) ;
503
+ } ) ;
504
+
404
505
it ( 'throws on invalid graphql if ignoreParsingErrors === false' , async ( ) => {
405
506
const app = express ( ) ;
406
507
0 commit comments