diff --git a/CHANGELOG.md b/CHANGELOG.md index d82ba35e11c..393759d95c9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ - Allow an optional function to resolve the `rootValue`, passing the `DocumentNode` AST to determine the value. [PR #1555](https://github.com/apollographql/apollo-server/pull/1555) - Follow-up on the work in [PR #1516](https://github.com/apollographql/apollo-server/pull/1516) to also fix missing insertion cursor/caret when a custom GraphQL configuration is specified which doesn't specify its own `cursorShape` property. [PR #1607](https://github.com/apollographql/apollo-server/pull/1607) +- Allow JSON parsing in `RESTDataSource` of Content Type `application/hal+json`. [PR ##185](https://github.com/apollographql/apollo-server/pull/1853) ### v2.1.0 diff --git a/packages/apollo-datasource-rest/src/RESTDataSource.ts b/packages/apollo-datasource-rest/src/RESTDataSource.ts index 59b068768e6..0c8f9bf1d2c 100644 --- a/packages/apollo-datasource-rest/src/RESTDataSource.ts +++ b/packages/apollo-datasource-rest/src/RESTDataSource.ts @@ -104,7 +104,11 @@ export abstract class RESTDataSource extends DataSource { protected parseBody(response: Response): Promise { const contentType = response.headers.get('Content-Type'); - if (contentType && contentType.startsWith('application/json')) { + if ( + contentType && + (contentType.startsWith('application/json') || + contentType.startsWith('application/hal+json')) + ) { return response.json(); } else { return response.text(); diff --git a/packages/apollo-datasource-rest/src/__tests__/RESTDataSource.test.ts b/packages/apollo-datasource-rest/src/__tests__/RESTDataSource.test.ts index b2a8ef90220..48868de308f 100644 --- a/packages/apollo-datasource-rest/src/__tests__/RESTDataSource.test.ts +++ b/packages/apollo-datasource-rest/src/__tests__/RESTDataSource.test.ts @@ -392,6 +392,27 @@ describe('RESTDataSource', () => { expect(data).toEqual({ foo: 'bar' }); }); + it('returns data as parsed JSON when Content-Type is application/hal+json', async () => { + const dataSource = new class extends RESTDataSource { + baseURL = 'https://api.example.com'; + + getFoo() { + return this.get('foo'); + } + }(); + + dataSource.httpCache = httpCache; + + fetch.mockJSONResponseOnce( + { foo: 'bar' }, + { 'Content-Type': 'application/hal+json' }, + ); + + const data = await dataSource.getFoo(); + + expect(data).toEqual({ foo: 'bar' }); + }); + it('returns data as a string when Content-Type is text/plain', async () => { const dataSource = new class extends RESTDataSource { baseURL = 'https://api.example.com';