Enables parsing of application/hal+json as JSON in RESTDataSource (#1853)

This commit is contained in:
Jan Hartmann 2018-10-26 05:14:00 +02:00 committed by Martijn Walraven
parent c1b0af18be
commit 458bc71ead
3 changed files with 27 additions and 1 deletions

View file

@ -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

View file

@ -104,7 +104,11 @@ export abstract class RESTDataSource<TContext = any> extends DataSource {
protected parseBody(response: Response): Promise<object | string> {
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();

View file

@ -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';