@@ -8,7 +8,7 @@ import sjson from 'secure-json-parse'
88import { KeyValueCache } from 'apollo-server-caching'
99import { ResponseData } from 'undici/types/dispatcher'
1010import { toApolloError } from 'apollo-server-errors'
11- import { EventEmitter , Readable } from 'stream'
11+ import { EventEmitter } from 'stream'
1212import { Logger } from 'apollo-server-types'
1313import { URLSearchParams } from 'url'
1414
@@ -42,19 +42,14 @@ interface Dictionary<T> {
4242 [ Key : string ] : T | undefined
4343}
4444
45- export type RequestOptions = {
46- context ?: Dictionary < string >
47- query ?: Dictionary < string | number >
48- body ?: string | Buffer | Uint8Array | Readable | null
49- headers ?: Dictionary < string >
50- signal ?: AbortSignal
51- } & CacheTTLOptions
45+ export type RequestOptions = Omit < Partial < Request > , 'origin' | 'path' | 'method' >
5246
5347export type Request = {
5448 context : Dictionary < string >
5549 query : Dictionary < string | number >
56- body : string | Buffer | Uint8Array | Readable | null
50+ body : any
5751 signal ?: AbortSignal | EventEmitter | null
52+ json ?: boolean
5853 origin : string
5954 path : string
6055 method : string
@@ -274,14 +269,23 @@ export abstract class HTTPDataSource<TContext = any> extends DataSource {
274269 await this . onRequest ?.( request )
275270
276271 try {
277- const responseData = await this . pool . request ( {
272+ const requestOptions = {
278273 method : request . method ,
279274 origin : request . origin ,
280275 path : request . path ,
281276 body : request . body ,
282277 headers : request . headers ,
283278 signal : request . signal ,
284- } )
279+ }
280+
281+ if ( request . json === true ) {
282+ if ( requestOptions . headers [ 'content-type' ] === undefined ) {
283+ requestOptions . headers [ 'content-type' ] = 'application/json; charset=utf-8'
284+ }
285+ requestOptions . body = JSON . stringify ( requestOptions . body )
286+ }
287+
288+ const responseData = await this . pool . request ( requestOptions )
285289 responseData . body . setEncoding ( 'utf8' )
286290
287291 let data = ''
0 commit comments