Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Serialize arrays as JSON on fetch in RESTDataSource #2219

Merged
merged 2 commits into from
Feb 5, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

### vNEXT

- Fix: Serialize arrays as JSON on fetch in `RESTDataSource`. [PR #2219](https://github.com/apollographql/apollo-server/pull/2219)

### v2.3.3

- `apollo-server` (only): Stop double-invocation of `serverWillStart` life-cycle event. (More specific integrations - e.g. Express, Koa, Hapi, etc. - were unaffected.) [PR #2239](https://github.com/apollographql/apollo-server/pull/2239)
Expand Down
2 changes: 1 addition & 1 deletion packages/apollo-datasource-rest/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "apollo-datasource-rest",
"version": "0.2.2",
"version": "0.2.3",
"author": "opensource@apollographql.com",
"license": "MIT",
"repository": {
Expand Down
3 changes: 2 additions & 1 deletion packages/apollo-datasource-rest/src/RESTDataSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,11 +218,12 @@ export abstract class RESTDataSource<TContext = any> extends DataSource {
url.searchParams.append(name, value);
}

// We accept arbitrary objects as body and serialize them as JSON
// We accept arbitrary objects and arrays as body and serialize them as JSON
if (
options.body !== undefined &&
options.body !== null &&
(options.body.constructor === Object ||
Array.isArray(options.body) ||
((options.body as any).toJSON &&
typeof (options.body as any).toJSON === 'function'))
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,31 @@ describe('RESTDataSource', () => {
);
});

it('serializes a request body that is an array as JSON', async () => {
const dataSource = new class extends RESTDataSource {
baseURL = 'https://api.example.com';

postFoo(foo) {
return this.post('foo', foo);
}
}();

dataSource.httpCache = httpCache;

fetch.mockJSONResponseOnce();

await dataSource.postFoo(['foo', 'bar']);

expect(fetch.mock.calls.length).toEqual(1);
expect(fetch.mock.calls[0][0].url).toEqual('https://api.example.com/foo');
expect(fetch.mock.calls[0][0].body).toEqual(
JSON.stringify(['foo', 'bar']),
);
expect(fetch.mock.calls[0][0].headers.get('Content-Type')).toEqual(
'application/json',
);
});

it('serializes a request body that has a toJSON method as JSON', async () => {
const dataSource = new class extends RESTDataSource {
baseURL = 'https://api.example.com';
Expand Down