From e8e49026dd0529c010e6134ce4403677240d1649 Mon Sep 17 00:00:00 2001 From: Chris Airey Date: Wed, 23 Jan 2019 16:20:35 -0500 Subject: [PATCH 1/2] Serialize arrays as JSON on fetch in RESTDataSource --- CHANGELOG.md | 2 ++ packages/apollo-datasource-rest/package.json | 2 +- .../src/RESTDataSource.ts | 3 ++- .../src/__tests__/RESTDataSource.test.ts | 25 +++++++++++++++++++ 4 files changed, 30 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fef4226084c..47f965fbac0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/packages/apollo-datasource-rest/package.json b/packages/apollo-datasource-rest/package.json index d5b08204802..765adbe5b3d 100644 --- a/packages/apollo-datasource-rest/package.json +++ b/packages/apollo-datasource-rest/package.json @@ -1,6 +1,6 @@ { "name": "apollo-datasource-rest", - "version": "0.2.2", + "version": "0.2.3", "author": "opensource@apollographql.com", "license": "MIT", "repository": { diff --git a/packages/apollo-datasource-rest/src/RESTDataSource.ts b/packages/apollo-datasource-rest/src/RESTDataSource.ts index 0c8f9bf1d2c..26d7a492a3c 100644 --- a/packages/apollo-datasource-rest/src/RESTDataSource.ts +++ b/packages/apollo-datasource-rest/src/RESTDataSource.ts @@ -218,11 +218,12 @@ export abstract class RESTDataSource 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')) ) { diff --git a/packages/apollo-datasource-rest/src/__tests__/RESTDataSource.test.ts b/packages/apollo-datasource-rest/src/__tests__/RESTDataSource.test.ts index 48868de308f..477ad4cc384 100644 --- a/packages/apollo-datasource-rest/src/__tests__/RESTDataSource.test.ts +++ b/packages/apollo-datasource-rest/src/__tests__/RESTDataSource.test.ts @@ -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'; From f99ebd373b682b81fd1e3fcc6fe6e409074ddbc4 Mon Sep 17 00:00:00 2001 From: Jesse Rosenberger Date: Tue, 5 Feb 2019 13:11:43 +0200 Subject: [PATCH 2/2] Set `apollo-datasource-rest` version back to v0.2.2. Lerna will take care of this on publishing. --- packages/apollo-datasource-rest/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/apollo-datasource-rest/package.json b/packages/apollo-datasource-rest/package.json index 765adbe5b3d..d5b08204802 100644 --- a/packages/apollo-datasource-rest/package.json +++ b/packages/apollo-datasource-rest/package.json @@ -1,6 +1,6 @@ { "name": "apollo-datasource-rest", - "version": "0.2.3", + "version": "0.2.2", "author": "opensource@apollographql.com", "license": "MIT", "repository": {