Skip to content

Commit e2a7696

Browse files
author
dustin deus
committed
support query params
1 parent 9c6efec commit e2a7696

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

src/http-data-source.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { ResponseData, RequestOptions as UndiciRequestOptions } from 'undici/typ
1010
import { ApolloError } from 'apollo-server-errors'
1111
import { EventEmitter, Readable } from 'stream'
1212
import { Logger } from 'apollo-server-types'
13+
import { URLSearchParams } from 'url'
1314

1415
type AbortSignal = unknown
1516

@@ -27,6 +28,7 @@ interface Dictionary<T> {
2728
}
2829

2930
export 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 = {
3537
export type Request = UndiciRequestOptions &
3638
CacheTTLOptions & {
3739
headers: Dictionary<string>
40+
query?: Dictionary<string | number>
3841
}
3942

4043
export 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

test/rest-data-source.test.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,46 @@ test('Should be able to make a simple PUT call', async (t) => {
151151
t.deepEqual(response.body, { name: 'foo' })
152152
})
153153

154+
test('Should be able to pass query params', async (t) => {
155+
t.plan(3)
156+
157+
const path = '/'
158+
159+
const wanted = { name: 'foo' }
160+
161+
const server = http.createServer((req, res) => {
162+
t.is(req.method, 'GET')
163+
t.is(req.url, '/?a=1&b=2')
164+
res.write(JSON.stringify(wanted))
165+
res.end()
166+
res.socket?.unref()
167+
})
168+
169+
t.teardown(server.close.bind(server))
170+
171+
server.listen()
172+
173+
const baseURL = `http://localhost:${(server.address() as AddressInfo)?.port}`
174+
175+
const dataSource = new (class extends HTTPDataSource {
176+
constructor() {
177+
super(baseURL)
178+
}
179+
getFoo() {
180+
return this.get(path, {
181+
query: {
182+
a: 1,
183+
b: '2'
184+
}
185+
})
186+
}
187+
})()
188+
189+
const response = await dataSource.getFoo()
190+
191+
t.deepEqual(response.body, { name: 'foo' })
192+
})
193+
154194
test('Should error', async (t) => {
155195
t.plan(2)
156196

0 commit comments

Comments
 (0)