@@ -52,7 +52,21 @@ export interface Request<RawRequest, Context> {
52
52
readonly method : string ;
53
53
readonly url : string ;
54
54
readonly headers : RequestHeaders ;
55
- readonly body : string | Record < string , unknown > | null ;
55
+ /**
56
+ * Parsed request body or a parser function.
57
+ *
58
+ * If the provided function throws, the error message "Unparsable JSON body" will
59
+ * be in the erroneous response.
60
+ */
61
+ readonly body :
62
+ | string
63
+ | Record < string , unknown >
64
+ | null
65
+ | ( ( ) =>
66
+ | string
67
+ | Record < string , unknown >
68
+ | null
69
+ | Promise < string | Record < string , unknown > | null > ) ;
56
70
/**
57
71
* The raw request itself from the implementing server.
58
72
*
@@ -412,16 +426,18 @@ export function createHandler<RawRequest = unknown, Context = unknown>(
412
426
if ( ! req . body ) {
413
427
throw new Error ( 'Missing body' ) ;
414
428
}
429
+ let data ;
415
430
try {
416
- const data =
417
- typeof req . body === 'string' ? JSON . parse ( req . body ) : req . body ;
418
- partParams . operationName = data . operationName ;
419
- partParams . query = data . query ;
420
- partParams . variables = data . variables ;
421
- partParams . extensions = data . extensions ;
422
- } catch {
431
+ const body =
432
+ typeof req . body === 'function' ? await req . body ( ) : req . body ;
433
+ data = typeof body === 'string' ? JSON . parse ( body ) : body ;
434
+ } catch ( err ) {
423
435
throw new Error ( 'Unparsable JSON body' ) ;
424
436
}
437
+ partParams . operationName = data . operationName ;
438
+ partParams . query = data . query ;
439
+ partParams . variables = data . variables ;
440
+ partParams . extensions = data . extensions ;
425
441
break ;
426
442
}
427
443
default : // graphql-http doesnt support any other content type
0 commit comments