Skip to content

Commit

Permalink
Enables parsing of application/hal+json as JSON in RESTDataSource (
Browse files Browse the repository at this point in the history
  • Loading branch information
janhartmann authored and martijnwalraven committed Oct 26, 2018
1 parent c1b0af1 commit 458bc71
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 5 additions & 1 deletion packages/apollo-datasource-rest/src/RESTDataSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down

0 comments on commit 458bc71

Please sign in to comment.