Apollo-server: Option like 'useQuerystring' in RequestPromise for RestDataSource

Created on 1 Aug 2018  路  2Comments  路  Source: apollographql/apollo-server

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.

source

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 ?

鉀诧笍 feature

Most helpful comment

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');
  }

All 2 comments

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dupski picture dupski  路  3Comments

hiucimon picture hiucimon  路  3Comments

jpcbarros picture jpcbarros  路  3Comments

nevyn-lookback picture nevyn-lookback  路  3Comments

stevezau picture stevezau  路  3Comments