@@ -123,12 +123,8 @@ export interface ResponseInit {
123
123
*/
124
124
export type Response = readonly [ body : ResponseBody | null , init : ResponseInit ] ;
125
125
126
- /**
127
- * Checks whether the passed value is the `graphql-http` server agnostic response.
128
- *
129
- * @category Server
130
- */
131
- export function isResponse ( val : unknown ) : val is Response {
126
+ /** Checks whether the passed value is the `graphql-http` server agnostic response. */
127
+ function isResponse ( val : unknown ) : val is Response {
132
128
// TODO: make sure the contents of init match ResponseInit
133
129
return (
134
130
Array . isArray ( val ) &&
@@ -437,7 +433,37 @@ export function createHandler<
437
433
] ;
438
434
}
439
435
440
- const acceptedMediaType = getAcceptableMediaType ( getHeader ( req , 'accept' ) ) ;
436
+ let acceptedMediaType : AcceptableMediaType | null = null ;
437
+ const accepts = ( getHeader ( req , 'accept' ) || '*/*' )
438
+ . replace ( / \s / g, '' )
439
+ . toLowerCase ( )
440
+ . split ( ',' ) ;
441
+ for ( const accept of accepts ) {
442
+ // accept-charset became obsolete, shouldnt be used (https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept-Charset)
443
+ // TODO: handle the weight parameter "q"
444
+ const [ mediaType , ...params ] = accept . split ( ';' ) ;
445
+ const charset =
446
+ params ?. find ( ( param ) => param . includes ( 'charset=' ) ) || 'charset=utf8' ; // utf-8 is assumed when not specified;
447
+
448
+ if (
449
+ mediaType === 'application/graphql-response+json' &&
450
+ charset === 'charset=utf8'
451
+ ) {
452
+ acceptedMediaType = 'application/graphql-response+json' ;
453
+ break ;
454
+ }
455
+
456
+ // application/json should be the default until watershed
457
+ if (
458
+ ( mediaType === 'application/json' ||
459
+ mediaType === 'application/*' ||
460
+ mediaType === '*/*' ) &&
461
+ charset === 'charset=utf8'
462
+ ) {
463
+ acceptedMediaType = 'application/json' ;
464
+ break ;
465
+ }
466
+ }
441
467
if ( ! acceptedMediaType ) {
442
468
return [
443
469
null ,
@@ -670,57 +696,11 @@ export function createHandler<
670
696
} ;
671
697
}
672
698
673
- /**
674
- * Request's Media-Type that the server accepts.
675
- *
676
- * @category Server
677
- */
678
- export type AcceptableMediaType =
699
+ /** Request's Media-Type that the server accepted. */
700
+ type AcceptableMediaType =
679
701
| 'application/graphql-response+json'
680
702
| 'application/json' ;
681
703
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
704
/**
725
705
* Creates an appropriate GraphQL over HTTP response following the provided arguments.
726
706
*
@@ -731,10 +711,8 @@ export function getAcceptableMediaType(
731
711
*
732
712
* If the first argument is an `Error`, the operation will be treated as a bad request responding with `400: Bad Request` and the
733
713
* error will be present in the `ExecutionResult` style.
734
- *
735
- * @category Server
736
714
*/
737
- export function makeResponse (
715
+ function makeResponse (
738
716
resultOrErrors :
739
717
| Readonly < ExecutionResult >
740
718
| Readonly < GraphQLError [ ] >
0 commit comments