@@ -437,7 +437,37 @@ export function createHandler<
437
437
] ;
438
438
}
439
439
440
- const acceptedMediaType = getAcceptableMediaType ( getHeader ( req , 'accept' ) ) ;
440
+ let acceptedMediaType : AcceptableMediaType | null = null ;
441
+ const accepts = ( getHeader ( req , 'accept' ) || '*/*' )
442
+ . replace ( / \s / g, '' )
443
+ . toLowerCase ( )
444
+ . split ( ',' ) ;
445
+ for ( const accept of accepts ) {
446
+ // accept-charset became obsolete, shouldnt be used (https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept-Charset)
447
+ // TODO: handle the weight parameter "q"
448
+ const [ mediaType , ...params ] = accept . split ( ';' ) ;
449
+ const charset =
450
+ params ?. find ( ( param ) => param . includes ( 'charset=' ) ) || 'charset=utf8' ; // utf-8 is assumed when not specified;
451
+
452
+ if (
453
+ mediaType === 'application/graphql-response+json' &&
454
+ charset === 'charset=utf8'
455
+ ) {
456
+ acceptedMediaType = 'application/graphql-response+json' ;
457
+ break ;
458
+ }
459
+
460
+ // application/json should be the default until watershed
461
+ if (
462
+ ( mediaType === 'application/json' ||
463
+ mediaType === 'application/*' ||
464
+ mediaType === '*/*' ) &&
465
+ charset === 'charset=utf8'
466
+ ) {
467
+ acceptedMediaType = 'application/json' ;
468
+ break ;
469
+ }
470
+ }
441
471
if ( ! acceptedMediaType ) {
442
472
return [
443
473
null ,
@@ -670,57 +700,11 @@ export function createHandler<
670
700
} ;
671
701
}
672
702
673
- /**
674
- * Request's Media-Type that the server accepts.
675
- *
676
- * @category Server
677
- */
678
- export type AcceptableMediaType =
703
+ /** Request's Media-Type that the server accepted. */
704
+ type AcceptableMediaType =
679
705
| 'application/graphql-response+json'
680
706
| 'application/json' ;
681
707
682
- /**
683
- * Inspects the request and detects the appropriate/acceptable Media-Type
684
- * looking at the `Accept` header while complying with the GraphQL over HTTP spec.
685
- *
686
- * @category Server
687
- */
688
- export function getAcceptableMediaType (
689
- acceptHeader : string | null | undefined ,
690
- ) : AcceptableMediaType | null {
691
- let acceptedMediaType : AcceptableMediaType | null = null ;
692
- const accepts = ( acceptHeader || '*/*' )
693
- . replace ( / \s / g, '' )
694
- . toLowerCase ( )
695
- . split ( ',' ) ;
696
- for ( const accept of accepts ) {
697
- // accept-charset became obsolete, shouldnt be used (https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept-Charset)
698
- // TODO: handle the weight parameter "q"
699
- const [ mediaType , ...params ] = accept . split ( ';' ) ;
700
- const charset =
701
- params ?. find ( ( param ) => param . includes ( 'charset=' ) ) || 'charset=utf8' ; // utf-8 is assumed when not specified;
702
-
703
- if (
704
- mediaType === 'application/graphql-response+json' &&
705
- charset === 'charset=utf8'
706
- ) {
707
- acceptedMediaType = 'application/graphql-response+json' ;
708
- break ;
709
- }
710
-
711
- if (
712
- ( mediaType === 'application/json' ||
713
- mediaType === 'application/*' ||
714
- mediaType === '*/*' ) &&
715
- charset === 'charset=utf8'
716
- ) {
717
- acceptedMediaType = 'application/json' ;
718
- break ;
719
- }
720
- }
721
- return acceptedMediaType ;
722
- }
723
-
724
708
/**
725
709
* Creates an appropriate GraphQL over HTTP response following the provided arguments.
726
710
*
@@ -731,10 +715,8 @@ export function getAcceptableMediaType(
731
715
*
732
716
* If the first argument is an `Error`, the operation will be treated as a bad request responding with `400: Bad Request` and the
733
717
* error will be present in the `ExecutionResult` style.
734
- *
735
- * @category Server
736
718
*/
737
- export function makeResponse (
719
+ function makeResponse (
738
720
resultOrErrors :
739
721
| Readonly < ExecutionResult >
740
722
| Readonly < GraphQLError [ ] >
0 commit comments