The 'useQuerystring' option in RequestPromise allows me to send a query param array as follows: &additional=test1&additional=test2
useQuerystring - if true, use querystring to stringify and parse querystrings, otherwise use qs (default: false). Set this option to true if you need arrays to be serialized as foo=bar&foo=baz instead of the default foo[0]=bar&foo[1]=baz.
When I try to use RestDataSource I can't seem to get the url right. Is there an option to make something like this;
this.get(`/rest/v1/test`, {
additional: ['test1', 'test2', 'test3']
});
to come up with a url like: http://localhost:8080/rest/v1/test?additional=test1&additional=test2&additional=test3 ?
Query parameter handling in RESTDataSource is based on the standard URLSearchParams class.
That offers various ways of constructing params. You could pass a query string for example:
this.get(`/rest/v1/test`, 'additional=test1&additional=test2&additional=test3');
You can also pass in a sequence:
this.get(`/rest/v1/test`, [['additional', 'test1'],['additional', 'test2'], ['additional', 'test3']]);
And you could also construct params programmatically. That would give you something like:
const params = new URLSearchParams();
params.append('additional', 'test1');
params.append('additional', 'test2');
params.append('additional', 'test3');
this.get(`/rest/v1/test`, params);
(In that case, you would need to import URLSearchParams however. It is currently only exported from apollo-server-env, but maybe we should re-export it from apollo-datasource-rest.)
Note that you don't have to set this per-request, you can also use the willSendRequest lifecycle method to modify every request:
willSendRequest(request: RequestOptions) {
request.params.append('additional', 'test1');
request.params.append('additional', 'test2');
request.params.append('additional', 'test3');
}
Thank you. I'll try these methods.
Most helpful comment
Query parameter handling in
RESTDataSourceis based on the standardURLSearchParamsclass.That offers various ways of constructing params. You could pass a query string for example:
You can also pass in a sequence:
And you could also construct params programmatically. That would give you something like:
(In that case, you would need to import
URLSearchParamshowever. It is currently only exported fromapollo-server-env, but maybe we should re-export it fromapollo-datasource-rest.)Note that you don't have to set this per-request, you can also use the
willSendRequestlifecycle method to modify every request: