@@ -10,6 +10,7 @@ import { ResponseData, RequestOptions as UndiciRequestOptions } from 'undici/typ
1010import { ApolloError } from 'apollo-server-errors'
1111import { EventEmitter , Readable } from 'stream'
1212import { Logger } from 'apollo-server-types'
13+ import { URLSearchParams } from 'url'
1314
1415type AbortSignal = unknown
1516
@@ -27,6 +28,7 @@ interface Dictionary<T> {
2728}
2829
2930export type RequestOptions = {
31+ query ?: Dictionary < string | number >
3032 body ?: string | Buffer | Uint8Array | Readable | null
3133 headers ?: Dictionary < string >
3234 signal ?: AbortSignal | EventEmitter | null
@@ -35,6 +37,7 @@ export type RequestOptions = {
3537export type Request = UndiciRequestOptions &
3638 CacheTTLOptions & {
3739 headers : Dictionary < string >
40+ query ?: Dictionary < string | number >
3841 }
3942
4043export type Response < TResult > = {
@@ -103,6 +106,19 @@ export abstract class HTTPDataSource<TContext = any> extends DataSource {
103106 this . logger = options ?. logger
104107 }
105108
109+ private buildQueryString ( query : Dictionary < string | number > ) : string {
110+ const params = new URLSearchParams ( )
111+ for ( const key in query ) {
112+ if ( Object . prototype . hasOwnProperty . call ( query , key ) ) {
113+ const value = query [ key ]
114+ if ( value !== undefined ) {
115+ params . append ( key , value . toString ( ) )
116+ }
117+ }
118+ }
119+ return params . toString ( )
120+ }
121+
106122 /**
107123 * Initialize the datasource with apollo internals (context, cache).
108124 *
@@ -275,6 +291,10 @@ export abstract class HTTPDataSource<TContext = any> extends DataSource {
275291 }
276292
277293 private async request < TResult = unknown > ( request : Request ) : Promise < Response < TResult > > {
294+ if ( request ?. query ) {
295+ request . path = request . path + '?' + this . buildQueryString ( request . query )
296+ }
297+
278298 const cacheKey = this . onCacheKeyCalculation ( request )
279299 const ttlCacheEnabled = request . requestCache
280300
0 commit comments